comparison src/main/java/com/glavsoft/transport/Writer.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 718cdde720d4 17b702648079
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.TransportException;
28
29 import java.io.DataOutputStream;
30 import java.io.IOException;
31 import java.io.OutputStream;
32
33 public class Writer {
34 private final DataOutputStream os;
35
36 public Writer(OutputStream os) {
37 this.os = new DataOutputStream(os);
38 }
39
40 public void flush() throws TransportException {
41 try {
42 os.flush();
43 } catch (IOException e) {
44 throw new TransportException("Cannot flush output stream", e);
45 }
46 }
47
48 public void writeByte(int b) throws TransportException {
49 write((byte) (b & 0xff));
50 }
51
52 public void write(byte b) throws TransportException {
53 try {
54 os.writeByte(b);
55 } catch (IOException e) {
56 throw new TransportException("Cannot write byte", e);
57 }
58 }
59
60 public void writeInt16(int sh) throws TransportException {
61 write((short) (sh & 0xffff));
62 }
63
64 public void write(short sh) throws TransportException {
65 try {
66 os.writeShort(sh);
67 } catch (IOException e) {
68 throw new TransportException("Cannot write short", e);
69 }
70 }
71
72 public void writeInt32(int i) throws TransportException {
73 write(i);
74 }
75
76 public void write(int i) throws TransportException {
77 try {
78 os.writeInt(i);
79 } catch (IOException e) {
80 throw new TransportException("Cannot write int", e);
81 }
82 }
83
84 public void write(byte[] b) throws TransportException {
85 write(b, 0, b.length);
86 }
87
88 public void write(byte[] b, int length) throws TransportException {
89 write(b, 0, length);
90 }
91
92 public void write(byte[] b, int offset, int length)
93 throws TransportException {
94 try {
95 os.write(b, offset, length <= b.length ? length : b.length);
96 } catch (IOException e) {
97 throw new TransportException("Cannot write " + length + " bytes", e);
98 }
99 }
100 }