changeset 576:d5138119d8c4

remove single buffer
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 07 Feb 2020 14:50:01 +0900
parents a898e6335978
children a236602a9191
files src/main/java/com/glavsoft/rfb/encoding/decoder/ByteBuffer.java src/main/java/com/glavsoft/rfb/encoding/decoder/RawDecoder.java src/main/java/com/glavsoft/rfb/encoding/decoder/RichCursorDecoder.java src/main/java/com/glavsoft/rfb/encoding/decoder/TightDecoder.java src/main/java/com/glavsoft/rfb/encoding/decoder/ZRLEDecoder.java src/main/java/com/glavsoft/rfb/encoding/decoder/ZlibDecoder.java src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java src/main/java/jp/ac/u_ryukyu/treevnc/SoundPacketQueue.java
diffstat 8 files changed, 31 insertions(+), 121 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/com/glavsoft/rfb/encoding/decoder/ByteBuffer.java	Fri Feb 07 12:12:27 2020 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,58 +0,0 @@
-// Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
-// All rights reserved.
-//
-//-------------------------------------------------------------------------
-// This file is part of the TightVNC software.  Please visit our Web site:
-//
-//                       http://www.tightvnc.com/
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License along
-// with this program; if not, write to the Free Software Foundation, Inc.,
-// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-//-------------------------------------------------------------------------
-//
-
-package com.glavsoft.rfb.encoding.decoder;
-
-/**
- * Resizeable to needed length byte buffer
- * Singleton for share among decoders.
- */
-public class ByteBuffer {
-	private static ByteBuffer instance = new ByteBuffer();
-	private byte [] buffer = new byte[0];
-
-	private ByteBuffer() { /*empty*/ }
-	public static ByteBuffer getInstance() {
-		return instance;
-	}
-
-	/**
-	 * Checks for buffer capacity is enougth ( < length) and enlarge it if not
-	 *
-	 * @param length
-	 */
-	public void correctBufferCapacity(int length) {
-		// procondition: buffer != null
-		assert (buffer != null);
-		if (buffer.length < length) {
-			buffer = new byte[length];
-		}
-	}
-
-	public byte[] getBuffer(int length) {
-		correctBufferCapacity(length);
-		return buffer;
-	}
-
-}
--- a/src/main/java/com/glavsoft/rfb/encoding/decoder/RawDecoder.java	Fri Feb 07 12:12:27 2020 +0900
+++ b/src/main/java/com/glavsoft/rfb/encoding/decoder/RawDecoder.java	Fri Feb 07 14:50:01 2020 +0900
@@ -28,6 +28,8 @@
 import com.glavsoft.exceptions.TransportException;
 import com.glavsoft.transport.Reader;
 
+import java.nio.ByteBuffer;
+
 public class RawDecoder extends Decoder {
 	private static RawDecoder instance = new RawDecoder();
 	public static RawDecoder getInstance() {
@@ -44,7 +46,7 @@
 	public void decode(Reader reader, Renderer renderer, int x, int y,
 			int width, int height) throws TransportException {
 		int length = width * height * renderer.getBytesPerPixel();
-		byte [] bytes = ByteBuffer.getInstance().getBuffer(length);
+		byte [] bytes = ByteBuffer.allocate(length).array();
 		reader.readBytes(bytes, 0, length);
 		renderer.drawBytes(bytes, x, y, width, height);
 	}
--- a/src/main/java/com/glavsoft/rfb/encoding/decoder/RichCursorDecoder.java	Fri Feb 07 12:12:27 2020 +0900
+++ b/src/main/java/com/glavsoft/rfb/encoding/decoder/RichCursorDecoder.java	Fri Feb 07 14:50:01 2020 +0900
@@ -28,6 +28,8 @@
 import com.glavsoft.exceptions.TransportException;
 import com.glavsoft.transport.Reader;
 
+import java.nio.ByteBuffer;
+
 /**
  * Decoder for RichCursor pseudo encoding
  */
@@ -47,7 +49,7 @@
 		int length = rect.width * rect.height * bytesPerPixel;
 		if (0 == length)
 			return;
-		byte[] buffer = ByteBuffer.getInstance().getBuffer(length);
+		byte[] buffer = ByteBuffer.allocate(length).array();
 		reader.readBytes(buffer, 0, length);
 
 		StringBuilder sb = new StringBuilder(" ");
--- a/src/main/java/com/glavsoft/rfb/encoding/decoder/TightDecoder.java	Fri Feb 07 12:12:27 2020 +0900
+++ b/src/main/java/com/glavsoft/rfb/encoding/decoder/TightDecoder.java	Fri Feb 07 14:50:01 2020 +0900
@@ -29,6 +29,7 @@
 import com.glavsoft.exceptions.TransportException;
 import com.glavsoft.transport.Reader;
 
+import java.nio.ByteBuffer;
 import java.util.logging.Logger;
 import java.util.zip.DataFormatException;
 import java.util.zip.Inflater;
@@ -204,7 +205,7 @@
 	 */
 	private byte[] readTightData(int expectedLength, Reader reader) throws TransportException {
 		if (expectedLength < MIN_SIZE_TO_COMPRESS) {
-			byte [] buffer = ByteBuffer.getInstance().getBuffer(expectedLength);
+			byte [] buffer = ByteBuffer.allocate(expectedLength).array();
 			reader.readBytes(buffer, 0, expectedLength);
 			return buffer;
 		} else
@@ -226,7 +227,7 @@
 	private byte[] readCompressedData(int expectedLength, Reader reader) throws TransportException {
 		int rawDataLength = readCompactSize(reader);
 
-		byte [] buffer = ByteBuffer.getInstance().getBuffer(expectedLength + rawDataLength);
+		byte [] buffer = ByteBuffer.allocate(expectedLength + rawDataLength).array();
 		// read compressed (raw) data behind space allocated for decompressed data
 		reader.readBytes(buffer, expectedLength, rawDataLength);
 		if (null == decoders[decoderId]) {
@@ -246,7 +247,7 @@
 	private void processJpegType(Reader reader, Renderer renderer,
 			FramebufferUpdateRectangle rect) throws TransportException {
 		int jpegBufferLength = readCompactSize(reader);
-		byte [] bytes = ByteBuffer.getInstance().getBuffer(jpegBufferLength);
+		byte [] bytes = ByteBuffer.allocate(jpegBufferLength).array();
 		reader.readBytes(bytes, 0, jpegBufferLength);
 		renderer.drawJpegImage(bytes, 0, jpegBufferLength, rect);
 	}
--- a/src/main/java/com/glavsoft/rfb/encoding/decoder/ZRLEDecoder.java	Fri Feb 07 12:12:27 2020 +0900
+++ b/src/main/java/com/glavsoft/rfb/encoding/decoder/ZRLEDecoder.java	Fri Feb 07 14:50:01 2020 +0900
@@ -249,7 +249,7 @@
 		int zippedLength = (int) reader.readUInt32();
 		if (0 == zippedLength) return;
 		int length = rect.width * rect.height * renderer.getBytesPerPixel();
-		byte[] bytes = unzip(reader, zippedLength, length, rect.getEncodingType());
+		byte[] bytes = unzip(reader, zippedLength, length, rect.getEncodingType()).array();
 		decode1(renderer, null, rect, bytes, zippedLength, null);
 	}
 
@@ -261,7 +261,7 @@
 		int zippedLength = (int) reader.readUInt32();
 		if (0 == zippedLength) return;
 		int length = rect.width * rect.height * renderer.getBytesPerPixel();
-		byte[] bytes = unzip(reader, zippedLength, length, rect.getEncodingType());
+		byte[] bytes = unzip(reader, zippedLength, length, rect.getEncodingType()).array();
 		decode1(renderer, header, rect, bytes, zippedLength, rfb);
 	}
 
--- a/src/main/java/com/glavsoft/rfb/encoding/decoder/ZlibDecoder.java	Fri Feb 07 12:12:27 2020 +0900
+++ b/src/main/java/com/glavsoft/rfb/encoding/decoder/ZlibDecoder.java	Fri Feb 07 14:50:01 2020 +0900
@@ -30,7 +30,7 @@
 import com.glavsoft.transport.Reader;
 
 import java.io.ByteArrayInputStream;
-import java.util.logging.Logger;
+import java.nio.ByteBuffer;
 import java.util.zip.DataFormatException;
 import java.util.zip.Inflater;
 
@@ -43,27 +43,30 @@
 		int zippedLength = (int) reader.readUInt32();
 		if (0 == zippedLength) return;
 		int length = rect.width * rect.height * renderer.getBytesPerPixel();
-		byte[] bytes = unzip(reader, zippedLength, length, rect.getEncodingType());
+		byte[] bytes = unzip(reader, zippedLength, length, rect.getEncodingType()).array();
 		Reader unzippedReader =
 			new Reader(	new ByteArrayInputStream(bytes, zippedLength, length));
 		RawDecoder.getInstance().decode(unzippedReader, renderer, rect);
 	}
 
-	protected byte[] unzip(Reader reader, int zippedLength, int length,EncodingType encodingType)
+	public ByteBuffer unzip(Reader reader, int zippedLength, int length, EncodingType encodingType)
 			throws TransportException {
-		byte [] bytes = ByteBuffer.getInstance().getBuffer(zippedLength + length);
+		ByteBuffer buf = ByteBuffer.allocate(zippedLength + length);
+		byte [] bytes = buf.array();
 		reader.readBytes(bytes, 0, zippedLength);
+		buf.position(zippedLength);
 		if (null == decoder || encodingType == EncodingType.ZRLEE) {
             decoder = new Inflater();
 		} 
 		decoder.setInput(bytes, 0, zippedLength);
 		try {
-			//messageDump(bytes, "inflate: ");
-			decoder.inflate(bytes, zippedLength, length);
+			decoder.inflate(buf);
+			buf.limit((buf.position()));
+			buf.position(zippedLength);
 		} catch (DataFormatException e) {
 			throw new TransportException("cannot inflate Zlib data", e);
 		}
-		return bytes;
+		return buf;
 	}
 
 	@Override
@@ -71,25 +74,4 @@
 		decoder = null;
 	}
 
-	 private void messageDump(Reader reader, String msg) {
-		 System.out.print(msg);
-		 reader.mark(30);
-		 try {
-			 for (int i = 0; i < 24; i++) {   // 20 + 4
-				 System.out.print(String.format("%02x ", reader.readByte()));
-			 }
-			 reader.reset();
-		 } catch (TransportException e) {
-
-		 }
-		 System.out.println();
-	 }
-
-	 private void messageDump(byte[] bytes, String msg) {
-		 System.out.print(msg);
-		 for (int i = 0; i < 24; i++) {   // 20 + 4
-			 System.out.print(String.format("%02x ", bytes[i]));
-		 }
-		 System.out.println();
-	 }
 }
--- a/src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java	Fri Feb 07 12:12:27 2020 +0900
+++ b/src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java	Fri Feb 07 14:50:01 2020 +0900
@@ -47,8 +47,6 @@
 import java.util.Timer;
 import java.util.logging.Logger;
 import java.util.Objects;
-import java.util.zip.DataFormatException;
-import java.util.zip.Inflater;
 
 
 public class ReceiverTask implements Runnable {
@@ -388,10 +386,14 @@
             if (true) {
                 in.mark(c1.limit() - 4);
                 rect.fill(in);
-                int length = rect.width * rect.height * renderer.getBytesPerPixel();
-                int zippedLength = (int) in.readUInt32();
-                byte[] unzipBytes = unzip(in, zippedLength, length, rect.getEncodingType());
-                CompairBytes(unzipBytes, checkBytes, flushOffset, flushEnd);
+                if (rect.getEncodingType() == EncodingType.ZRLEE ) {
+                    int length = rect.width * rect.height * renderer.getBytesPerPixel();
+                    int zippedLength = (int) in.readUInt32();
+                    ZRLEDecoder decoder = new ZRLEDecoder();
+                    ByteBuffer buf = decoder.unzip(in, zippedLength, length, rect.getEncodingType());
+                    byte[] unzipBytes = buf.array();
+                    compareBytes(unzipBytes, checkBytes, flushOffset, flushEnd);
+                }
                 in.reset();
             }
             while (numberOfRectangeles-- > 0) {
@@ -407,7 +409,7 @@
         }
     }
 
-    private void CompairBytes(byte[] compressBytes, byte[] unCompressBytes, int flushOffset, int flushEnd) {
+    private void compareBytes(byte[] compressBytes, byte[] unCompressBytes, int flushOffset, int flushEnd) {
         if (compressBytes.length == (flushEnd - flushOffset)) {
             if (Objects.deepEquals(compressBytes, unCompressBytes)) {
                 System.out.println("Bytes compair is true");
@@ -419,25 +421,6 @@
         }
     }
 
-    private byte[] unzip(Reader reader, int zippedLength, int length,EncodingType encodingType)
-            throws TransportException {
-        Inflater decoder = null;
-        byte [] bytes = com.glavsoft.rfb.encoding.decoder.ByteBuffer.getInstance().getBuffer(zippedLength + length);
-        reader.readBytes(bytes, 0, zippedLength);
-        if (encodingType == EncodingType.ZRLEE) {
-            //decoder = new ZRLEDecoder();
-            decoder = new Inflater();
-        }
-        decoder.setInput(bytes, 0, zippedLength);
-        try {
-            //messageDump(bytes, "inflate: ");
-            decoder.inflate(bytes, zippedLength, length);
-        } catch (DataFormatException e) {
-            throw new TransportException("cannot inflate Zlib data", e);
-        }
-        return bytes;
-    }
-
     private void setScreenParameter(FramebufferUpdateRectangle rect,int singleWidth ,int singleHeight) {
         ViewerInterface v = rfb.getViewer();
         ConnectionPresenter cp = v.getConnectionPresenter();
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/SoundPacketQueue.java	Fri Feb 07 12:12:27 2020 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/SoundPacketQueue.java	Fri Feb 07 14:50:01 2020 +0900
@@ -1,7 +1,5 @@
 package jp.ac.u_ryukyu.treevnc;
 
-import com.glavsoft.rfb.encoding.decoder.ByteBuffer;
-
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.TimeUnit;