1 /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // rdr::InStream marshalls data from a buffer stored in RDR (RFB Data
24 abstract public class InStream {
26 // check() ensures there is buffer data for at least one item of size
27 // itemSize bytes. Returns the number of items in the buffer (up to a
28 // maximum of nItems).
30 public final int check(int itemSize, int nItems) throws Exception {
31 if (ptr + itemSize * nItems > end) {
32 if (ptr + itemSize > end)
33 return overrun(itemSize, nItems);
35 nItems = (end - ptr) / itemSize;
40 public final void check(int itemSize) throws Exception {
41 if (ptr + itemSize > end)
45 // readU/SN() methods read unsigned and signed N-bit integers.
47 public final int readS8() throws Exception {
48 check(1); return b[ptr++];
51 public final int readS16() throws Exception {
52 check(2); int b0 = b[ptr++];
53 int b1 = b[ptr++] & 0xff; return b0 << 8 | b1;
56 public final int readS32() throws Exception {
57 check(4); int b0 = b[ptr++];
58 int b1 = b[ptr++] & 0xff;
59 int b2 = b[ptr++] & 0xff;
60 int b3 = b[ptr++] & 0xff;
61 return b0 << 24 | b1 << 16 | b2 << 8 | b3;
64 public final int readU8() throws Exception {
65 return readS8() & 0xff;
68 public final int readU16() throws Exception {
69 return readS16() & 0xffff;
72 public final int readU32() throws Exception {
73 return readS32() & 0xffffffff;
76 // readString() reads a string - a U32 length followed by the data.
78 public final String readString() throws Exception {
80 if (len > maxStringLength)
81 throw new Exception("InStream max string length exceeded");
83 char[] str = new char[len];
86 int j = i + check(1, len - i);
88 str[i++] = (char)b[ptr++];
92 return new String(str);
95 // maxStringLength protects against allocating a huge buffer. Set it
96 // higher if you need longer strings.
98 public static int maxStringLength = 65535;
100 public final void skip(int bytes) throws Exception {
102 int n = check(1, bytes);
108 // readBytes() reads an exact number of bytes into an array at an offset.
110 public void readBytes(byte[] data, int offset, int length) throws Exception {
111 int offsetEnd = offset + length;
112 while (offset < offsetEnd) {
113 int n = check(1, offsetEnd - offset);
114 System.arraycopy(b, ptr, data, offset, n);
120 // readOpaqueN() reads a quantity "without byte-swapping". Because java has
121 // no byte-ordering, we just use big-endian.
123 public final int readOpaque8() throws Exception {
127 public final int readOpaque16() throws Exception {
131 public final int readOpaque32() throws Exception {
135 public final int readOpaque24A() throws Exception {
136 check(3); int b0 = b[ptr++];
137 int b1 = b[ptr++]; int b2 = b[ptr++];
138 return b0 << 24 | b1 << 16 | b2 << 8;
141 public final int readOpaque24B() throws Exception {
142 check(3); int b0 = b[ptr++];
143 int b1 = b[ptr++]; int b2 = b[ptr++];
144 return b0 << 16 | b1 << 8 | b2;
147 // pos() returns the position in the stream.
149 abstract public int pos();
151 // bytesAvailable() returns true if at least one byte can be read from the
152 // stream without blocking. i.e. if false is returned then readU8() would
155 public boolean bytesAvailable() { return end != ptr; }
157 // getbuf(), getptr(), getend() and setptr() are "dirty" methods which allow
158 // you to manipulate the buffer directly. This is useful for a stream which
159 // is a wrapper around an underlying stream.
161 public final byte[] getbuf() { return b; }
162 public final int getptr() { return ptr; }
163 public final int getend() { return end; }
164 public final void setptr(int p) { ptr = p; }
166 // overrun() is implemented by a derived class to cope with buffer overrun.
167 // It ensures there are at least itemSize bytes of buffer data. Returns
168 // the number of items in the buffer (up to a maximum of nItems). itemSize
169 // is supposed to be "small" (a few bytes).
171 abstract protected int overrun(int itemSize, int nItems) throws Exception;
173 protected InStream() {}