KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > packets > TypesReader


1
2 package ch.ethz.ssh2.packets;
3
4 import java.io.IOException JavaDoc;
5 import java.math.BigInteger JavaDoc;
6
7 import ch.ethz.ssh2.util.Tokenizer;
8
9 /**
10  * TypesReader.
11  *
12  * @author Christian Plattner, plattner@inf.ethz.ch
13  * @version $Id: TypesReader.java,v 1.6 2006/08/31 20:04:29 cplattne Exp $
14  */

15 public class TypesReader
16 {
17     byte[] arr;
18     int pos = 0;
19     int max = 0;
20
21     public TypesReader(byte[] arr)
22     {
23         this.arr = arr;
24         pos = 0;
25         max = arr.length;
26     }
27
28     public TypesReader(byte[] arr, int off)
29     {
30         this.arr = arr;
31         this.pos = off;
32         this.max = arr.length;
33
34         if ((pos < 0) || (pos > arr.length))
35             throw new IllegalArgumentException JavaDoc("Illegal offset.");
36     }
37
38     public TypesReader(byte[] arr, int off, int len)
39     {
40         this.arr = arr;
41         this.pos = off;
42         this.max = off + len;
43
44         if ((pos < 0) || (pos > arr.length))
45             throw new IllegalArgumentException JavaDoc("Illegal offset.");
46
47         if ((max < 0) || (max > arr.length))
48             throw new IllegalArgumentException JavaDoc("Illegal length.");
49     }
50
51     public int readByte() throws IOException JavaDoc
52     {
53         if (pos >= max)
54             throw new IOException JavaDoc("Packet too short.");
55
56         return (arr[pos++] & 0xff);
57     }
58
59     public byte[] readBytes(int len) throws IOException JavaDoc
60     {
61         if ((pos + len) > max)
62             throw new IOException JavaDoc("Packet too short.");
63
64         byte[] res = new byte[len];
65
66         System.arraycopy(arr, pos, res, 0, len);
67         pos += len;
68
69         return res;
70     }
71
72     public void readBytes(byte[] dst, int off, int len) throws IOException JavaDoc
73     {
74         if ((pos + len) > max)
75             throw new IOException JavaDoc("Packet too short.");
76
77         System.arraycopy(arr, pos, dst, off, len);
78         pos += len;
79     }
80
81     public boolean readBoolean() throws IOException JavaDoc
82     {
83         if (pos >= max)
84             throw new IOException JavaDoc("Packet too short.");
85
86         return (arr[pos++] != 0);
87     }
88
89     public int readUINT32() throws IOException JavaDoc
90     {
91         if ((pos + 4) > max)
92             throw new IOException JavaDoc("Packet too short.");
93
94         return ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8)
95                 | (arr[pos++] & 0xff);
96     }
97
98     public long readUINT64() throws IOException JavaDoc
99     {
100         if ((pos + 8) > max)
101             throw new IOException JavaDoc("Packet too short.");
102
103         long high = ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8)
104                 | (arr[pos++] & 0xff); /* sign extension may take place - will be shifted away =) */
105
106         long low = ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8)
107                 | (arr[pos++] & 0xff); /* sign extension may take place - handle below */
108
109         return (high << 32) | (low & 0xffffffffl); /* see Java language spec (15.22.1, 5.6.2) */
110     }
111
112     public BigInteger JavaDoc readMPINT() throws IOException JavaDoc
113     {
114         BigInteger JavaDoc b;
115
116         byte raw[] = readByteString();
117
118         if (raw.length == 0)
119             b = BigInteger.ZERO;
120         else
121             b = new BigInteger JavaDoc(raw);
122
123         return b;
124     }
125
126     public byte[] readByteString() throws IOException JavaDoc
127     {
128         int len = readUINT32();
129
130         if ((len + pos) > max)
131             throw new IOException JavaDoc("Malformed SSH byte string.");
132
133         byte[] res = new byte[len];
134         System.arraycopy(arr, pos, res, 0, len);
135         pos += len;
136         return res;
137     }
138
139     public String JavaDoc readString(String JavaDoc charsetName) throws IOException JavaDoc
140     {
141         int len = readUINT32();
142
143         if ((len + pos) > max)
144             throw new IOException JavaDoc("Malformed SSH string.");
145
146         String JavaDoc res = (charsetName == null) ? new String JavaDoc(arr, pos, len) : new String JavaDoc(arr, pos, len, charsetName);
147         pos += len;
148
149         return res;
150     }
151
152     public String JavaDoc readString() throws IOException JavaDoc
153     {
154         int len = readUINT32();
155
156         if ((len + pos) > max)
157             throw new IOException JavaDoc("Malformed SSH string.");
158
159         String JavaDoc res = new String JavaDoc(arr, pos, len);
160         pos += len;
161
162         return res;
163     }
164
165     public String JavaDoc[] readNameList() throws IOException JavaDoc
166     {
167         return Tokenizer.parseTokens(readString(), ',');
168     }
169
170     public int remain()
171     {
172         return max - pos;
173     }
174
175 }
176
Popular Tags