annotate src/myVncProxy/ZlibInStream.java @ 129:0571d955da35

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