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