diff src/main/java/com/glavsoft/transport/Reader.java @ 52:472a9bcacb21 draft default tip

TightVNC 2.7.1.0
author you@cr.ie.u-ryukyu.ac.jp
date Wed, 07 Aug 2013 19:01:17 +0900
parents 4689cc86d6cb
children
line wrap: on
line diff
--- a/src/main/java/com/glavsoft/transport/Reader.java	Tue Jul 03 13:20:49 2012 +0900
+++ b/src/main/java/com/glavsoft/transport/Reader.java	Wed Aug 07 19:01:17 2013 +0900
@@ -1,4 +1,4 @@
-// Copyright (C) 2010, 2011 GlavSoft LLC.
+// Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
 // All rights reserved.
 //
 //-------------------------------------------------------------------------
@@ -32,6 +32,7 @@
 
 public class Reader {
 	final static Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
+	final static Charset UTF8 = Charset.forName("UTF-8");
 	private final DataInputStream is;
 
 	public Reader(InputStream is) {
@@ -80,7 +81,17 @@
 		} catch (EOFException e) {
 			throw new ClosedConnectionException(e);
 		} catch (IOException e) {
-			throw new TransportException("Cannot read int16", e);
+			throw new TransportException("Cannot read int32", e);
+		}
+	}
+
+	public long readInt64() throws TransportException {
+		try {
+			return is.readLong();
+		} catch (EOFException e) {
+			throw new ClosedConnectionException(e);
+		} catch (IOException e) {
+			throw new TransportException("Cannot read int32", e);
 		}
 	}
 
@@ -98,21 +109,35 @@
 	/**
 	 * Read 32-bit string length and then string themself by it length
 	 * Use this method only when sure no character accept ASCII will be read.
-	 * Use readBytes and character encoding conversion instead.
+	 * Use readBytes and character encoding conversion instead or {@link #readUtf8String} method
+	 * when utf-8 encoding needed.
 	 *
 	 * @return String read
 	 * @throws TransportException
 	 */
 	public String readString() throws TransportException {
-		// unset most sighificant (sign) bit 'cause InputStream#readFully
-		// reads
-		// [int] length bytes from stream. Change when realy need read sthing more
-		// than
-		// 2147483647 bytes lenght
+		// unset most significant (sign) bit 'cause InputStream#readFully reads
+		// [int] length bytes from stream. Change when really need read string more
+		// than 2147483647 bytes length
 		int length = readInt32() & Integer.MAX_VALUE;
 		return readString(length);
 	}
 
+	/**
+	 * Read 32-bit string length and then string themself by it length
+	 * Assume UTF-8 character encoding used
+	 *
+	 * @return String read
+	 * @throws TransportException
+	 */
+	public String readUtf8String() throws TransportException {
+		// unset most significant (sign) bit 'cause InputStream#readFully  reads
+		// [int] length bytes from stream. Change when really need read string more
+		// than 2147483647 bytes length
+		int length = readInt32() & Integer.MAX_VALUE;
+		return new String(readBytes(length));
+	}
+
 	public byte[] readBytes(int length) throws TransportException {
 		byte b[] = new byte[length];
 		return readBytes(b, 0, length);
@@ -128,4 +153,14 @@
 			throw new TransportException("Cannot read " + length + " bytes array", e);
 		}
 	}
+
+	public void skip(int length) throws TransportException {
+		try {
+			is.skipBytes(length);
+		} catch (EOFException e) {
+			throw new ClosedConnectionException(e);
+		} catch (IOException e) {
+			throw new TransportException("Cannot skip " + length + " bytes", e);
+		}
+	}
 }
\ No newline at end of file