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