annotate src/myVncProxy/InStream.java @ 191:b2f0cd0cff6c default tip

Added tag Version-1.0 for changeset 79046b4e5990
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Tue, 29 Nov 2011 15:52:44 +0900
parents 87b29d6039a6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
87b29d6039a6 add package myVncProxy
e085711
parents: 0
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 // rdr::InStream marshalls data from a buffer stored in RDR (RFB Data
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
22 // Representation).
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
23 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
24
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
25 abstract public class InStream {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
26
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
27 // check() ensures there is buffer data for at least one item of size
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
28 // itemSize bytes. Returns the number of items in the buffer (up to a
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
29 // maximum of nItems).
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
30
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
31 public final int check(int itemSize, int nItems) throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
32 if (ptr + itemSize * nItems > end) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
33 if (ptr + itemSize > end)
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
34 return overrun(itemSize, nItems);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
35
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
36 nItems = (end - ptr) / itemSize;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
37 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
38 return nItems;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
39 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
40
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
41 public final void check(int itemSize) throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
42 if (ptr + itemSize > end)
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
43 overrun(itemSize, 1);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
44 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
45
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
46 // readU/SN() methods read unsigned and signed N-bit integers.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
47
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
48 public final int readS8() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
49 check(1); return b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
50 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
51
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
52 public final int readS16() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
53 check(2); int b0 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
54 int b1 = b[ptr++] & 0xff; return b0 << 8 | b1;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
55 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
56
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
57 public final int readS32() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
58 check(4); int b0 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
59 int b1 = b[ptr++] & 0xff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
60 int b2 = b[ptr++] & 0xff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
61 int b3 = b[ptr++] & 0xff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
62 return b0 << 24 | b1 << 16 | b2 << 8 | b3;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
63 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
64
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
65 public final int readU8() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
66 return readS8() & 0xff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
67 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
68
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
69 public final int readU16() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
70 return readS16() & 0xffff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
71 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
72
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
73 public final int readU32() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
74 return readS32() & 0xffffffff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
75 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
76
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
77 // readString() reads a string - a U32 length followed by the data.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
78
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
79 public final String readString() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
80 int len = readU32();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
81 if (len > maxStringLength)
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
82 throw new Exception("InStream max string length exceeded");
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
83
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
84 char[] str = new char[len];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
85 int i = 0;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
86 while (i < len) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
87 int j = i + check(1, len - i);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
88 while (i < j) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
89 str[i++] = (char)b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
90 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
91 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
92
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
93 return new String(str);
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 // maxStringLength protects against allocating a huge buffer. Set it
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
97 // higher if you need longer strings.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
98
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
99 public static int maxStringLength = 65535;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
100
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
101 public final void skip(int bytes) throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
102 while (bytes > 0) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
103 int n = check(1, bytes);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
104 ptr += n;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
105 bytes -= n;
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
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
109 // readBytes() reads an exact number of bytes into an array at an offset.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
110
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
111 public void readBytes(byte[] data, int offset, int length) throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
112 int offsetEnd = offset + length;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
113 while (offset < offsetEnd) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
114 int n = check(1, offsetEnd - offset);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
115 System.arraycopy(b, ptr, data, offset, n);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
116 ptr += n;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
117 offset += n;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
118 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
119 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
120
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
121 // readOpaqueN() reads a quantity "without byte-swapping". Because java has
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
122 // no byte-ordering, we just use big-endian.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
123
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
124 public final int readOpaque8() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
125 return readU8();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
126 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
127
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
128 public final int readOpaque16() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
129 return readU16();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
130 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
131
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
132 public final int readOpaque32() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
133 return readU32();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
134 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
135
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
136 public final int readOpaque24A() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
137 check(3); int b0 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
138 int b1 = b[ptr++]; int b2 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
139 return b0 << 24 | b1 << 16 | b2 << 8;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
140 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
141
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
142 public final int readOpaque24B() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
143 check(3); int b0 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
144 int b1 = b[ptr++]; int b2 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
145 return b0 << 16 | b1 << 8 | b2;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
146 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
147
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
148 // pos() returns the position in the stream.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
149
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
150 abstract public int pos();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
151
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
152 // bytesAvailable() returns true if at least one byte can be read from the
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
153 // stream without blocking. i.e. if false is returned then readU8() would
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
154 // block.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
155
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
156 public boolean bytesAvailable() { return end != ptr; }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
157
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
158 // getbuf(), getptr(), getend() and setptr() are "dirty" methods which allow
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
159 // you to manipulate the buffer directly. This is useful for a stream which
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
160 // is a wrapper around an underlying stream.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
161
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
162 public final byte[] getbuf() { return b; }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
163 public final int getptr() { return ptr; }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
164 public final int getend() { return end; }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
165 public final void setptr(int p) { ptr = p; }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
166
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
167 // overrun() is implemented by a derived class to cope with buffer overrun.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
168 // It ensures there are at least itemSize bytes of buffer data. Returns
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
169 // the number of items in the buffer (up to a maximum of nItems). itemSize
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
170 // is supposed to be "small" (a few bytes).
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
171
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
172 abstract protected int overrun(int itemSize, int nItems) throws Exception;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
173
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
174 protected InStream() {}
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
175 protected byte[] b;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
176 protected int ptr;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
177 protected int end;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
178 }