summaryrefslogtreecommitdiff
path: root/NET/worlds/network/ServerInputStream.java
blob: 1e2123bdf2652f9c6a1a1b1cddb12b820c75d03c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*     */ package NET.worlds.network;
/*     */ 
/*     */ import java.io.EOFException;
/*     */ import java.io.FilterInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.UTFDataFormatException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ServerInputStream
/*     */   extends FilterInputStream
/*     */ {
/*     */   private int _packetSize;
/*     */   private int _version;
/*     */   
/*     */   public ServerInputStream(InputStream in)
/*     */   {
/*  70 */     super(in);
/*  71 */     assert (in != null);
/*  72 */     this._packetSize = 0;
/*  73 */     this._version = 24;
/*     */   }
/*     */   
/*     */   public ServerInputStream(InputStream in, int vers) {
/*  77 */     super(in);
/*  78 */     assert (in != null);
/*  79 */     this._packetSize = 0;
/*  80 */     this._version = vers;
/*     */   }
/*     */   
/*     */   public void setVersion(int vers) {
/*  84 */     this._version = vers;
/*     */   }
/*     */   
/*     */   public int getVersion() {
/*  88 */     return this._version;
/*     */   }
/*     */   
/*     */   public void readPacketSize() throws IOException {
/*  92 */     assert (this._packetSize == 0);
/*  93 */     this._packetSize = 1;
/*  94 */     this._packetSize = (readUnsignedByte() - 1);
/*     */     
/*     */ 
/*  97 */     if (((this._packetSize & 0x80) != 0) && (getVersion() > 24)) {
/*  98 */       this._packetSize = 
/*  99 */         ((this._packetSize & 0x80) * 256 + readUnsignedByte() - 1);
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean isEmpty() {
/* 104 */     return this._packetSize == 0;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public int bytesLeft()
/*     */   {
/* 113 */     return this._packetSize;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public final int read(byte[] b)
/*     */     throws IOException
/*     */   {
/* 124 */     assert (this._packetSize >= b.length);
/* 125 */     if (b.length == 0)
/* 126 */       return 0;
/* 127 */     int bytesRead = this.in.read(b, 0, b.length);
/* 128 */     if (bytesRead < 0)
/* 129 */       throw new EOFException();
/* 130 */     this._packetSize -= bytesRead;
/* 131 */     return bytesRead;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public final int read(byte[] b, int off, int len)
/*     */     throws IOException
/*     */   {
/* 142 */     assert (this._packetSize >= len);
/* 143 */     int bytesRead = this.in.read(b, off, len);
/* 144 */     if (bytesRead < 0)
/* 145 */       throw new EOFException();
/* 146 */     this._packetSize -= bytesRead;
/* 147 */     return bytesRead;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public final void readFully(byte[] b)
/*     */     throws IOException
/*     */   {
/* 156 */     assert (this._packetSize >= b.length);
/* 157 */     readFully(b, 0, b.length);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public final void readFully(byte[] b, int off, int len)
/*     */     throws IOException
/*     */   {
/* 167 */     assert (this._packetSize >= len);
/* 168 */     this._packetSize -= len;
/* 169 */     InputStream in = this.in;
/* 170 */     while (len > 0) {
/* 171 */       int bytesRead = in.read(b, off, len);
/* 172 */       if (bytesRead < 0)
/* 173 */         throw new EOFException();
/* 174 */       len -= bytesRead;
/* 175 */       off += bytesRead;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public void skipBytes(int n)
/*     */     throws IOException
/*     */   {
/* 183 */     assert (this._packetSize >= n);
/* 184 */     this._packetSize -= n;
/* 185 */     InputStream in = this.in;
/* 186 */     while (n > 0) {
/* 187 */       int bytesSkipped = (int)in.skip(n);
/* 188 */       if (bytesSkipped < 0)
/* 189 */         throw new EOFException();
/* 190 */       n -= bytesSkipped;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public final byte readByte()
/*     */     throws IOException
/*     */   {
/* 201 */     if (this._packetSize < 1)
/* 202 */       throw new IOException();
/* 203 */     int ch = this.in.read();
/* 204 */     if (ch < 0)
/* 205 */       throw new EOFException();
/* 206 */     this._packetSize -= 1;
/* 207 */     return (byte)ch;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public final int readUnsignedByte()
/*     */     throws IOException
/*     */   {
/* 217 */     if (this._packetSize < 1)
/* 218 */       throw new IOException();
/* 219 */     int ch = this.in.read();
/* 220 */     if (ch < 0)
/* 221 */       throw new EOFException();
/* 222 */     this._packetSize -= 1;
/* 223 */     return ch;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public final short readShort()
/*     */     throws IOException
/*     */   {
/* 232 */     assert (this._packetSize >= 2);
/* 233 */     InputStream in = this.in;
/* 234 */     int ch1 = in.read();
/* 235 */     int ch2 = in.read();
/* 236 */     if ((ch1 | ch2) < 0)
/* 237 */       throw new EOFException();
/* 238 */     this._packetSize -= 2;
/* 239 */     return (short)((ch1 << 8) + (ch2 << 0));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public final int readUnsignedShort()
/*     */     throws IOException
/*     */   {
/* 248 */     assert (this._packetSize >= 2);
/* 249 */     InputStream in = this.in;
/* 250 */     int ch1 = in.read();
/* 251 */     int ch2 = in.read();
/* 252 */     if ((ch1 | ch2) < 0)
/* 253 */       throw new EOFException();
/* 254 */     this._packetSize -= 2;
/* 255 */     return (ch1 << 8) + (ch2 << 0);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String readUTF()
/*     */     throws IOException
/*     */   {
/* 267 */     if (this._packetSize < 1) {
/* 268 */       throw new IOException();
/*     */     }
/* 270 */     int utflen = readUnsignedByte();
/*     */     
/* 272 */     if (this._packetSize < utflen) {
/* 273 */       throw new UTFDataFormatException();
/*     */     }
/*     */     
/*     */ 
/* 277 */     char[] str = new char[utflen];
/* 278 */     int count = 0;
/* 279 */     int strlen = 0;
/* 280 */     while (count < utflen) {
/* 281 */       int c = readUnsignedByte();
/*     */       
/* 283 */       switch (c >> 4)
/*     */       {
/*     */       case 0: 
/*     */       case 1: 
/*     */       case 2: 
/*     */       case 3: 
/*     */       case 4: 
/*     */       case 5: 
/*     */       case 6: 
/*     */       case 7: 
/* 293 */         count++;
/* 294 */         str[(strlen++)] = ((char)c);
/* 295 */         break;
/*     */       
/*     */       case 12: 
/*     */       case 13: 
/* 299 */         count += 2;
/* 300 */         if (count > utflen)
/* 301 */           throw new UTFDataFormatException();
/* 302 */         int char2 = readUnsignedByte();
/* 303 */         if ((char2 & 0xC0) != 128)
/* 304 */           throw new UTFDataFormatException();
/* 305 */         str[(strlen++)] = ((char)((c & 0x1F) << 6 | char2 & 0x3F));
/* 306 */         break;
/*     */       
/*     */       case 14: 
/* 309 */         count += 3;
/* 310 */         if (count > utflen)
/* 311 */           throw new UTFDataFormatException();
/* 312 */         int char2 = readUnsignedByte();
/* 313 */         int char3 = readUnsignedByte();
/* 314 */         if (((char2 & 0xC0) != 128) || ((char3 & 0xC0) != 128))
/* 315 */           throw new UTFDataFormatException();
/* 316 */         str[(strlen++)] = 
/* 317 */           ((char)((c & 0xF) << 12 | (char2 & 0x3F) << 6 | (char3 & 0x3F) << 0));
/* 318 */         break;
/*     */       case 8: case 9: case 10: 
/*     */       case 11: default: 
/* 321 */         throw new UTFDataFormatException();
/*     */       }
/*     */     }
/* 324 */     return new String(str, 0, strlen);
/*     */   }
/*     */ }


/* Location:              C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\network\ServerInputStream.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */