2
0

DefalteStream.jvm.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //
  2. // DeflateStream.cs
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. //
  7. // (c) 2008 Mainsoft corp. <http://www.mainsoft.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. //#if NET_2_0
  29. using System;
  30. using System.IO;
  31. using System.Runtime.InteropServices;
  32. using System.Runtime.Remoting.Messaging;
  33. using java.io;
  34. using java.util.zip;
  35. using vmw.common;
  36. namespace System.IO.Compression
  37. {
  38. public class DeflateStream : Stream
  39. {
  40. readonly Stream _baseStream;
  41. readonly InflaterInputStream _reader;
  42. readonly DeflaterOutputStream _writer;
  43. readonly bool _leaveOpen;
  44. bool _open;
  45. delegate int ReadMethod (byte [] array, int offset, int count);
  46. delegate void WriteMethod (byte [] array, int offset, int count);
  47. internal DeflateStream (Stream compressedStream, CompressionMode mode, bool leaveOpen, bool gzip) {
  48. if (compressedStream == null)
  49. throw new ArgumentNullException ("compressedStream");
  50. switch (mode) {
  51. case CompressionMode.Compress:
  52. if (!compressedStream.CanWrite)
  53. throw new ArgumentException ("The base stream is not writeable.");
  54. OutputStream outStream = new OutputStreamImpl(compressedStream);
  55. _writer = gzip ? new GZIPOutputStream (outStream) : new DeflaterOutputStream (outStream, new Deflater (Deflater.DEFAULT_COMPRESSION, true));
  56. break;
  57. case CompressionMode.Decompress:
  58. if (!compressedStream.CanRead)
  59. throw new ArgumentException ("The base stream is not readable.");
  60. InputStream inStream = new InputStreamImpl (compressedStream);
  61. _reader = gzip ? new GZIPInputStream (inStream) : new InflaterInputStream (inStream, new Inflater (true));
  62. break;
  63. default:
  64. throw new ArgumentException ("mode");
  65. }
  66. _baseStream = compressedStream;
  67. _leaveOpen = leaveOpen;
  68. _open = true;
  69. }
  70. public DeflateStream (Stream compressedStream, CompressionMode mode)
  71. :
  72. this (compressedStream, mode, false, false) { }
  73. public DeflateStream (Stream compressedStream, CompressionMode mode, bool leaveOpen)
  74. :
  75. this (compressedStream, mode, leaveOpen, false) { }
  76. protected override void Dispose (bool disposing) {
  77. if (!_open) {
  78. base.Dispose (disposing);
  79. return;
  80. }
  81. try {
  82. FlushInternal (true);
  83. base.Dispose (disposing);
  84. }
  85. finally {
  86. _open = false;
  87. if (!_leaveOpen)
  88. _baseStream.Close ();
  89. }
  90. }
  91. private int ReadInternal (byte [] array, int offset, int count) {
  92. int r = _reader.read (TypeUtils.ToSByteArray (array), offset, count);
  93. return r < 0 ? 0 : r;
  94. }
  95. public override int Read (byte [] dest, int dest_offset, int count) {
  96. if (!_open)
  97. throw new ObjectDisposedException ("DeflateStream");
  98. if (dest == null)
  99. throw new ArgumentNullException ("Destination array is null.");
  100. if (!CanRead)
  101. throw new InvalidOperationException ("Stream does not support reading.");
  102. int len = dest.Length;
  103. if (dest_offset < 0 || count < 0)
  104. throw new ArgumentException ("Dest or count is negative.");
  105. if (dest_offset > len)
  106. throw new ArgumentException ("destination offset is beyond array size");
  107. if ((dest_offset + count) > len)
  108. throw new ArgumentException ("Reading would overrun buffer");
  109. return ReadInternal (dest, dest_offset, count);
  110. }
  111. private void WriteInternal (byte [] array, int offset, int count) {
  112. _writer.write (TypeUtils.ToSByteArray (array), offset, count);
  113. }
  114. public override void Write (byte [] src, int src_offset, int count) {
  115. if (!_open)
  116. throw new ObjectDisposedException ("DeflateStream");
  117. if (src == null)
  118. throw new ArgumentNullException ("src");
  119. if (src_offset < 0)
  120. throw new ArgumentOutOfRangeException ("src_offset");
  121. if (count < 0)
  122. throw new ArgumentOutOfRangeException ("count");
  123. if (!CanWrite)
  124. throw new NotSupportedException ("Stream does not support writing");
  125. WriteInternal (src, src_offset, count);
  126. }
  127. private void FlushInternal (bool finish) {
  128. if (!_open)
  129. throw new ObjectDisposedException ("DeflateStream");
  130. if (_writer != null) {
  131. _writer.flush ();
  132. if (finish)
  133. _writer.finish ();
  134. }
  135. }
  136. public override void Flush () {
  137. FlushInternal (false);
  138. }
  139. public override long Seek (long offset, SeekOrigin origin) {
  140. throw new System.NotSupportedException ();
  141. }
  142. public override void SetLength (long value) {
  143. throw new System.NotSupportedException ();
  144. }
  145. public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
  146. AsyncCallback cback, object state) {
  147. if (!_open)
  148. throw new ObjectDisposedException ("DeflateStream");
  149. if (!CanRead)
  150. throw new NotSupportedException ("This stream does not support reading");
  151. if (buffer == null)
  152. throw new ArgumentNullException ("buffer");
  153. if (count < 0)
  154. throw new ArgumentOutOfRangeException ("count", "Must be >= 0");
  155. if (offset < 0)
  156. throw new ArgumentOutOfRangeException ("offset", "Must be >= 0");
  157. if (count + offset > buffer.Length)
  158. throw new ArgumentException ("Buffer too small. count/offset wrong.");
  159. ReadMethod r = new ReadMethod (ReadInternal);
  160. return r.BeginInvoke (buffer, offset, count, cback, state);
  161. }
  162. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
  163. AsyncCallback cback, object state) {
  164. if (!_open)
  165. throw new ObjectDisposedException ("DeflateStream");
  166. if (!CanWrite)
  167. throw new InvalidOperationException ("This stream does not support writing");
  168. if (buffer == null)
  169. throw new ArgumentNullException ("buffer");
  170. if (count < 0)
  171. throw new ArgumentOutOfRangeException ("count", "Must be >= 0");
  172. if (offset < 0)
  173. throw new ArgumentOutOfRangeException ("offset", "Must be >= 0");
  174. if (count + offset > buffer.Length)
  175. throw new ArgumentException ("Buffer too small. count/offset wrong.");
  176. WriteMethod w = new WriteMethod (WriteInternal);
  177. return w.BeginInvoke (buffer, offset, count, cback, state);
  178. }
  179. public override int EndRead (IAsyncResult async_result) {
  180. if (async_result == null)
  181. throw new ArgumentNullException ("async_result");
  182. AsyncResult ares = async_result as AsyncResult;
  183. if (ares == null)
  184. throw new ArgumentException ("Invalid IAsyncResult", "async_result");
  185. ReadMethod r = ares.AsyncDelegate as ReadMethod;
  186. if (r == null)
  187. throw new ArgumentException ("Invalid IAsyncResult", "async_result");
  188. return r.EndInvoke (async_result);
  189. }
  190. public override void EndWrite (IAsyncResult async_result) {
  191. if (async_result == null)
  192. throw new ArgumentNullException ("async_result");
  193. AsyncResult ares = async_result as AsyncResult;
  194. if (ares == null)
  195. throw new ArgumentException ("Invalid IAsyncResult", "async_result");
  196. WriteMethod w = ares.AsyncDelegate as WriteMethod;
  197. if (w == null)
  198. throw new ArgumentException ("Invalid IAsyncResult", "async_result");
  199. w.EndInvoke (async_result);
  200. }
  201. public Stream BaseStream {
  202. get {
  203. return _baseStream;
  204. }
  205. }
  206. public override bool CanRead {
  207. get {
  208. return _open && _reader != null;
  209. }
  210. }
  211. public override bool CanSeek {
  212. get {
  213. return false;
  214. }
  215. }
  216. public override bool CanWrite {
  217. get {
  218. return _open && _writer != null;
  219. }
  220. }
  221. public override long Length {
  222. get {
  223. throw new NotSupportedException ();
  224. }
  225. }
  226. public override long Position {
  227. get {
  228. throw new NotSupportedException ();
  229. }
  230. set {
  231. throw new NotSupportedException ();
  232. }
  233. }
  234. #region InputStreamImpl
  235. sealed class InputStreamImpl : InputStream
  236. {
  237. readonly Stream _stream;
  238. public InputStreamImpl (Stream stream) {
  239. _stream = stream;
  240. }
  241. public override void close () {
  242. BaseStream.Close ();
  243. }
  244. public override int read () {
  245. return BaseStream.ReadByte ();
  246. }
  247. public override int read (sbyte [] b, int off, int len) {
  248. int r = BaseStream.Read ((byte []) TypeUtils.ToByteArray (b), off, len);
  249. return r == 0 ? -1 : r;
  250. }
  251. public override long skip (long n) {
  252. return BaseStream.Seek (n, SeekOrigin.Current);
  253. }
  254. public override bool Equals (object obj) {
  255. return (obj is InputStreamImpl) &&
  256. BaseStream.Equals (((InputStreamImpl) obj).BaseStream);
  257. }
  258. public override int GetHashCode () {
  259. return _stream.GetHashCode ();
  260. }
  261. public Stream BaseStream {
  262. get { return _stream; }
  263. }
  264. }
  265. #endregion
  266. #region OutputStreamImpl
  267. sealed class OutputStreamImpl : OutputStream
  268. {
  269. readonly Stream _stream;
  270. public OutputStreamImpl (Stream stream) {
  271. _stream = stream;
  272. }
  273. public override void close () {
  274. BaseStream.Close ();
  275. }
  276. public override void flush () {
  277. BaseStream.Flush ();
  278. }
  279. public override void write (int b) {
  280. BaseStream.WriteByte ((byte) (b & 0xFF));
  281. }
  282. public override void write (sbyte [] b, int off, int len) {
  283. BaseStream.Write ((byte []) TypeUtils.ToByteArray (b), off, len);
  284. }
  285. public override bool Equals (object obj) {
  286. return (obj is OutputStreamImpl) &&
  287. BaseStream.Equals (((OutputStreamImpl) obj).BaseStream);
  288. }
  289. public override int GetHashCode () {
  290. return _stream.GetHashCode ();
  291. }
  292. public Stream BaseStream {
  293. get { return _stream; }
  294. }
  295. }
  296. #endregion
  297. }
  298. }
  299. //#endif