merge the formfield patch from ooo-build
[ooovba.git] / jurt / com / sun / star / lib / connections / socket / SocketConnection.java
blob39b9fad7841b8a69284ead74097bc90581c37ef3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SocketConnection.java,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package com.sun.star.lib.connections.socket;
33 import java.io.BufferedInputStream;
34 import java.io.BufferedOutputStream;
35 import java.io.InputStream;
36 import java.io.IOException;
37 import java.io.OutputStream;
39 import java.net.Socket;
41 import java.util.Enumeration;
42 import java.util.Vector;
45 import com.sun.star.io.XStreamListener;
47 import com.sun.star.connection.XConnection;
48 import com.sun.star.connection.XConnectionBroadcaster;
50 /**
51 * The SocketConnection implements the <code>XConnection</code> interface
52 * and is uses by the <code>SocketConnector</code> and the <code>SocketAcceptor</code>.
53 * This class is not part of the provided <code>api</code>.
54 * <p>
55 * @version $Revision: 1.6 $ $ $Date: 2008-04-11 11:14:31 $
56 * @author Kay Ramme
57 * @see com.sun.star.comp.connections.SocketAcceptor
58 * @see com.sun.star.comp.connections.SocketConnector
59 * @see com.sun.star.connections.XConnection
60 * @since UDK1.0
62 public class SocketConnection implements XConnection, XConnectionBroadcaster {
63 /**
64 * When set to true, enables various debugging output.
66 static public final boolean DEBUG = false;
68 protected String _description;
69 protected Socket _socket;
70 protected InputStream _inputStream;
71 protected OutputStream _outputStream;
72 protected Vector _listeners;
73 protected boolean _firstRead;
75 /**
76 * Constructs a new <code>SocketConnection</code>.
77 * <p>
78 * @param description the description of the connection
79 * @param socket the socket of the connection
81 public SocketConnection(String description, Socket socket) throws IOException {
82 if (DEBUG) System.err.println("##### " + getClass().getName() + " - instantiated " + description + " " + socket);
84 _description = description
85 + ",localHost=" + socket.getLocalAddress().getHostName()
86 + ",localPort=" + socket.getLocalPort()
87 + ",peerHost=" + socket.getInetAddress().getHostName()
88 + ",peerPort=" + socket.getPort();
90 _socket = socket;
91 _inputStream = new BufferedInputStream(socket.getInputStream());
92 _outputStream = new BufferedOutputStream(socket.getOutputStream());
94 _listeners = new Vector();
95 _firstRead = true;
101 public void addStreamListener(XStreamListener aListener ) throws com.sun.star.uno.RuntimeException {
102 _listeners.addElement(aListener);
105 public void removeStreamListener(XStreamListener aListener ) throws com.sun.star.uno.RuntimeException {
106 _listeners.removeElement(aListener);
109 private void notifyListeners_open() {
110 Enumeration elements = _listeners.elements();
111 while(elements.hasMoreElements()) {
112 XStreamListener xStreamListener = (XStreamListener)elements.nextElement();
113 xStreamListener.started();
117 private void notifyListeners_close() {
118 Enumeration elements = _listeners.elements();
119 while(elements.hasMoreElements()) {
120 XStreamListener xStreamListener = (XStreamListener)elements.nextElement();
121 xStreamListener.closed();
125 private void notifyListeners_error(com.sun.star.uno.Exception exception) {
126 Enumeration elements = _listeners.elements();
127 while(elements.hasMoreElements()) {
128 XStreamListener xStreamListener = (XStreamListener)elements.nextElement();
129 xStreamListener.error(exception);
135 * Read the required number of bytes.
136 * <p>
137 * @return the number of bytes read
138 * @param aReadBytes the outparameter, where the bytes have to be placed
139 * @param nBytesToRead the number of bytes to read
140 * @see com.sun.star.connections.XConnection#read
142 public int read(/*OUT*/byte[][] bytes, int nBytesToRead) throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
143 if(_firstRead) {
144 _firstRead = false;
146 notifyListeners_open();
149 String errMessage = null;
151 int read_bytes = 0;
152 bytes[0] = new byte[nBytesToRead];
154 try {
155 int count = 0;
157 do {
158 count = _inputStream.read(bytes[0], read_bytes, nBytesToRead - read_bytes);
159 if(count == -1)
160 errMessage = "EOF reached - " + getDescription();
162 read_bytes += count;
164 while(read_bytes >= 0 && read_bytes < nBytesToRead && count >= 0);
166 catch(IOException ioException) {
167 if(DEBUG) {
168 System.err.println("##### " + getClass().getName() + ".read - exception occurred:" + ioException);
169 ioException.printStackTrace();
172 errMessage = ioException.toString();
175 if(errMessage != null) {
176 com.sun.star.io.IOException unoIOException = new com.sun.star.io.IOException(errMessage);
177 notifyListeners_error(unoIOException);
179 throw unoIOException;
182 if (DEBUG) System.err.println("##### " + getClass().getName() + " - read byte:" + read_bytes + " " + bytes[0]);
184 return read_bytes;
188 * Write bytes.
189 * <p>
190 * @param aData the bytes to write
191 * @see com.sun.star.connections.XConnection#write
193 public void write(byte aData[]) throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
194 try {
195 _outputStream.write(aData);
197 catch(IOException ioException) {
198 com.sun.star.io.IOException unoIOException = new com.sun.star.io.IOException(ioException.toString());
199 notifyListeners_error(unoIOException);
201 throw unoIOException;
204 if (DEBUG) System.err.println("##### " + getClass().getName() + " - written bytes:" + aData + " " + aData.length);
208 * Flushes the buffer.
209 * <p>
210 * @see com.sun.star.connections.XConnection#flush
212 public void flush() throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
213 try {
214 _outputStream.flush();
216 catch(IOException ioException) {
217 com.sun.star.io.IOException unoIOException = new com.sun.star.io.IOException(ioException.toString());
218 notifyListeners_error(unoIOException);
220 throw unoIOException;
225 * Closes the connection.
226 * <p>
227 * @see com.sun.star.connections.XConnection#close
229 public void close() throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
230 try {
231 _socket.close();
233 catch(IOException ioException) {
234 com.sun.star.io.IOException unoIOException = new com.sun.star.io.IOException(ioException.toString());
235 notifyListeners_error(unoIOException);
237 throw unoIOException;
239 if (DEBUG) System.err.println("##### " + getClass().getName() + " - socket closed");
241 notifyListeners_close();
245 * Gives a description of the connection.
246 * <p>
247 * @return the description
248 * @see com.sun.star.connections.XConnection#getDescription
250 public String getDescription() throws com.sun.star.uno.RuntimeException {
251 return _description;