annotate src/InStream.java @ 0:e04119c40b9b

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