1 // UnixDomainSocket.java
3 // Copyright (c) 2001; Echogent Systems, Inc.
4 // See COPYRIGHT file for license details
6 // Modified on 05/29/2003 by Thomas Yan to replace deprecated thread code
11 * This class provides a means of connecting to a unix domain socket server.
13 * @author Robert Morgan
16 public class UnixDomainSocket
20 // Load the Unix Domain Socket C library
21 System
.out
.println("Reading library file: " +
22 System
.mapLibraryName("unixdomainsocket"));
23 System
.loadLibrary("unixdomainsocket");
26 // Input and output streams
27 private UnixDomainSocketInputStream in
;
28 private UnixDomainSocketOutputStream out
;
30 // Socket read timeout
33 // Native methods, implemented in the Unix Domain Socket C library
34 private native static int nativeOpen(String socketFile
);
35 private native static int nativeRead(int nativeSocketFileHandle
);
36 private native static int nativeWrite(int nativeSocketFileHandle
, int data
);
37 private native static void nativeClose(int nativeSocketFileHandle
);
38 private native static void nativeCloseInput(int nativeSocketFileHandle
);
39 private native static void nativeCloseOutput(int nativeSocketFileHandle
);
41 // Handle for the native Unix Domain Socket
42 private int nativeSocketFileHandle
;
45 * Creates a unix domain socket and connects it to the server specified by the socket file.
47 * @param socketFile Name of the socket file
49 * @throws IOException If unable to construct the socket
52 public UnixDomainSocket(String socketFile
)
55 // Create the native socket, and connect using the specified socket file
56 if( (nativeSocketFileHandle
= nativeOpen(socketFile
)) < 0)
58 throw new IOException("Unable to open Unix Domain Socket");
61 // Initialise the socket input and output streams
62 in
= new UnixDomainSocketInputStream();
63 out
= new UnixDomainSocketOutputStream();
67 * Returns an input stream for this socket.
69 * @return An input stream for reading bytes from this socket
72 public InputStream
getInputStream()
74 return (InputStream
)in
;
78 * Returns an output stream for this socket.
80 * @return An output stream for writing bytes to this socket
83 public OutputStream
getOutputStream()
85 return (OutputStream
)out
;
89 * Sets the read timeout for the socket. If a read call blocks for the specified amount of time
90 * it will be cancelled, and a java.io.InterruptedIOException will be thrown. A timeout of zero
91 * is interpreted as an infinite timeout.
93 * @param timeout The specified timeout, in milliseconds.
95 public void setTimeout(int timeout
)
97 this.timeout
= timeout
;
105 nativeClose(nativeSocketFileHandle
);
108 private class UnixDomainSocketInputStream
extends InputStream
110 // Reads a byte of data from the socket input stream
116 // If a timeout is set, then use a read thread
119 // Create a thread to read the byte
120 UnixDomainSocketReadThread thread
= new UnixDomainSocketReadThread();
121 thread
.setDaemon(true);
126 // Wait up until the specified timeout for the thread to complete
127 thread
.join(timeout
);
129 catch(InterruptedException e
)
132 // If the thread is still alive, then the read() call has blocked longer than
133 // the specified timeout
136 //thread.stop(); This has been deprecated!
137 //Just leave thread alone until OS timeout occurs
138 throw new InterruptedIOException("Unix Domain Socket read() call timed out");
142 data
= thread
.getData();
148 data
= nativeRead(nativeSocketFileHandle
);
155 // Closes the socket input stream
159 nativeCloseInput(nativeSocketFileHandle
);
163 private class UnixDomainSocketOutputStream
extends OutputStream
165 // Write a byte of data to the socket output stream
166 public void write(int data
)
169 if((nativeWrite(nativeSocketFileHandle
, data
))<0)
171 throw new IOException("Unable to write to Unix Domain Socket");
175 // Closes the socket output stream
179 nativeCloseOutput(nativeSocketFileHandle
);
183 // Thread class reads a byte of data from the socket. Used for enforcing timeouts.
184 private class UnixDomainSocketReadThread
extends Thread
190 data
= nativeRead(nativeSocketFileHandle
);