您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ZlibInStream.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. //
  19. // A ZlibInStream reads from a zlib.io.InputStream
  20. //
  21. public class ZlibInStream extends InStream {
  22. static final int defaultBufSize = 16384;
  23. public ZlibInStream(int bufSize_) {
  24. bufSize = bufSize_;
  25. b = new byte[bufSize];
  26. ptr = end = ptrOffset = 0;
  27. inflater = new java.util.zip.Inflater();
  28. }
  29. public ZlibInStream() { this(defaultBufSize); }
  30. public void setUnderlying(InStream is, int bytesIn_) {
  31. underlying = is;
  32. bytesIn = bytesIn_;
  33. ptr = end = 0;
  34. }
  35. public void reset() throws Exception {
  36. ptr = end = 0;
  37. if (underlying == null) return;
  38. while (bytesIn > 0) {
  39. decompress();
  40. end = 0; // throw away any data
  41. }
  42. underlying = null;
  43. }
  44. public int pos() { return ptrOffset + ptr; }
  45. protected int overrun(int itemSize, int nItems) throws Exception {
  46. if (itemSize > bufSize)
  47. throw new Exception("ZlibInStream overrun: max itemSize exceeded");
  48. if (underlying == null)
  49. throw new Exception("ZlibInStream overrun: no underlying stream");
  50. if (end - ptr != 0)
  51. System.arraycopy(b, ptr, b, 0, end - ptr);
  52. ptrOffset += ptr;
  53. end -= ptr;
  54. ptr = 0;
  55. while (end < itemSize) {
  56. decompress();
  57. }
  58. if (itemSize * nItems > end)
  59. nItems = end / itemSize;
  60. return nItems;
  61. }
  62. // decompress() calls the decompressor once. Note that this won't
  63. // necessarily generate any output data - it may just consume some input
  64. // data. Returns false if wait is false and we would block on the underlying
  65. // stream.
  66. private void decompress() throws Exception {
  67. try {
  68. underlying.check(1);
  69. int avail_in = underlying.getend() - underlying.getptr();
  70. if (avail_in > bytesIn)
  71. avail_in = bytesIn;
  72. if (inflater.needsInput()) {
  73. inflater.setInput(underlying.getbuf(), underlying.getptr(), avail_in);
  74. }
  75. int n = inflater.inflate(b, end, bufSize - end);
  76. end += n;
  77. if (inflater.needsInput()) {
  78. bytesIn -= avail_in;
  79. underlying.setptr(underlying.getptr() + avail_in);
  80. }
  81. } catch (java.util.zip.DataFormatException e) {
  82. throw new Exception("ZlibInStream: inflate failed");
  83. }
  84. }
  85. private InStream underlying;
  86. private int bufSize;
  87. private int ptrOffset;
  88. private java.util.zip.Inflater inflater;
  89. private int bytesIn;
  90. }