2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package com
.sun
.star
.lib
.connections
.socket
;
21 import java
.io
.BufferedInputStream
;
22 import java
.io
.BufferedOutputStream
;
23 import java
.io
.IOException
;
24 import java
.io
.InputStream
;
25 import java
.io
.OutputStream
;
27 import java
.net
.Socket
;
29 import java
.util
.ArrayList
;
30 import java
.util
.Arrays
;
32 import com
.sun
.star
.connection
.XConnection
;
33 import com
.sun
.star
.connection
.XConnectionBroadcaster
;
34 import com
.sun
.star
.io
.XStreamListener
;
37 * The SocketConnection implements the <code>XConnection</code> interface
38 * and is uses by the <code>SocketConnector</code> and the <code>SocketAcceptor</code>.
40 * <p>This class is not part of the provided <code>api</code>.</p>
42 * @see com.sun.star.lib.connections.socket.socketAcceptor
43 * @see com.sun.star.lib.connections.socket.socketConnector
44 * @see com.sun.star.connection.XConnection
47 public class SocketConnection
implements XConnection
, XConnectionBroadcaster
{
49 * When set to true, enables various debugging output.
51 static public final boolean DEBUG
= false;
53 protected String _description
;
54 protected Socket _socket
;
55 protected InputStream _inputStream
;
56 protected OutputStream _outputStream
;
57 protected ArrayList
<XStreamListener
> _listeners
;
58 protected boolean _firstRead
;
61 * Constructs a new <code>SocketConnection</code>.
63 * @param description the description of the connection.
64 * @param socket the socket of the connection.
66 public SocketConnection(String description
, Socket socket
) throws IOException
{
67 if (DEBUG
) System
.err
.println("##### " + getClass().getName() + " - instantiated " + description
+ " " + socket
);
69 _description
= description
70 + ",localHost=" + socket
.getLocalAddress().getHostName()
71 + ",localPort=" + socket
.getLocalPort()
72 + ",peerHost=" + socket
.getInetAddress().getHostName()
73 + ",peerPort=" + socket
.getPort();
76 _inputStream
= new BufferedInputStream(socket
.getInputStream());
77 _outputStream
= new BufferedOutputStream(socket
.getOutputStream());
79 _listeners
= new ArrayList
<XStreamListener
>();
83 public void addStreamListener(XStreamListener aListener
)
84 throws com
.sun
.star
.uno
.RuntimeException
{
85 _listeners
.add(aListener
);
88 public void removeStreamListener(XStreamListener aListener
)
89 throws com
.sun
.star
.uno
.RuntimeException
{
90 _listeners
.remove(aListener
);
93 private void notifyListeners_open() {
94 for (XStreamListener xStreamListener
: _listeners
) {
95 xStreamListener
.started();
99 private void notifyListeners_close() {
100 for (XStreamListener xStreamListener
: _listeners
) {
101 xStreamListener
.closed();
105 private void notifyListeners_error(com
.sun
.star
.uno
.Exception exception
) {
106 for (XStreamListener xStreamListener
: _listeners
) {
107 xStreamListener
.error(exception
);
112 * Read the required number of bytes.
114 * @param bytes the outparameter, where the bytes have to be placed.
115 * @param nBytesToRead the number of bytes to read.
116 * @return the number of bytes read.
118 * @see com.sun.star.connection.XConnection#read
120 public int read(/*OUT*/byte[][] bytes
, int nBytesToRead
)
121 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
{
125 notifyListeners_open();
128 String errMessage
= null;
131 bytes
[0] = new byte[nBytesToRead
];
137 count
= _inputStream
.read(bytes
[0], read_bytes
, nBytesToRead
- read_bytes
);
139 errMessage
= "EOF reached - " + getDescription();
143 while(read_bytes
>= 0 && read_bytes
< nBytesToRead
&& count
>= 0);
144 } catch(IOException ioException
) {
146 System
.err
.println("##### " + getClass().getName() + ".read - exception occurred:" + ioException
);
147 ioException
.printStackTrace();
150 errMessage
= ioException
.toString();
153 if(errMessage
!= null) {
154 com
.sun
.star
.io
.IOException unoIOException
= new com
.sun
.star
.io
.IOException(errMessage
);
155 notifyListeners_error(unoIOException
);
157 throw unoIOException
;
160 if (DEBUG
) System
.err
.println("##### " + getClass().getName() + " - read byte:" + read_bytes
+ " " + Arrays
.toString(bytes
[0]));
168 * @param aData the bytes to write.
169 * @see com.sun.star.connection.XConnection#write
171 public void write(byte aData
[]) throws com
.sun
.star
.io
.IOException
,
172 com
.sun
.star
.uno
.RuntimeException
{
174 _outputStream
.write(aData
);
175 } catch(IOException ioException
) {
176 com
.sun
.star
.io
.IOException unoIOException
= new com
.sun
.star
.io
.IOException(ioException
);
177 notifyListeners_error(unoIOException
);
179 throw unoIOException
;
182 if (DEBUG
) System
.err
.println("##### " + getClass().getName() + " - written bytes:" + Arrays
.toString(aData
) + " " + aData
.length
);
186 * Flushes the buffer.
188 * @see com.sun.star.connection.XConnection#flush
190 public void flush() throws com
.sun
.star
.io
.IOException
,
191 com
.sun
.star
.uno
.RuntimeException
{
193 _outputStream
.flush();
194 } catch(IOException ioException
) {
195 com
.sun
.star
.io
.IOException unoIOException
= new com
.sun
.star
.io
.IOException(ioException
);
196 notifyListeners_error(unoIOException
);
198 throw unoIOException
;
203 * Closes the connection.
205 * @see com.sun.star.connection.XConnection#close
207 public void close() throws com
.sun
.star
.io
.IOException
,
208 com
.sun
.star
.uno
.RuntimeException
{
211 } catch(IOException ioException
) {
212 com
.sun
.star
.io
.IOException unoIOException
= new com
.sun
.star
.io
.IOException(ioException
);
213 notifyListeners_error(unoIOException
);
215 throw unoIOException
;
217 if (DEBUG
) System
.err
.println("##### " + getClass().getName() + " - socket closed");
219 notifyListeners_close();
223 * Gives a description of the connection.
225 * @return the description.
226 * @see com.sun.star.connection.XConnection#getDescription
228 public String
getDescription() throws com
.sun
.star
.uno
.RuntimeException
{