ZInflaterInputStream.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 'ZInputStream' instead")]
  10. public class ZInflaterInputStream : 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 inp=null;
  17. private bool nomoreinput=false;
  18. public ZInflaterInputStream(Stream inp) : this(inp, false) {
  19. }
  20. public ZInflaterInputStream(Stream inp, bool nowrap) {
  21. this.inp=inp;
  22. z.inflateInit(nowrap);
  23. z.next_in=buf;
  24. z.next_in_index=0;
  25. z.avail_in=0;
  26. }
  27. public override bool CanRead {
  28. get {
  29. // TODO: Add DeflaterOutputStream.CanRead getter implementation
  30. return true;
  31. }
  32. }
  33. public override bool CanSeek {
  34. get {
  35. // TODO: Add DeflaterOutputStream.CanSeek getter implementation
  36. return false;
  37. }
  38. }
  39. public override bool CanWrite {
  40. get {
  41. // TODO: Add DeflaterOutputStream.CanWrite getter implementation
  42. return false;
  43. }
  44. }
  45. public override long Length {
  46. get {
  47. // TODO: Add DeflaterOutputStream.Length getter implementation
  48. return 0;
  49. }
  50. }
  51. public override long Position {
  52. get {
  53. // TODO: Add DeflaterOutputStream.Position getter implementation
  54. return 0;
  55. }
  56. set {
  57. // TODO: Add DeflaterOutputStream.Position setter implementation
  58. }
  59. }
  60. public override void Write(byte[] b, int off, int len) {
  61. }
  62. public override long Seek(long offset, SeekOrigin origin) {
  63. // TODO: Add DeflaterOutputStream.Seek implementation
  64. return 0;
  65. }
  66. public override void SetLength(long value) {
  67. // TODO: Add DeflaterOutputStream.SetLength implementation
  68. }
  69. public override int Read(byte[] b, int off, int len) {
  70. if(len==0)
  71. return(0);
  72. int err;
  73. z.next_out=b;
  74. z.next_out_index=off;
  75. z.avail_out=len;
  76. do {
  77. if((z.avail_in==0)&&(!nomoreinput)) { // if buffer is empty and more input is avaiable, refill it
  78. z.next_in_index=0;
  79. z.avail_in=inp.Read(buf, 0, BUFSIZE);//(BUFSIZE<z.avail_out ? BUFSIZE : z.avail_out));
  80. if(z.avail_in<=0) {
  81. z.avail_in=0;
  82. nomoreinput=true;
  83. }
  84. }
  85. err=z.inflate(flushLevel);
  86. if(nomoreinput&&(err==JZlib.Z_BUF_ERROR))
  87. return(0);
  88. if(err!=JZlib.Z_OK && err!=JZlib.Z_STREAM_END)
  89. throw new IOException("inflating: "+z.msg);
  90. if((nomoreinput||err==JZlib.Z_STREAM_END)&&(z.avail_out==len))
  91. return(0);
  92. }
  93. while(z.avail_out==len&&err==JZlib.Z_OK);
  94. //System.err.print("("+(len-z.avail_out)+")");
  95. return(len-z.avail_out);
  96. }
  97. public override void Flush() {
  98. inp.Flush();
  99. }
  100. public override void WriteByte(byte b) {
  101. }
  102. #if PORTABLE || NETFX_CORE
  103. protected override void Dispose(bool disposing)
  104. {
  105. if (disposing)
  106. {
  107. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(inp);
  108. }
  109. base.Dispose(disposing);
  110. }
  111. #else
  112. public override void Close()
  113. {
  114. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(inp);
  115. base.Close();
  116. }
  117. #endif
  118. public override int ReadByte() {
  119. if(Read(buf1, 0, 1)<=0)
  120. return -1;
  121. return(buf1[0]&0xFF);
  122. }
  123. }
  124. }
  125. #pragma warning restore
  126. #endif