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