ZDeflaterOutputStream.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Zlib {
  6. /// <summary>
  7. /// Summary description for DeflaterOutputStream.
  8. /// </summary>
  9. [Obsolete("Use 'ZOutputStream' instead")]
  10. public class ZDeflaterOutputStream : Stream {
  11. protected ZStream z=new ZStream();
  12. protected int flushLevel=JZlib.Z_NO_FLUSH;
  13. private const int BUFSIZE = 4192;
  14. protected byte[] buf=new byte[BUFSIZE];
  15. private byte[] buf1=new byte[1];
  16. protected Stream outp;
  17. public ZDeflaterOutputStream(Stream outp) : this(outp, 6, false) {
  18. }
  19. public ZDeflaterOutputStream(Stream outp, int level) : this(outp, level, false) {
  20. }
  21. public ZDeflaterOutputStream(Stream outp, int level, bool nowrap) {
  22. this.outp=outp;
  23. z.deflateInit(level, nowrap);
  24. }
  25. public override bool CanRead {
  26. get {
  27. // TODO: Add DeflaterOutputStream.CanRead getter implementation
  28. return false;
  29. }
  30. }
  31. public override bool CanSeek {
  32. get {
  33. // TODO: Add DeflaterOutputStream.CanSeek getter implementation
  34. return false;
  35. }
  36. }
  37. public override bool CanWrite {
  38. get {
  39. // TODO: Add DeflaterOutputStream.CanWrite getter implementation
  40. return true;
  41. }
  42. }
  43. public override long Length {
  44. get {
  45. // TODO: Add DeflaterOutputStream.Length getter implementation
  46. return 0;
  47. }
  48. }
  49. public override long Position {
  50. get {
  51. // TODO: Add DeflaterOutputStream.Position getter implementation
  52. return 0;
  53. }
  54. set {
  55. // TODO: Add DeflaterOutputStream.Position setter implementation
  56. }
  57. }
  58. public override void Write(byte[] b, int off, int len) {
  59. if(len==0)
  60. return;
  61. int err;
  62. z.next_in=b;
  63. z.next_in_index=off;
  64. z.avail_in=len;
  65. do{
  66. z.next_out=buf;
  67. z.next_out_index=0;
  68. z.avail_out=BUFSIZE;
  69. err=z.deflate(flushLevel);
  70. if(err!=JZlib.Z_OK)
  71. throw new IOException("deflating: "+z.msg);
  72. if (z.avail_out < BUFSIZE)
  73. {
  74. outp.Write(buf, 0, BUFSIZE-z.avail_out);
  75. }
  76. }
  77. while(z.avail_in>0 || z.avail_out==0);
  78. }
  79. public override long Seek(long offset, SeekOrigin origin) {
  80. // TODO: Add DeflaterOutputStream.Seek implementation
  81. return 0;
  82. }
  83. public override void SetLength(long value) {
  84. // TODO: Add DeflaterOutputStream.SetLength implementation
  85. }
  86. public override int Read(byte[] buffer, int offset, int count) {
  87. // TODO: Add DeflaterOutputStream.Read implementation
  88. return 0;
  89. }
  90. public override void Flush() {
  91. outp.Flush();
  92. }
  93. public override void WriteByte(byte b) {
  94. buf1[0]=(byte)b;
  95. Write(buf1, 0, 1);
  96. }
  97. public void Finish() {
  98. int err;
  99. do{
  100. z.next_out=buf;
  101. z.next_out_index=0;
  102. z.avail_out=BUFSIZE;
  103. err=z.deflate(JZlib.Z_FINISH);
  104. if(err!=JZlib.Z_STREAM_END && err != JZlib.Z_OK)
  105. throw new IOException("deflating: "+z.msg);
  106. if(BUFSIZE-z.avail_out>0){
  107. outp.Write(buf, 0, BUFSIZE-z.avail_out);
  108. }
  109. }
  110. while(z.avail_in>0 || z.avail_out==0);
  111. Flush();
  112. }
  113. public void End() {
  114. if(z==null)
  115. return;
  116. z.deflateEnd();
  117. z.free();
  118. z=null;
  119. }
  120. #if PORTABLE || NETFX_CORE
  121. protected override void Dispose(bool disposing)
  122. {
  123. if (disposing)
  124. {
  125. try{
  126. try{Finish();}
  127. catch (IOException) {}
  128. }
  129. finally{
  130. End();
  131. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(outp);
  132. outp=null;
  133. }
  134. }
  135. base.Dispose(disposing);
  136. }
  137. #else
  138. public override void Close() {
  139. try{
  140. try{Finish();}
  141. catch (IOException) {}
  142. }
  143. finally{
  144. End();
  145. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(outp);
  146. outp=null;
  147. }
  148. base.Close();
  149. }
  150. #endif
  151. }
  152. }
  153. #pragma warning restore
  154. #endif