comparison src/main/java/com/glavsoft/transport/Reader.java @ 0:4689cc86d6cb

create TreeViewer2 Repository
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Tue, 03 Jul 2012 13:20:49 +0900
parents
children e7ce2b2ffed8 472a9bcacb21
comparison
equal deleted inserted replaced
-1:000000000000 0:4689cc86d6cb
1 // Copyright (C) 2010, 2011 GlavSoft LLC.
2 // All rights reserved.
3 //
4 //-------------------------------------------------------------------------
5 // This file is part of the TightVNC software. Please visit our Web site:
6 //
7 // http://www.tightvnc.com/
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License along
20 // with this program; if not, write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 //-------------------------------------------------------------------------
23 //
24
25 package com.glavsoft.transport;
26
27 import com.glavsoft.exceptions.ClosedConnectionException;
28 import com.glavsoft.exceptions.TransportException;
29
30 import java.io.*;
31 import java.nio.charset.Charset;
32
33 public class Reader {
34 final static Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
35 private final DataInputStream is;
36
37 public Reader(InputStream is) {
38 this.is = new DataInputStream(new BufferedInputStream(is));
39 }
40
41 public byte readByte() throws TransportException {
42 try {
43 byte readByte = is.readByte();
44 return readByte;
45 } catch (EOFException e) {
46 throw new ClosedConnectionException(e);
47 } catch (IOException e) {
48 throw new TransportException("Cannot read byte", e);
49 }
50
51 }
52
53 public int readUInt8() throws TransportException {
54 return readByte() & 0x0ff;
55 }
56
57 public int readUInt16() throws TransportException {
58 return readInt16() & 0x0ffff;
59 }
60
61 public short readInt16() throws TransportException {
62 try {
63 short readShort = is.readShort();
64 return readShort;
65 } catch (EOFException e) {
66 throw new ClosedConnectionException(e);
67 } catch (IOException e) {
68 throw new TransportException("Cannot read int16", e);
69 }
70 }
71
72 public long readUInt32() throws TransportException {
73 return readInt32() & 0xffffffffL;
74 }
75
76 public int readInt32() throws TransportException {
77 try {
78 int readInt = is.readInt();
79 return readInt;
80 } catch (EOFException e) {
81 throw new ClosedConnectionException(e);
82 } catch (IOException e) {
83 throw new TransportException("Cannot read int16", e);
84 }
85 }
86
87 /**
88 * Read string by it length.
89 * Use this method only when sure no character accept ASCII will be read.
90 * Use readBytes and character encoding conversion instead.
91 *
92 * @return String read
93 */
94 public String readString(int length) throws TransportException {
95 return new String(readBytes(length));
96 }
97
98 /**
99 * 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.
101 * Use readBytes and character encoding conversion instead.
102 *
103 * @return String read
104 * @throws TransportException
105 */
106 public String readString() throws TransportException {
107 // unset most sighificant (sign) bit 'cause InputStream#readFully
108 // reads
109 // [int] length bytes from stream. Change when realy need read sthing more
110 // than
111 // 2147483647 bytes lenght
112 int length = readInt32() & Integer.MAX_VALUE;
113 return readString(length);
114 }
115
116 public byte[] readBytes(int length) throws TransportException {
117 byte b[] = new byte[length];
118 return readBytes(b, 0, length);
119 }
120
121 public byte[] readBytes(byte[] b, int offset, int length) throws TransportException {
122 try {
123 is.readFully(b, offset, length);
124 return b;
125 } catch (EOFException e) {
126 throw new ClosedConnectionException(e);
127 } catch (IOException e) {
128 throw new TransportException("Cannot read " + length + " bytes array", e);
129 }
130 }
131 }