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

« back to all changes in this revision

Viewing changes to contrib/Sharpen/Sharpen/RandomAccessFile.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 RandomAccessFile
 
7
        {
 
8
                private FileStream stream;
 
9
 
 
10
                public RandomAccessFile (FilePath file, string mode) : this(file.GetPath (), mode)
 
11
                {
 
12
                }
 
13
 
 
14
                public RandomAccessFile (string file, string mode)
 
15
                {
 
16
                        if (mode.IndexOf ('w') != -1)
 
17
                                stream = new FileStream (file, System.IO.FileMode.OpenOrCreate, FileAccess.ReadWrite);
 
18
                        else
 
19
                                stream = new FileStream (file, System.IO.FileMode.Open, FileAccess.Read);
 
20
                }
 
21
 
 
22
                public void Close ()
 
23
                {
 
24
                        stream.Close ();
 
25
                }
 
26
 
 
27
                public FileChannel GetChannel ()
 
28
                {
 
29
                        return new FileChannel (this.stream);
 
30
                }
 
31
 
 
32
                public long GetFilePointer ()
 
33
                {
 
34
                        return stream.Position;
 
35
                }
 
36
 
 
37
                public long Length ()
 
38
                {
 
39
                        return stream.Length;
 
40
                }
 
41
 
 
42
                public int Read (byte[] buffer)
 
43
                {
 
44
                        int r = stream.Read (buffer, 0, buffer.Length);
 
45
                        return r > 0 ? r : -1;
 
46
                }
 
47
 
 
48
                public int Read (byte[] buffer, int start, int size)
 
49
                {
 
50
                        return stream.Read (buffer, start, size);
 
51
                }
 
52
 
 
53
                public void ReadFully (byte[] buffer, int start, int size)
 
54
                {
 
55
                        while (size > 0) {
 
56
                                int num = stream.Read (buffer, start, size);
 
57
                                if (num == 0) {
 
58
                                        throw new EOFException ();
 
59
                                }
 
60
                                size -= num;
 
61
                                start += num;
 
62
                        }
 
63
                }
 
64
 
 
65
                public void Seek (long pos)
 
66
                {
 
67
                        stream.Position = pos;
 
68
                }
 
69
 
 
70
                public void SetLength (long len)
 
71
                {
 
72
                        stream.SetLength (len);
 
73
                }
 
74
 
 
75
                public void Write (byte[] buffer)
 
76
                {
 
77
                        stream.Write (buffer, 0, buffer.Length);
 
78
                }
 
79
 
 
80
                public void Write (byte[] buffer, int start, int size)
 
81
                {
 
82
                        stream.Write (buffer, start, size);
 
83
                }
 
84
        }
 
85
}