001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024:
025: package org.rdesktop.objects;
026:
027: import java.io.*;
028: import java.net.*;
029:
030: public class StreamExchangeDbImpl extends
031: org.myoodb.collectable.CollectableDbImpl implements
032: StreamExchange {
033: private static final org.myoodb.util.Logger LOGGER = org.myoodb.util.Logger
034: .getLogger(StreamExchange.class);
035:
036: protected transient int m_port;
037: protected transient String m_host;
038: protected transient Socket m_socket;
039: protected transient OutputStream m_outputStream;
040: protected transient DataInputStream m_inputStream;
041:
042: public StreamExchangeDbImpl() {
043: m_inputStream = null;
044: m_outputStream = null;
045: }
046:
047: public void onDelete() {
048: super .onDelete();
049:
050: try {
051: close();
052: } catch (Exception e) {
053: LOGGER.error("failed close(" + m_host + "," + m_port + ")",
054: e);
055: }
056: }
057:
058: public void open(String host, int port) throws java.io.IOException,
059: java.net.UnknownHostException {
060: m_host = host;
061: m_port = port;
062:
063: try {
064: m_socket = new Socket(m_host, m_port);
065:
066: m_inputStream = new DataInputStream(
067: new BufferedInputStream(m_socket.getInputStream(),
068: 16384));
069: m_outputStream = m_socket.getOutputStream();
070: } catch (java.io.IOException e) {
071: LOGGER.error("failed open(" + m_host + "," + m_port
072: + ") - " + e);
073:
074: throw e;
075: }
076: }
077:
078: public String getHost() {
079: return m_host;
080: }
081:
082: public int getPort() {
083: return m_port;
084: }
085:
086: public void close() throws java.io.IOException {
087: if (m_socket != null) {
088: m_socket.close();
089: m_socket = null;
090: }
091: }
092:
093: public int available() throws java.io.IOException {
094: return m_inputStream.available();
095: }
096:
097: public byte readByte() throws java.io.IOException {
098: return m_inputStream.readByte();
099: }
100:
101: public byte[] readFully(int len) throws java.io.IOException {
102: byte[] b = new byte[len];
103:
104: m_inputStream.readFully(b);
105:
106: return b;
107: }
108:
109: /*
110: public void readFully(byte[] b) throws java.io.IOException
111: {
112: m_inputStream.readFully(b);
113: }
114:
115: public void readFully(byte[] b, int off, int len) throws java.io.IOException
116: {
117: m_inputStream.readFully(b, off, len);
118: }
119: */
120:
121: public byte[] readFully(byte[] b) throws java.io.IOException {
122: m_inputStream.readFully(b);
123:
124: return b;
125: }
126:
127: public byte[] readFully(byte[] b, int off, int len)
128: throws java.io.IOException {
129: m_inputStream.readFully(b, off, len);
130:
131: return b;
132: }
133:
134: public int readInt() throws java.io.IOException {
135: int x = m_inputStream.readInt();
136:
137: return x;
138: }
139:
140: public int readUnsignedByte() throws java.io.IOException {
141: int x = m_inputStream.readUnsignedByte();
142:
143: return x;
144: }
145:
146: public int readUnsignedShort() throws java.io.IOException {
147: int x = m_inputStream.readUnsignedShort();
148:
149: return x;
150: }
151:
152: public int skipBytes(int n) throws java.io.IOException {
153: return m_inputStream.skipBytes(n);
154: }
155:
156: public void write(byte[] b) throws java.io.IOException {
157: m_outputStream.write(b);
158: }
159:
160: public void write(byte[] b, int off, int len)
161: throws java.io.IOException {
162: m_outputStream.write(b, off, len);
163: }
164:
165: public void write(int b) throws java.io.IOException {
166: m_outputStream.write(b);
167: }
168: }
|