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