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
.pipe
;
20 import java
.io
.IOException
;
21 import java
.util
.ArrayList
;
22 import java
.util
.StringTokenizer
;
24 import com
.sun
.star
.connection
.XConnection
;
25 import com
.sun
.star
.connection
.XConnectionBroadcaster
;
26 import com
.sun
.star
.io
.XStreamListener
;
27 import com
.sun
.star
.lib
.util
.NativeLibraryLoader
;
30 * The PipeConnection implements the <code>XConnection</code> interface
31 * and is uses by the <code>PipeConnector</code> and the <code>PipeAcceptor</code>.
32 * This class is not part of the provided <code>api</code>.
34 * @see com.sun.star.lib.connections.pipe.pipeAcceptor
35 * @see com.sun.star.lib.connections.pipe.pipeConnector
36 * @see com.sun.star.connection.XConnection
39 public class PipeConnection
implements XConnection
, XConnectionBroadcaster
{
41 * When set to true, enables various debugging output.
43 static public final boolean DEBUG
= false;
46 // load shared library for JNI code
47 NativeLibraryLoader
.loadLibrary(PipeConnection
.class.getClassLoader(), "jpipe");
50 protected String _aDescription
;
51 protected long _nPipeHandle
;
52 protected ArrayList
<XStreamListener
> _aListeners
;
53 protected boolean _bFirstRead
;
56 * Constructs a new <code>PipeConnection</code>.
58 * @param description the description of the connection.
60 public PipeConnection(String description
)
63 if (DEBUG
) System
.err
.println("##### " + getClass().getName() + " - instantiated " + description
);
65 _aListeners
= new ArrayList
<XStreamListener
>();
68 // get pipe name from pipe descriptor
70 StringTokenizer aTokenizer
= new StringTokenizer( description
, "," );
71 if ( aTokenizer
.hasMoreTokens() ) {
72 String aConnType
= aTokenizer
.nextToken();
73 if ( !aConnType
.equals( "pipe" ) )
74 throw new RuntimeException( "invalid pipe descriptor: does not start with 'pipe,'" );
76 String aPipeNameParam
= aTokenizer
.nextToken();
77 if ( !aPipeNameParam
.substring( 0, 5 ).equals( "name=" ) )
78 throw new RuntimeException( "invalid pipe descriptor: no 'name=' parameter found" );
79 aPipeName
= aPipeNameParam
.substring( 5 );
82 throw new RuntimeException( "invalid or empty pipe descriptor" );
86 createJNI( aPipeName
);
87 } catch ( java
.lang
.Exception ex1
) {
88 IOException ex2
= new IOException();
94 public void addStreamListener(XStreamListener aListener
) throws com
.sun
.star
.uno
.RuntimeException
{
95 _aListeners
.add(aListener
);
98 public void removeStreamListener(XStreamListener aListener
) throws com
.sun
.star
.uno
.RuntimeException
{
99 _aListeners
.remove(aListener
);
102 private void notifyListeners_open() {
103 for (XStreamListener xStreamListener
: _aListeners
) {
104 xStreamListener
.started();
108 private void notifyListeners_close() {
109 for (XStreamListener xStreamListener
: _aListeners
) {
110 xStreamListener
.closed();
114 private void notifyListeners_error(com
.sun
.star
.uno
.Exception exception
) {
115 for (XStreamListener xStreamListener
: _aListeners
) {
116 xStreamListener
.error(exception
);
120 // JNI implementation to create the pipe
121 private native int createJNI( String name
)
122 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
;
124 // JNI implementation to read from the pipe
125 private native int readJNI(/*OUT*/byte[][] bytes
, int nBytesToRead
)
126 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
;
128 // JNI implementation to write to the pipe
129 private native void writeJNI(byte aData
[])
130 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
;
132 // JNI implementation to flush the pipe
133 private native void flushJNI()
134 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
;
136 // JNI implementation to close the pipe
137 private native void closeJNI()
138 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
;
141 * Read the required number of bytes.
143 * @param bytes the outparameter, where the bytes have to be placed.
144 * @param nBytesToRead the number of bytes to read.
145 * @return the number of bytes read.
147 * @see com.sun.star.connection.XConnection#read
149 public int read(/*OUT*/byte[][] bytes
, int nBytesToRead
)
150 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
155 notifyListeners_open();
158 return readJNI( bytes
, nBytesToRead
);
164 * @param aData the bytes to write.
165 * @see com.sun.star.connection.XConnection#write
167 public void write(byte aData
[])
168 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
174 * Flushes the buffer.
176 * @see com.sun.star.connection.XConnection#flush
179 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
185 * Closes the connection.
187 * @see com.sun.star.connection.XConnection#close
190 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
192 if (DEBUG
) System
.out
.print( "PipeConnection::close() " );
194 notifyListeners_close();
195 if (DEBUG
) System
.out
.println( "done" );
199 * Gives a description of the connection.
201 * @return the description.
202 * @see com.sun.star.connection.XConnection#getDescription
204 public String
getDescription() throws com
.sun
.star
.uno
.RuntimeException
{
205 return _aDescription
;