~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Sharpen/Sharpen/FileChannel.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace Sharpen
 
2
{
 
3
        using System;
 
4
        using System.IO;
 
5
 
 
6
        internal class FileChannel
 
7
        {
 
8
                private FileStream s;
 
9
                byte[] buffer;
 
10
                bool isOpen;
 
11
 
 
12
                internal FileChannel (FileStream s)
 
13
                {
 
14
                        this.s = s;
 
15
                        isOpen = true;
 
16
                }
 
17
                
 
18
                internal FileStream Stream {
 
19
                        get { return s; }
 
20
                }
 
21
 
 
22
                public void Close ()
 
23
                {
 
24
                        isOpen = false;
 
25
                        s.Close ();
 
26
                }
 
27
                
 
28
                public bool IsOpen ()
 
29
                {
 
30
                        return isOpen;
 
31
                }
 
32
 
 
33
                public void Force (bool f)
 
34
                {
 
35
                        s.Flush ();
 
36
                }
 
37
 
 
38
                public MappedByteBuffer Map ()
 
39
                {
 
40
                        throw new NotImplementedException ();
 
41
                }
 
42
 
 
43
                public MappedByteBuffer Map (MapMode mode, long pos, int size)
 
44
                {
 
45
                        throw new NotImplementedException ();
 
46
                }
 
47
 
 
48
                public int Read (byte[] buffer)
 
49
                {
 
50
                        return s.Read (buffer, 0, buffer.Length);
 
51
                }
 
52
 
 
53
                public int Read (ByteBuffer buffer)
 
54
                {
 
55
                        int offset = buffer.Position () + buffer.ArrayOffset ();
 
56
                        int num2 = s.Read (buffer.Array (), offset, (buffer.Limit () + buffer.ArrayOffset ()) - offset);
 
57
                        buffer.Position (buffer.Position () + num2);
 
58
                        return num2;
 
59
                }
 
60
 
 
61
                public long Size ()
 
62
                {
 
63
                        return s.Length;
 
64
                }
 
65
 
 
66
                public FileLock TryLock ()
 
67
                {
 
68
                        try {
 
69
                                s.Lock (0, int.MaxValue);
 
70
                                return new FileLock (s);
 
71
                        } catch (IOException) {
 
72
                                return null;
 
73
                        }
 
74
                }
 
75
 
 
76
                public int Write (byte[] buffer)
 
77
                {
 
78
                        s.Write (buffer, 0, buffer.Length);
 
79
                        return buffer.Length;
 
80
                }
 
81
 
 
82
                public int Write (ByteBuffer buffer)
 
83
                {
 
84
                        int offset = buffer.Position () + buffer.ArrayOffset ();
 
85
                        int count = (buffer.Limit () + buffer.ArrayOffset ()) - offset;
 
86
                        s.Write (buffer.Array (), offset, count);
 
87
                        buffer.Position (buffer.Position () + count);
 
88
                        return count;
 
89
                }
 
90
                
 
91
                public long TransferFrom (FileChannel src, long pos, long count)
 
92
                {
 
93
                        if (buffer == null)
 
94
                                buffer = new byte [8092];
 
95
                        int nr = src.s.Read (buffer, 0, (int) Math.Min (buffer.Length, count));
 
96
                        long curPos = s.Position;
 
97
                        s.Position = pos;
 
98
                        s.Write (buffer, 0, nr);
 
99
                        s.Position = curPos;
 
100
                        return nr;
 
101
                }
 
102
 
 
103
                public enum MapMode
 
104
                {
 
105
                        READ_ONLY
 
106
                }
 
107
        }
 
108
}