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