1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SocketConnection.java,v $
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
;
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>.
55 * @version $Revision: 1.6 $ $ $Date: 2008-04-11 11:14:31 $
57 * @see com.sun.star.comp.connections.SocketAcceptor
58 * @see com.sun.star.comp.connections.SocketConnector
59 * @see com.sun.star.connections.XConnection
62 public class SocketConnection
implements XConnection
, XConnectionBroadcaster
{
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
;
76 * Constructs a new <code>SocketConnection</code>.
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();
91 _inputStream
= new BufferedInputStream(socket
.getInputStream());
92 _outputStream
= new BufferedOutputStream(socket
.getOutputStream());
94 _listeners
= new Vector();
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.
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
{
146 notifyListeners_open();
149 String errMessage
= null;
152 bytes
[0] = new byte[nBytesToRead
];
158 count
= _inputStream
.read(bytes
[0], read_bytes
, nBytesToRead
- read_bytes
);
160 errMessage
= "EOF reached - " + getDescription();
164 while(read_bytes
>= 0 && read_bytes
< nBytesToRead
&& count
>= 0);
166 catch(IOException ioException
) {
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]);
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
{
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.
210 * @see com.sun.star.connections.XConnection#flush
212 public void flush() throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
{
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.
227 * @see com.sun.star.connections.XConnection#close
229 public void close() throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
{
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.
247 * @return the description
248 * @see com.sun.star.connections.XConnection#getDescription
250 public String
getDescription() throws com
.sun
.star
.uno
.RuntimeException
{