Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/MyRfbProto.java @ 105:e166c3cad2b8
new Defleter is working with fixed TightVNC clients
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 04 Aug 2011 19:34:58 +0900 |
parents | b649584c9712 |
children | 7a7baebdd3cf |
rev | line source |
---|---|
24 | 1 package myVncProxy; |
54 | 2 |
88 | 3 import static org.junit.Assert.*; |
4 | |
25 | 5 import java.awt.Graphics; |
6 import java.awt.Image; | |
7 import java.awt.image.BufferedImage; | |
8 import java.io.BufferedOutputStream; | |
15 | 9 import java.io.BufferedReader; |
25 | 10 import java.io.ByteArrayInputStream; |
11 import java.io.ByteArrayOutputStream; | |
10 | 12 import java.io.IOException; |
43 | 13 import java.io.InputStream; |
15 | 14 import java.io.InputStreamReader; |
23 | 15 import java.net.BindException; |
10 | 16 import java.net.ServerSocket; |
17 import java.net.Socket; | |
90 | 18 import java.nio.ByteBuffer; |
93 | 19 import java.util.Iterator; |
10 | 20 import java.util.LinkedList; |
21 | |
25 | 22 import javax.imageio.ImageIO; |
23 | |
88 | 24 import org.junit.Test; |
25 | |
54 | 26 import myVncProxy.MulticastQueue.Client; |
27 | |
40 | 28 import java.util.concurrent.ExecutorService; |
80 | 29 import java.util.zip.DataFormatException; |
30 import java.util.zip.Deflater; | |
31 import java.util.zip.Inflater; | |
40 | 32 import java.io.OutputStream; |
10 | 33 |
88 | 34 public |
93 | 35 class MyRfbProto<ByteBuffersIterator> extends RfbProto { |
43 | 36 final static String versionMsg_3_998 = "RFB 003.998\n"; |
65 | 37 /** |
38 * CheckMillis is one of new msgType for RFB 3.998. | |
39 */ | |
90 | 40 final static byte SpeedCheckMillis = 4; |
83 | 41 private static final int INFLATE_BUFSIZE = 1024*100; |
65 | 42 boolean printStatusFlag = false; |
43 long startCheckTime; | |
54 | 44 |
18 | 45 private int messageType; |
46 private int rectangles; | |
23 | 47 private int rectX; |
48 private int rectY; | |
49 private int rectW; | |
50 private int rectH; | |
18 | 51 private int encoding; |
27 | 52 private int zLen; |
104 | 53 private boolean clicomp = false; |
18 | 54 |
23 | 55 private ServerSocket servSock; |
56 private int acceptPort; | |
10 | 57 private byte initData[]; |
54 | 58 private LinkedList<Socket> cliListTmp; |
59 private LinkedList<Socket> cliList; | |
27 | 60 boolean createBimgFlag; |
54 | 61 |
40 | 62 ExecutorService executor; |
54 | 63 |
25 | 64 byte[] pngBytes; |
54 | 65 |
102 | 66 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MostRecentMultiCast<LinkedList<ByteBuffer>>(10); |
67 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); | |
80 | 68 private int clients = 0; |
81
9109273b96dc
consume too much memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
69 private Inflater inflater = new Inflater(); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
70 private Deflater deflater = new Deflater(); |
54 | 71 |
88 | 72 public |
73 MyRfbProto() throws IOException { | |
74 } | |
75 | |
54 | 76 MyRfbProto(String h, int p, VncViewer v) throws IOException { |
10 | 77 super(h, p, v); |
54 | 78 cliList = new LinkedList<Socket>(); |
79 cliListTmp = new LinkedList<Socket>(); | |
27 | 80 createBimgFlag = false; |
61 | 81 // sendThreads = new LinkedList<Thread>(); |
54 | 82 // executor = Executors.newCachedThreadPool(); |
83 // executor = Executors.newSingleThreadExecutor(); | |
10 | 84 } |
85 | |
13 | 86 MyRfbProto(String h, int p) throws IOException { |
87 super(h, p); | |
54 | 88 cliList = new LinkedList<Socket>(); |
89 cliListTmp = new LinkedList<Socket>(); | |
27 | 90 createBimgFlag = false; |
61 | 91 // sendThreads = new LinkedList<Thread>(); |
54 | 92 // executor = Executors.newCachedThreadPool(); |
93 // executor = Executors.newSingleThreadExecutor(); | |
13 | 94 } |
24 | 95 |
44 | 96 // over write |
43 | 97 void writeVersionMsg() throws IOException { |
98 clientMajor = 3; | |
99 if (serverMinor >= 9) { | |
54 | 100 clientMinor = 9; |
101 os.write(versionMsg_3_998.getBytes()); | |
43 | 102 } else if (serverMajor > 3 || serverMinor >= 8) { |
103 clientMinor = 8; | |
104 os.write(versionMsg_3_8.getBytes()); | |
105 } else if (serverMinor >= 9) { | |
106 clientMinor = 9; | |
107 os.write(versionMsg_3_998.getBytes()); | |
108 } else if (serverMinor >= 7) { | |
109 clientMinor = 7; | |
110 os.write(versionMsg_3_7.getBytes()); | |
111 } else { | |
112 clientMinor = 3; | |
113 os.write(versionMsg_3_3.getBytes()); | |
114 } | |
115 protocolTightVNC = false; | |
116 initCapabilities(); | |
117 } | |
118 | |
54 | 119 void initServSock(int port) throws IOException { |
10 | 120 servSock = new ServerSocket(port); |
23 | 121 acceptPort = port; |
10 | 122 } |
54 | 123 |
124 // 5550を開けるが、開いてないなら+1のポートを開ける。 | |
80 | 125 void selectPort(int p) { |
126 int port = p; | |
54 | 127 while (true) { |
128 try { | |
80 | 129 initServSock(port); |
23 | 130 break; |
54 | 131 } catch (BindException e) { |
80 | 132 port++; |
23 | 133 continue; |
54 | 134 } catch (IOException e) { |
10 | 135 |
23 | 136 } |
137 } | |
80 | 138 System.out.println("accept port = " + port); |
23 | 139 } |
54 | 140 |
141 int getAcceptPort() { | |
23 | 142 return acceptPort; |
143 } | |
54 | 144 |
10 | 145 void setSoTimeout(int num) throws IOException { |
146 servSock.setSoTimeout(num); | |
147 } | |
54 | 148 |
10 | 149 Socket accept() throws IOException { |
150 return servSock.accept(); | |
151 } | |
152 | |
54 | 153 void addSock(Socket sock) { |
10 | 154 cliList.add(sock); |
155 } | |
54 | 156 |
157 void addSockTmp(Socket sock) { | |
158 System.out.println("connected " + sock.getInetAddress()); | |
27 | 159 cliListTmp.add(sock); |
160 } | |
54 | 161 |
10 | 162 boolean markSupported() { |
163 return is.markSupported(); | |
164 } | |
54 | 165 |
10 | 166 void readServerInit() throws IOException { |
54 | 167 |
78 | 168 is.mark(255); |
10 | 169 skipBytes(20); |
170 int nlen = readU32(); | |
54 | 171 int blen = 20 + 4 + nlen; |
10 | 172 initData = new byte[blen]; |
78 | 173 is.reset(); |
10 | 174 |
78 | 175 is.mark(blen); |
10 | 176 readFully(initData); |
78 | 177 is.reset(); |
54 | 178 |
10 | 179 framebufferWidth = readU16(); |
180 framebufferHeight = readU16(); | |
181 bitsPerPixel = readU8(); | |
182 depth = readU8(); | |
183 bigEndian = (readU8() != 0); | |
184 trueColour = (readU8() != 0); | |
185 redMax = readU16(); | |
186 greenMax = readU16(); | |
187 blueMax = readU16(); | |
188 redShift = readU8(); | |
189 greenShift = readU8(); | |
190 blueShift = readU8(); | |
191 byte[] pad = new byte[3]; | |
192 readFully(pad); | |
193 int nameLength = readU32(); | |
194 byte[] name = new byte[nameLength]; | |
195 readFully(name); | |
196 desktopName = new String(name); | |
197 | |
198 // Read interaction capabilities (TightVNC protocol extensions) | |
199 if (protocolTightVNC) { | |
200 int nServerMessageTypes = readU16(); | |
201 int nClientMessageTypes = readU16(); | |
202 int nEncodingTypes = readU16(); | |
203 readU16(); | |
204 readCapabilityList(serverMsgCaps, nServerMessageTypes); | |
205 readCapabilityList(clientMsgCaps, nClientMessageTypes); | |
206 readCapabilityList(encodingCaps, nEncodingTypes); | |
207 } | |
208 | |
209 inNormalProtocol = true; | |
210 } | |
211 | |
54 | 212 void sendRfbVersion(OutputStream os) throws IOException { |
213 os.write(versionMsg_3_998.getBytes()); | |
43 | 214 } |
54 | 215 |
45 | 216 void readVersionMsg(InputStream is) throws IOException { |
217 | |
218 byte[] b = new byte[12]; | |
219 | |
220 is.read(b); | |
221 | |
222 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
223 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
224 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
225 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
226 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
227 throw new IOException("Host " + host + " port " + port | |
228 + " is not an RFB server"); | |
229 } | |
230 | |
231 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
232 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
233 | |
234 if (serverMajor < 3) { | |
235 throw new IOException( | |
236 "RFB server does not support protocol version 3"); | |
54 | 237 } |
238 | |
239 } | |
240 | |
43 | 241 void sendSecurityType(OutputStream os) throws IOException { |
242 // number-of-security-types | |
243 os.write(1); | |
54 | 244 // security-types |
43 | 245 // 1:None |
246 os.write(1); | |
247 } | |
54 | 248 |
46
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
249 void readSecType(InputStream is) throws IOException { |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
250 byte[] b = new byte[1]; |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
251 is.read(b); |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
252 |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
253 } |
54 | 254 |
47
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
255 void sendSecResult(OutputStream os) throws IOException { |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
256 byte[] b = castIntByte(0); |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
257 os.write(b); |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
258 } |
54 | 259 |
43 | 260 void readClientInit(InputStream in) throws IOException { |
261 byte[] b = new byte[0]; | |
262 in.read(b); | |
263 } | |
54 | 264 |
265 void sendInitData(OutputStream os) throws IOException { | |
266 os.write(initData); | |
10 | 267 } |
268 | |
54 | 269 |
270 void sendPngImage() { | |
271 try { | |
272 for (Socket cli : cliListTmp) { | |
273 try { | |
27 | 274 sendPngData(cli); |
275 addSock(cli); | |
54 | 276 } catch (IOException e) { |
27 | 277 // if socket closed |
278 cliListTmp.remove(cli); | |
279 } | |
280 } | |
54 | 281 // System.out.println("cliSize="+cliSize()); |
282 } catch (Exception e) { | |
27 | 283 } |
284 cliListTmp.clear(); | |
285 } | |
286 | |
15 | 287 boolean ready() throws IOException { |
288 BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
289 return br.ready(); | |
54 | 290 } |
10 | 291 |
54 | 292 int cliSize() { |
10 | 293 return cliList.size(); |
54 | 294 } |
295 | |
296 void printNumBytesRead() { | |
297 System.out.println("numBytesRead=" + numBytesRead); | |
298 } | |
299 | |
300 | |
61 | 301 |
54 | 302 void regiFramebufferUpdate() throws IOException { |
78 | 303 is.mark(20); |
80 | 304 messageType = readU8(); // 0 |
305 skipBytes(1); // 1 | |
306 rectangles = readU16(); // 2 | |
307 rectX = readU16(); // 4 | |
308 rectY = readU16(); // 6 | |
309 rectW = readU16(); // 8 | |
310 rectH = readU16(); // 10 | |
311 encoding = readU32(); // 12 | |
78 | 312 System.out.println("encoding = "+encoding); |
80 | 313 if (encoding == EncodingZRLE) |
27 | 314 zLen = readU32(); |
80 | 315 else |
316 zLen = 0; | |
78 | 317 is.reset(); |
104 | 318 |
15 | 319 } |
54 | 320 |
61 | 321 int checkAndMark() throws IOException { |
322 int dataLen; | |
54 | 323 switch (encoding) { |
23 | 324 case RfbProto.EncodingRaw: |
39 | 325 dataLen = rectW * rectH * 4 + 16; |
78 | 326 is.mark(dataLen); |
23 | 327 break; |
67 | 328 case RfbProto.EncodingCopyRect: |
329 dataLen = 16 + 4; | |
78 | 330 is.mark(dataLen); |
67 | 331 break; |
332 case RfbProto.EncodingRRE: | |
333 case RfbProto.EncodingCoRRE: | |
334 case RfbProto.EncodingHextile: | |
80 | 335 case RfbProto.EncodingTight: |
336 dataLen = zLen + 20; | |
337 is.mark(dataLen); | |
338 break; | |
67 | 339 case RfbProto.EncodingZlib: |
27 | 340 case RfbProto.EncodingZRLE: |
54 | 341 dataLen = zLen + 20; |
78 | 342 is.mark(dataLen); |
343 break; | |
344 case RfbProto.EncodingXCursor: | |
345 case RfbProto.EncodingRichCursor: | |
346 int pixArray = rectW * rectH * 4; | |
347 int u8Array = (int)Math.floor((rectW + 7)/8) * rectH; | |
348 dataLen = pixArray + u8Array; | |
349 printFramebufferUpdate(); | |
350 is.mark(dataLen); | |
27 | 351 break; |
23 | 352 default: |
61 | 353 dataLen = 1000000; |
78 | 354 is.mark(dataLen); |
54 | 355 } |
61 | 356 return dataLen; |
357 } | |
65 | 358 |
359 | |
81
9109273b96dc
consume too much memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
360 void sendDataToClient() throws Exception { |
61 | 361 regiFramebufferUpdate(); |
362 int dataLen = checkAndMark(); | |
363 readSendData(dataLen); | |
23 | 364 } |
54 | 365 |
366 BufferedImage createBufferedImage(Image img) { | |
367 BufferedImage bimg = new BufferedImage(img.getWidth(null), | |
368 img.getHeight(null), BufferedImage.TYPE_INT_RGB); | |
27 | 369 |
25 | 370 Graphics g = bimg.getGraphics(); |
371 g.drawImage(img, 0, 0, null); | |
372 g.dispose(); | |
373 return bimg; | |
374 } | |
375 | |
54 | 376 void createPngBytes(BufferedImage bimg) throws IOException { |
377 pngBytes = getImageBytes(bimg, "png"); | |
25 | 378 } |
54 | 379 |
380 byte[] getBytes(BufferedImage img) throws IOException { | |
25 | 381 byte[] b = getImageBytes(img, "png"); |
382 return b; | |
383 } | |
54 | 384 |
385 byte[] getImageBytes(BufferedImage image, String imageFormat) | |
386 throws IOException { | |
25 | 387 ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
388 BufferedOutputStream os = new BufferedOutputStream(bos); | |
389 image.flush(); | |
390 ImageIO.write(image, imageFormat, os); | |
391 os.flush(); | |
392 os.close(); | |
393 return bos.toByteArray(); | |
394 } | |
395 | |
54 | 396 void sendPngData(Socket sock) throws IOException { |
26 | 397 byte[] dataLength = castIntByte(pngBytes.length); |
398 sock.getOutputStream().write(dataLength); | |
25 | 399 sock.getOutputStream().write(pngBytes); |
400 } | |
54 | 401 |
402 byte[] castIntByte(int len) { | |
26 | 403 byte[] b = new byte[4]; |
54 | 404 b[0] = (byte) ((len >>> 24) & 0xFF); |
405 b[1] = (byte) ((len >>> 16) & 0xFF); | |
406 b[2] = (byte) ((len >>> 8) & 0xFF); | |
407 b[3] = (byte) ((len >>> 0) & 0xFF); | |
26 | 408 return b; |
409 } | |
54 | 410 |
411 BufferedImage createBimg() throws IOException { | |
25 | 412 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes)); |
413 return bimg; | |
414 } | |
104 | 415 |
54 | 416 void printFramebufferUpdate() { |
417 | |
18 | 418 System.out.println("messageType=" + messageType); |
54 | 419 System.out.println("rectangles=" + rectangles); |
18 | 420 System.out.println("encoding=" + encoding); |
78 | 421 System.out.println("rectX = "+rectX+": rectY = "+rectY); |
422 System.out.println("rectW = "+rectW+": rectH = "+rectH); | |
54 | 423 switch (encoding) { |
23 | 424 case RfbProto.EncodingRaw: |
54 | 425 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 |
426 + 16); | |
23 | 427 break; |
428 default: | |
429 } | |
18 | 430 } |
65 | 431 |
77 | 432 void readSpeedCheck() throws IOException { |
433 byte[] b = new byte[1]; | |
65 | 434 readFully(b); |
435 } | |
436 | |
77 | 437 void startSpeedCheck() { |
90 | 438 ByteBuffer b = ByteBuffer.allocate(10); |
439 b.put((byte)SpeedCheckMillis); | |
440 b.flip(); | |
66 | 441 startCheckTime = System.currentTimeMillis(); |
442 System.out.println("startChckTime = "+ startCheckTime); | |
90 | 443 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
84 | 444 bufs.add(b); |
445 multicastqueue.put(bufs); | |
65 | 446 } |
447 | |
77 | 448 void endSpeedCheck() { |
65 | 449 long accTime = System.currentTimeMillis(); |
450 long time = accTime - startCheckTime; | |
451 System.out.println("checkMillis: " + time); | |
452 } | |
453 | |
454 void printStatus() { | |
455 System.out.println(); | |
456 } | |
457 | |
458 synchronized void changeStatusFlag() { | |
459 printStatusFlag = true; | |
460 } | |
461 | |
462 void printMills() { | |
463 if(printStatusFlag) { | |
464 | |
465 changeStatusFlag(); | |
466 } else { | |
467 changeStatusFlag(); | |
468 } | |
469 } | |
87 | 470 |
471 void speedCheckMillis() { | |
472 Runnable stdin = new Runnable() { | |
473 public void run() { | |
474 int c; | |
475 try { | |
476 while( (c = System.in.read()) != -1 ) { | |
477 switch(c) { | |
478 case 's': | |
479 break; | |
480 default: | |
481 startSpeedCheck(); | |
482 break; | |
483 } | |
484 } | |
485 }catch(IOException e){ | |
486 System.out.println(e); | |
487 } | |
488 } | |
489 }; | |
490 | |
491 new Thread(stdin).start(); | |
492 } | |
86 | 493 |
87 | 494 /** |
495 * gzip byte arrays | |
496 * @param deflater | |
497 * @param inputs | |
498 * byte data[] | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
499 * @param inputIndex |
87 | 500 * @param outputs |
501 * byte data[] | |
502 * @return byte length in last byte array | |
503 * @throws IOException | |
504 */ | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
505 public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException { |
102 | 506 int len = 0; |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
507 ByteBuffer c1= ByteBuffer.allocate(INFLATE_BUFSIZE); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
508 while(inputIndex < inputs.size() ) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
509 ByteBuffer b1 = inputs.get(inputIndex++); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
510 deflater.setInput(b1.array(),b1.position(),b1.remaining()); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
511 if (inputIndex==inputs.size()) |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
512 deflater.finish(); |
86 | 513 do { |
102 | 514 int len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining()); |
515 if (len1>0) { | |
516 len += len1; | |
517 c1.position(c1.position()+len1); | |
518 if (c1.remaining()==0) { | |
519 c1.flip(); outputs.addLast(c1); | |
520 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); | |
521 } | |
86 | 522 } |
102 | 523 } while (!deflater.needsInput()&&!deflater.finished()); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
524 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
525 if (c1.position()!=0) { |
102 | 526 c1.flip(); outputs.addLast(c1); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
527 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
528 deflater.reset(); |
90 | 529 return len; |
86 | 530 } |
87 | 531 |
532 /** | |
533 * gunzip byte arrays | |
534 * @param inflater | |
535 * @param inputs | |
536 * byte data[] | |
537 * @param outputs | |
538 * byte data[] | |
91 | 539 *@return number of total bytes |
87 | 540 * @throws IOException |
541 */ | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
542 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs,int bufSize) |
88 | 543 throws DataFormatException { |
102 | 544 int len=0; |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
545 ByteBuffer buf = ByteBuffer.allocate(bufSize); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
546 while (inputIndex < inputs.size()) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
547 ByteBuffer input = inputs.get(inputIndex++); |
102 | 548 inflater.setInput(input.array(),input.position(),input.limit()); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
549 // if (inputIndex==inputs.size()) if inflater/deflater has symmetry, we need this |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
550 // inflater.end(); but this won't work |
88 | 551 do { |
102 | 552 int len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining()); |
553 if (len0>0) { | |
554 buf.position(buf.position()+len0); | |
555 len += len0; | |
556 if (buf.remaining()==0) { | |
557 buf.flip(); | |
558 outputs.addLast(buf); | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
559 buf = ByteBuffer.allocate(bufSize); |
102 | 560 } |
91 | 561 } |
102 | 562 } while (!inflater.needsInput()); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
563 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
564 if (buf.position()!=0) { |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
565 buf.flip(); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
566 outputs.addLast(buf); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
567 } |
90 | 568 return len; |
86 | 569 } |
65 | 570 |
86 | 571 void readSendData(int dataLen) throws IOException, DataFormatException { |
90 | 572 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
573 ByteBuffer header = ByteBuffer.allocate(16); | |
574 readFully(header.array(),0,16); | |
575 header.limit(16); | |
576 if (header.get(0)==RfbProto.FramebufferUpdate) { | |
577 int encoding = header.getInt(12); | |
86 | 578 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { |
90 | 579 ByteBuffer len = ByteBuffer.allocate(4); |
580 readFully(len.array(),0,4); len.limit(4); | |
581 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20); | |
582 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20); | |
583 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>(); | |
88 | 584 inputs.add(inputData); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
585 // int length = rectW * rectH * (bitsPerPixel/8); |
103
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
586 if (clicomp) { |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
587 unzip(inflater, inputs, 0, bufs, INFLATE_BUFSIZE); |
103
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
588 } else { |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
589 Deflater nDeflater = false ? deflater : new Deflater(); |
103
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
590 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
591 unzip(inflater, inputs, 0 , out, INFLATE_BUFSIZE); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
592 dump32(inputs); |
103
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
593 int len2 = zip(nDeflater, out, 0, bufs); |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
594 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
595 bufs.addFirst(blen); |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
596 } |
87 | 597 bufs.addFirst(header); |
86 | 598 multicastqueue.put(bufs); |
599 is.reset(); | |
600 return ; | |
601 } | |
602 } | |
87 | 603 bufs.add(header); |
604 if (dataLen>16) { | |
90 | 605 ByteBuffer b = ByteBuffer.allocate(dataLen-16); |
606 readFully(b.array(),0,dataLen-16); b.limit(dataLen-16); | |
87 | 607 bufs.add(b); |
608 } | |
86 | 609 multicastqueue.put(bufs); |
610 is.reset(); | |
611 | |
612 // It may be compressed. We can inflate here to avoid repeating clients decompressing here, | |
613 // but it may generate too many large data. It is better to do it in each client. | |
614 // But we have do inflation for all input data, so we have to do it here. | |
615 } | |
43 | 616 |
71 | 617 void newClient(AcceptThread acceptThread, final Socket newCli, |
54 | 618 final OutputStream os, final InputStream is) throws IOException { |
619 // createBimgFlag = true; | |
620 // rfb.addSockTmp(newCli); | |
621 // addSock(newCli); | |
90 | 622 final Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient(); |
102 | 623 |
624 final Runnable reader = new Runnable() { | |
625 public void run() { | |
626 byte b[] = new byte[4096]; | |
627 for(;;) { | |
628 try { | |
629 int c = is.read(b); | |
630 if (c<=0) throw new IOException(); | |
631 System.out.println("client read "+c); | |
632 } catch (IOException e) { | |
633 try { | |
634 os.close(); | |
635 is.close(); | |
636 } catch (IOException e1) { | |
637 } | |
638 return; | |
639 } | |
640 } | |
641 } | |
642 }; | |
54 | 643 Runnable sender = new Runnable() { |
644 public void run() { | |
100
ae4df9b07805
Test passed. VNC partial draw on ZRE.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
99
diff
changeset
|
645 Deflater deflater = new Deflater(); |
54 | 646 try { |
65 | 647 /** |
648 * initial connection of RFB protocol | |
649 */ | |
54 | 650 sendRfbVersion(os); |
651 readVersionMsg(is); | |
652 sendSecurityType(os); | |
653 readSecType(is); | |
654 sendSecResult(os); | |
655 readClientInit(is); | |
656 sendInitData(os); | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
657 new Thread(reader).start(); |
54 | 658 for (;;) { |
90 | 659 LinkedList<ByteBuffer> bufs = c.poll(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
660 int inputIndex = 0; |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
661 ByteBuffer header = bufs.get(inputIndex++); |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
662 if (header==null) continue; |
90 | 663 if (header.get(0)==RfbProto.FramebufferUpdate) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
664 System.out.println("client "+ clients); |
90 | 665 int encoding = header.getInt(12); |
80 | 666 if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) { |
103
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
667 LinkedList<ByteBuffer> outs; |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
668 if (clicomp) { |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
669 outs = new LinkedList<ByteBuffer>(); |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
670 int len2 = zip(deflater, bufs, inputIndex, outs); |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
671 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
672 outs.addFirst(blen); |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
673 outs.addFirst(header); |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
674 inputIndex = 0; |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
675 } else { |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
676 outs = bufs; |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
677 inputIndex = 0; |
d1dc2bb0200d
server side recompression.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
678 } |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
679 writeToClient(os,bufs,inputIndex); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
680 continue; |
80 | 681 } |
87 | 682 } |
90 | 683 os.write(header.array(),header.position(),header.limit()); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
684 writeToClient(os, bufs, inputIndex); |
54 | 685 } |
686 } catch (IOException e) { | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
687 try { |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
688 os.close(); |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
689 } catch (IOException e1) { |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
690 } |
87 | 691 /* if socket closed cliList.remove(newCli); */ |
54 | 692 } |
693 } | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
694 |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
695 public void writeToClient(final OutputStream os, |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
696 LinkedList<ByteBuffer> bufs, int inputIndex) |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
697 throws IOException { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
698 while(inputIndex < bufs.size()) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
699 ByteBuffer b = bufs.get(inputIndex++); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
700 os.write(b.array(), b.position(), b.limit()); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
701 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
702 os.flush(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
703 } |
54 | 704 }; |
80 | 705 clients++; |
54 | 706 new Thread(sender).start(); |
707 | |
708 } | |
66 | 709 |
710 | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
711 public void dump32(LinkedList<ByteBuffer>bufs) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
712 int len =0; |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
713 for(ByteBuffer b: bufs) len += b.remaining(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
714 ByteBuffer top = bufs.getFirst(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
715 ByteBuffer end = bufs.getLast(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
716 System.err.println("length: "+len); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
717 System.err.print("head 0: "); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
718 for(int i = 0; i<16 && i < top.remaining(); i++) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
719 System.err.print(" "+ top.get(i)); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
720 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
721 System.err.print("tail 0: "); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
722 for(int i = 0; i<16 && i < end.remaining(); i++) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
723 System.err.print(" "+end.get(i)); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
724 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
725 System.err.println(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
726 } |
88 | 727 |
728 @Test | |
729 public void test1() { | |
730 try { | |
90 | 731 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>(); |
732 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
733 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>(); | |
88 | 734 for(int i=0;i<10;i++) { |
90 | 735 in.add(ByteBuffer.wrap("test1".getBytes())); |
736 in.add(ByteBuffer.wrap("test2".getBytes())); | |
737 in.add(ByteBuffer.wrap("test3".getBytes())); | |
104 | 738 in.add(ByteBuffer.wrap("test44".getBytes())); |
88 | 739 } |
92 | 740 LinkedList<ByteBuffer> in1 = clone(in); |
90 | 741 |
88 | 742 Deflater deflater = new Deflater(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
743 zip(deflater,in,0,out); |
92 | 744 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend on deflator's state |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
745 unzip(inflater, out, 0,out2, INFLATE_BUFSIZE); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
746 inflater.reset(); |
92 | 747 equalByteBuffers(in1, out2); |
748 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>(); | |
104 | 749 deflater = new Deflater(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
750 zip(deflater,out2,0,out4); |
92 | 751 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>(); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
752 unzip(inflater,out4,0, out5, INFLATE_BUFSIZE); |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
753 int len = equalByteBuffers(in1,out5); |
92 | 754 |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
755 System.out.println("Test Ok. "+len); |
88 | 756 } catch (Exception e) { |
757 assertEquals(0,1); | |
758 } | |
759 } | |
760 | |
92 | 761 private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) { |
762 LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>(); | |
763 for(ByteBuffer b: in) { | |
764 ByteBuffer c = b.duplicate(); | |
765 copy.add(c); | |
766 } | |
767 return copy; | |
768 } | |
769 | |
93 | 770 |
771 | |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
772 public int equalByteBuffers(LinkedList<ByteBuffer> in, |
92 | 773 LinkedList<ByteBuffer> out2) { |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
774 int len = 0; |
93 | 775 Iterable<Byte> i = byteBufferIterator(in); |
776 Iterator<Byte> o = byteBufferIterator(out2).iterator(); | |
777 | |
778 for(int b: i) { | |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
779 len ++; |
93 | 780 if (o.hasNext()) { |
781 int c = o.next(); | |
782 assertEquals(b,c); | |
783 } else | |
784 assertEquals(0,1); | |
785 } | |
104 | 786 if (o.hasNext()) |
787 assertEquals(0,1); | |
93 | 788 // System.out.println(); |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
789 return len; |
93 | 790 } |
791 | |
792 private Iterable<Byte> byteBufferIterator(final LinkedList<ByteBuffer> in) { | |
793 return new Iterable<Byte>() { | |
794 public Iterator<Byte> iterator() { | |
795 return new Iterator<Byte>() { | |
796 int bytes = 0; | |
797 int buffers = 0; | |
798 public boolean hasNext() { | |
104 | 799 for(;;) { |
800 if (buffers>=in.size()) return false; | |
801 ByteBuffer b = in.get(buffers); | |
802 if (! (bytes<b.remaining())) { | |
803 buffers ++; bytes=0; | |
804 } else return true; | |
805 } | |
92 | 806 } |
93 | 807 public Byte next() { |
104 | 808 ByteBuffer bf =in.get(buffers); |
93 | 809 byte b = bf.get(bytes++); |
810 if (bf.remaining()<=bytes) { | |
811 buffers++; | |
812 bytes = 0; | |
813 } | |
814 // System.out.print(b); | |
815 return b; | |
816 } | |
817 public void remove() { | |
818 } | |
819 }; | |
92 | 820 } |
93 | 821 }; |
92 | 822 } |
823 | |
54 | 824 } |
66 | 825 |
826 |