annotate src/ZlibInStream.java @ 2:dc1d008d987b

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