Mercurial > hg > Applications > TightVNC_orginal
comparison 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 |
comparison
equal
deleted
inserted
replaced
0:4689cc86d6cb | 52:472a9bcacb21 |
---|---|
1 // Copyright (C) 2010, 2011 GlavSoft LLC. | 1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 //------------------------------------------------------------------------- | 4 //------------------------------------------------------------------------- |
5 // This file is part of the TightVNC software. Please visit our Web site: | 5 // This file is part of the TightVNC software. Please visit our Web site: |
6 // | 6 // |
30 import java.io.*; | 30 import java.io.*; |
31 import java.nio.charset.Charset; | 31 import java.nio.charset.Charset; |
32 | 32 |
33 public class Reader { | 33 public class Reader { |
34 final static Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); | 34 final static Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); |
35 final static Charset UTF8 = Charset.forName("UTF-8"); | |
35 private final DataInputStream is; | 36 private final DataInputStream is; |
36 | 37 |
37 public Reader(InputStream is) { | 38 public Reader(InputStream is) { |
38 this.is = new DataInputStream(new BufferedInputStream(is)); | 39 this.is = new DataInputStream(new BufferedInputStream(is)); |
39 } | 40 } |
78 int readInt = is.readInt(); | 79 int readInt = is.readInt(); |
79 return readInt; | 80 return readInt; |
80 } catch (EOFException e) { | 81 } catch (EOFException e) { |
81 throw new ClosedConnectionException(e); | 82 throw new ClosedConnectionException(e); |
82 } catch (IOException e) { | 83 } catch (IOException e) { |
83 throw new TransportException("Cannot read int16", e); | 84 throw new TransportException("Cannot read int32", e); |
85 } | |
86 } | |
87 | |
88 public long readInt64() throws TransportException { | |
89 try { | |
90 return is.readLong(); | |
91 } catch (EOFException e) { | |
92 throw new ClosedConnectionException(e); | |
93 } catch (IOException e) { | |
94 throw new TransportException("Cannot read int32", e); | |
84 } | 95 } |
85 } | 96 } |
86 | 97 |
87 /** | 98 /** |
88 * Read string by it length. | 99 * Read string by it length. |
96 } | 107 } |
97 | 108 |
98 /** | 109 /** |
99 * Read 32-bit string length and then string themself by it length | 110 * Read 32-bit string length and then string themself by it length |
100 * Use this method only when sure no character accept ASCII will be read. | 111 * Use this method only when sure no character accept ASCII will be read. |
101 * Use readBytes and character encoding conversion instead. | 112 * Use readBytes and character encoding conversion instead or {@link #readUtf8String} method |
113 * when utf-8 encoding needed. | |
102 * | 114 * |
103 * @return String read | 115 * @return String read |
104 * @throws TransportException | 116 * @throws TransportException |
105 */ | 117 */ |
106 public String readString() throws TransportException { | 118 public String readString() throws TransportException { |
107 // unset most sighificant (sign) bit 'cause InputStream#readFully | 119 // unset most significant (sign) bit 'cause InputStream#readFully reads |
108 // reads | 120 // [int] length bytes from stream. Change when really need read string more |
109 // [int] length bytes from stream. Change when realy need read sthing more | 121 // than 2147483647 bytes length |
110 // than | |
111 // 2147483647 bytes lenght | |
112 int length = readInt32() & Integer.MAX_VALUE; | 122 int length = readInt32() & Integer.MAX_VALUE; |
113 return readString(length); | 123 return readString(length); |
124 } | |
125 | |
126 /** | |
127 * Read 32-bit string length and then string themself by it length | |
128 * Assume UTF-8 character encoding used | |
129 * | |
130 * @return String read | |
131 * @throws TransportException | |
132 */ | |
133 public String readUtf8String() throws TransportException { | |
134 // unset most significant (sign) bit 'cause InputStream#readFully reads | |
135 // [int] length bytes from stream. Change when really need read string more | |
136 // than 2147483647 bytes length | |
137 int length = readInt32() & Integer.MAX_VALUE; | |
138 return new String(readBytes(length)); | |
114 } | 139 } |
115 | 140 |
116 public byte[] readBytes(int length) throws TransportException { | 141 public byte[] readBytes(int length) throws TransportException { |
117 byte b[] = new byte[length]; | 142 byte b[] = new byte[length]; |
118 return readBytes(b, 0, length); | 143 return readBytes(b, 0, length); |
126 throw new ClosedConnectionException(e); | 151 throw new ClosedConnectionException(e); |
127 } catch (IOException e) { | 152 } catch (IOException e) { |
128 throw new TransportException("Cannot read " + length + " bytes array", e); | 153 throw new TransportException("Cannot read " + length + " bytes array", e); |
129 } | 154 } |
130 } | 155 } |
156 | |
157 public void skip(int length) throws TransportException { | |
158 try { | |
159 is.skipBytes(length); | |
160 } catch (EOFException e) { | |
161 throw new ClosedConnectionException(e); | |
162 } catch (IOException e) { | |
163 throw new TransportException("Cannot skip " + length + " bytes", e); | |
164 } | |
165 } | |
131 } | 166 } |