17
|
1 package myVncClient;
|
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
|
4
|
26 static final int defaultBufSize = 16384;
|
0
|
27
|
4
|
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 }
|
0
|
34
|
4
|
35 public ZlibInStream() {
|
|
36 this(defaultBufSize);
|
|
37 }
|
0
|
38
|
4
|
39 public void setUnderlying(InStream is, int bytesIn_) {
|
|
40 underlying = is;
|
|
41 bytesIn = bytesIn_;
|
|
42 ptr = end = 0;
|
|
43 }
|
0
|
44
|
4
|
45 public void reset() throws Exception {
|
|
46 ptr = end = 0;
|
|
47 if (underlying == null)
|
|
48 return;
|
0
|
49
|
4
|
50 while (bytesIn > 0) {
|
|
51 decompress();
|
|
52 end = 0; // throw away any data
|
|
53 }
|
|
54 underlying = null;
|
|
55 }
|
0
|
56
|
4
|
57 public int pos() {
|
|
58 return ptrOffset + ptr;
|
|
59 }
|
0
|
60
|
4
|
61 protected int overrun(int itemSize, int nItems) throws Exception {
|
|
62 if (itemSize > bufSize)
|
|
63 throw new Exception("ZlibInStream overrun: max itemSize exceeded");
|
|
64 if (underlying == null)
|
|
65 throw new Exception("ZlibInStream overrun: no underlying stream");
|
0
|
66
|
4
|
67 if (end - ptr != 0)
|
|
68 System.arraycopy(b, ptr, b, 0, end - ptr);
|
0
|
69
|
4
|
70 ptrOffset += ptr;
|
|
71 end -= ptr;
|
|
72 ptr = 0;
|
0
|
73
|
4
|
74 while (end < itemSize) {
|
|
75 decompress();
|
|
76 }
|
0
|
77
|
4
|
78 if (itemSize * nItems > end)
|
|
79 nItems = end / itemSize;
|
0
|
80
|
4
|
81 return nItems;
|
|
82 }
|
|
83
|
|
84 // decompress() calls the decompressor once. Note that this won't
|
|
85 // necessarily generate any output data - it may just consume some input
|
|
86 // data. Returns false if wait is false and we would block on the underlying
|
|
87 // stream.
|
0
|
88
|
4
|
89 private void decompress() throws Exception {
|
|
90 try {
|
|
91 underlying.check(1);
|
|
92 int avail_in = underlying.getend() - underlying.getptr();
|
|
93 if (avail_in > bytesIn)
|
|
94 avail_in = bytesIn;
|
|
95
|
|
96 if (inflater.needsInput()) {
|
|
97 inflater.setInput(underlying.getbuf(), underlying.getptr(),
|
|
98 avail_in);
|
|
99 }
|
|
100 int n = inflater.inflate(b, end, bufSize - end);
|
|
101 end += n;
|
|
102 if (inflater.needsInput()) {
|
|
103 bytesIn -= avail_in;
|
|
104 underlying.setptr(underlying.getptr() + avail_in);
|
|
105 }
|
|
106 } catch (java.util.zip.DataFormatException e) {
|
|
107 throw new Exception("ZlibInStream: inflate failed");
|
|
108 }
|
|
109 }
|
0
|
110
|
4
|
111 private InStream underlying;
|
|
112 private int bufSize;
|
5
|
113 private int ptrOffset;
|
4
|
114 private java.util.zip.Inflater inflater;
|
|
115 private int bytesIn;
|
0
|
116 }
|