2010-03-05 Rodrigo Kumpera <rkumpera@novell.com>
[mcs.git] / class / WindowsBase / ZipSharp / ZipStream.cs
blob51d15c56c12dfa67691c08f2019edcedbd9c4851
1 // ZipStream.cs created with MonoDevelop
2 // User: alan at 15:16 20/10/2008
3 //
4 // To change standard headers go to Edit->Preferences->Coding->Standard Headers
5 //
7 using System;
8 using System.IO;
9 using System.Runtime.InteropServices;
11 namespace zipsharp
13 class ZipStream : Stream
15 const int ZLIB_FILEFUNC_SEEK_CUR = 1;
16 const int ZLIB_FILEFUNC_SEEK_END = 2;
17 const int ZLIB_FILEFUNC_SEEK_SET = 0;
19 bool canRead;
20 bool canSeek;
21 bool canWrite;
23 public override bool CanRead {
24 get { return canRead; }
27 public override bool CanSeek {
28 get { return canSeek; }
31 public override bool CanWrite {
32 get { return canWrite; }
35 public override bool CanTimeout {
36 get { return false; }
39 private Stream DataStream {
40 get; set;
43 public ZlibFileFuncDef IOFunctions {
44 get; set;
47 public override long Length {
48 get { return DataStream.Length; }
51 bool OwnsStream {
52 get; set;
55 public override long Position {
56 get { return DataStream.Position; }
57 set { DataStream.Position = value; }
60 public ZipStream (Stream dataStream, bool ownsStream)
62 // FIXME: Not necessarily true
63 canRead = true;
64 canSeek = true;
65 canWrite = true;
67 DataStream = dataStream;
68 OwnsStream = ownsStream;
70 ZlibFileFuncDef f = new ZlibFileFuncDef();
72 f.opaque = IntPtr.Zero;
73 f.zclose_file = CloseFile_Native;
74 f.zerror_file = TestError_Native;
75 f.zopen_file = OpenFile_Native;
76 f.zread_file = ReadFile_Native;
77 f.zseek_file = SeekFile_Native;
78 f.ztell_file = TellFile_Native;
79 f.zwrite_file = WriteFile_Native;
81 IOFunctions = f;
84 protected override void Dispose(bool disposing)
86 if (!disposing)
87 return;
89 DataStream.Flush ();
90 if (OwnsStream)
91 DataStream.Dispose ();
94 public override void Flush()
96 DataStream.Flush ();
99 public override int Read(byte[] buffer, int offset, int count)
101 return DataStream.Read (buffer, offset, count);
104 public override long Seek(long offset, SeekOrigin origin)
106 DataStream.Seek (offset, origin);
107 return DataStream.Position;
110 public override void SetLength(long value)
112 DataStream.SetLength (value);
115 public override void Write(byte[] buffer, int offset, int count)
117 DataStream.Write (buffer, offset, count);
118 Flush ();
121 int CloseFile_Native (IntPtr opaque, IntPtr stream)
123 Close ();
124 return 0;
127 IntPtr OpenFile_Native (IntPtr opaque, string filename, int mode)
129 // always success. The stream is opened in managed code
130 return new IntPtr (1);
133 unsafe IntPtr ReadFile_Native (IntPtr opaque, IntPtr stream, IntPtr buffer, IntPtr size)
135 int count = size.ToInt32 ();
136 byte[] b = new byte[count];
137 int read;
139 try {
140 read = Math.Max (0, Read (b, 0, count));
141 byte* ptrBuffer = (byte*) buffer.ToPointer ();
142 for (int i = 0; i < count && i < read; i ++)
143 ptrBuffer[i] = b[i];
144 } catch {
145 read = -1;
148 return new IntPtr (read);
151 IntPtr SeekFile_Native (IntPtr opaque, IntPtr stream, IntPtr offset, int origin)
153 SeekOrigin seek;
154 if (origin == ZipStream.ZLIB_FILEFUNC_SEEK_CUR)
155 seek = SeekOrigin.Current;
156 else if (origin == ZLIB_FILEFUNC_SEEK_END)
157 seek = SeekOrigin.End;
158 else if (origin == ZLIB_FILEFUNC_SEEK_SET)
159 seek = SeekOrigin.Begin;
160 else
161 return new IntPtr (-1);
163 Seek (offset.ToInt64 (), seek);
165 return new IntPtr (0);
168 IntPtr TellFile_Native (IntPtr opaque, IntPtr stream)
170 if (IntPtr.Size == 4)
171 return new IntPtr ((int)Position);
172 else if (IntPtr.Size == 8)
173 return new IntPtr (Position);
174 else
175 return new IntPtr (-1);
178 int TestError_Native (IntPtr opaque, IntPtr stream)
180 // No errors here.
181 return 0;
184 unsafe IntPtr WriteFile_Native (IntPtr opaque, IntPtr stream, IntPtr buffer, /* ulong */ IntPtr size)
186 int count = size.ToInt32 ();
187 byte[] b = new byte[count];
189 byte* ptrBuffer = (byte*) buffer.ToPointer ();
190 for (int i = 0; i < count; i ++)
191 b[i] = ptrBuffer[i];
193 try {
194 Write (b, 0, count);
195 } catch {
199 return new IntPtr (count);