annotate src/InStream.java @ 9:c0ad3ecdf827

create method cliSize(). cliSize() is clisList.size()
author e085711
date Sat, 16 Apr 2011 01:19:18 +0900
parents 30bb7074acb1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
1 /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
2 *
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
3 * This is free software; you can redistribute it and/or modify
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
4 * it under the terms of the GNU General Public License as published by
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
5 * the Free Software Foundation; either version 2 of the License, or
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
6 * (at your option) any later version.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
7 *
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
8 * This software is distributed in the hope that it will be useful,
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
11 * GNU General Public License for more details.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
12 *
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
13 * You should have received a copy of the GNU General Public License
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
14 * along with this software; if not, write to the Free Software
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
16 * USA.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
17 */
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 // rdr::InStream marshalls data from a buffer stored in RDR (RFB Data
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
21 // Representation).
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 abstract public class InStream {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
25
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
26 // check() ensures there is buffer data for at least one item of size
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
27 // itemSize bytes. Returns the number of items in the buffer (up to a
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
28 // maximum of nItems).
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
29
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
30 public final int check(int itemSize, int nItems) throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
31 if (ptr + itemSize * nItems > end) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
32 if (ptr + itemSize > end)
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
33 return overrun(itemSize, nItems);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
34
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
35 nItems = (end - ptr) / itemSize;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
36 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
37 return nItems;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
38 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
39
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
40 public final void check(int itemSize) throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
41 if (ptr + itemSize > end)
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
42 overrun(itemSize, 1);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
43 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
44
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
45 // readU/SN() methods read unsigned and signed N-bit integers.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
46
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
47 public final int readS8() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
48 check(1); return b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
49 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
50
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
51 public final int readS16() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
52 check(2); int b0 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
53 int b1 = b[ptr++] & 0xff; return b0 << 8 | b1;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
54 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
55
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
56 public final int readS32() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
57 check(4); int b0 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
58 int b1 = b[ptr++] & 0xff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
59 int b2 = b[ptr++] & 0xff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
60 int b3 = b[ptr++] & 0xff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
61 return b0 << 24 | b1 << 16 | b2 << 8 | b3;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
62 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
63
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
64 public final int readU8() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
65 return readS8() & 0xff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
66 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
67
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
68 public final int readU16() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
69 return readS16() & 0xffff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
70 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
71
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
72 public final int readU32() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
73 return readS32() & 0xffffffff;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
74 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
75
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
76 // readString() reads a string - a U32 length followed by the data.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
77
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
78 public final String readString() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
79 int len = readU32();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
80 if (len > maxStringLength)
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
81 throw new Exception("InStream max string length exceeded");
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
82
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
83 char[] str = new char[len];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
84 int i = 0;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
85 while (i < len) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
86 int j = i + check(1, len - i);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
87 while (i < j) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
88 str[i++] = (char)b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
89 }
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 return new String(str);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
93 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
94
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
95 // maxStringLength protects against allocating a huge buffer. Set it
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
96 // higher if you need longer strings.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
97
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
98 public static int maxStringLength = 65535;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
99
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
100 public final void skip(int bytes) throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
101 while (bytes > 0) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
102 int n = check(1, bytes);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
103 ptr += n;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
104 bytes -= n;
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 // readBytes() reads an exact number of bytes into an array at an offset.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
109
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
110 public void readBytes(byte[] data, int offset, int length) throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
111 int offsetEnd = offset + length;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
112 while (offset < offsetEnd) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
113 int n = check(1, offsetEnd - offset);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
114 System.arraycopy(b, ptr, data, offset, n);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
115 ptr += n;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
116 offset += n;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
117 }
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 // readOpaqueN() reads a quantity "without byte-swapping". Because java has
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
121 // no byte-ordering, we just use big-endian.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
122
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
123 public final int readOpaque8() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
124 return readU8();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
125 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
126
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
127 public final int readOpaque16() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
128 return readU16();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
129 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
130
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
131 public final int readOpaque32() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
132 return readU32();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
133 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
134
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
135 public final int readOpaque24A() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
136 check(3); int b0 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
137 int b1 = b[ptr++]; int b2 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
138 return b0 << 24 | b1 << 16 | b2 << 8;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
139 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
140
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
141 public final int readOpaque24B() throws Exception {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
142 check(3); int b0 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
143 int b1 = b[ptr++]; int b2 = b[ptr++];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
144 return b0 << 16 | b1 << 8 | b2;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
145 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
146
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
147 // pos() returns the position in the stream.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
148
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
149 abstract public int pos();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
150
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
151 // bytesAvailable() returns true if at least one byte can be read from the
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
152 // stream without blocking. i.e. if false is returned then readU8() would
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
153 // block.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
154
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
155 public boolean bytesAvailable() { return end != ptr; }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
156
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
157 // getbuf(), getptr(), getend() and setptr() are "dirty" methods which allow
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
158 // you to manipulate the buffer directly. This is useful for a stream which
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
159 // is a wrapper around an underlying stream.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
160
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
161 public final byte[] getbuf() { return b; }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
162 public final int getptr() { return ptr; }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
163 public final int getend() { return end; }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
164 public final void setptr(int p) { ptr = p; }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
165
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
166 // overrun() is implemented by a derived class to cope with buffer overrun.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
167 // It ensures there are at least itemSize bytes of buffer data. Returns
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
168 // the number of items in the buffer (up to a maximum of nItems). itemSize
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
169 // is supposed to be "small" (a few bytes).
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
170
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
171 abstract protected int overrun(int itemSize, int nItems) throws Exception;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
172
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
173 protected InStream() {}
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
174 protected byte[] b;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
175 protected int ptr;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
176 protected int end;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
177 }