/**************************************************************** ** Copyright 1997 by DTAI Incorporated, All Rights Reserved ** **************************************************************** ** ** $Id: NetPort.java,v 1.1 1999/10/18 17:52:20 csl Exp $ ** ** $Source: /h/chrome/alliance/developer/viewsource/kadel_ssjava/NetPort.java,v $ ** ****************************************************************/ package dtai.net; import java.net.*; import java.io.*; /** * NetPort - provides a simple API to client and server sockets. This is a * subclass for NetPortToServer and NetPortToClient * * @see dtai.net.NetPortToServer * @see dtai.net.NetPortToClient * @version 1.0b1 * @author DTAI, Incorporated * * $Id: NetPort.java,v 1.1 1999/10/18 17:52:20 csl Exp $ * * $Source: /h/chrome/alliance/developer/viewsource/kadel_ssjava/NetPort.java,v $ */ public class NetPort extends Thread implements DataInput, DataOutput { private Socket socket; private DataOutputStream outstream; private DataInputStream instream; /** * constructs a NetPort superclass from the given Socket, and opens * associated * input and output streams */ protected NetPort( Socket socket ) throws IOException { this.socket = socket; instream = new DataInputStream( new BufferedInputStream( socket.getInputStream() ) ); outstream = new DataOutputStream( new BufferedOutputStream( socket.getOutputStream() ) ); } /** * This is normally invoked by calling "start()" to run this in its own * thread. It loops forever and calls readInput(). */ public void run () { try { while ( true ) { try { readInput (); } catch ( IOException ioerror ) { ioerror.printStackTrace(); break; } } } catch ( Exception e ) { e.printStackTrace(); } } /** * closes the socket, input stream and output stream */ public void close() throws IOException { if ( instream != null ) { instream.close(); } if ( outstream != null ) { outstream.close(); } if ( socket != null ) { socket.close(); } } protected void finalize() { try { close(); } catch( IOException ioerror ) { ioerror.printStackTrace(); } catch( Exception e ) { } } /** * Returns a DataInputStream associated with this port. Most functions * can be executed directly on the NetPort object itself, however, since * it implements both DataInput and DataOutput. * * @return the DataInputStream * @exception CLASSNAME DESCRIPTION * @see CLASSNAME#METHOD */ public DataInputStream getInputStream () { return ( instream ); } /** * Returns a DataOutputStream associated with this port. Most functions * can be executed directly on the NetPort object itself, however, since * it implements both DataInput and DataOutput. * * @return the DataOutputStream */ public DataOutputStream getOutputStream () { return ( outstream ); } private synchronized void doNothing() { try { wait(); } catch ( InterruptedException ie ) { } } /** * This function should be extended in a subclass. It is called by "run()" * (normally if a thread is started with "start()") and is continually * recalled in a loop. */ protected void readInput() throws IOException { doNothing(); } // implement DataOutput /////////////////////////////////////////////////////////////////////////////// /** * Flushes the stream. This will write any buffered * output bytes. * @exception IOException If an I/O error has occurred. */ public void flush() throws IOException { // not part of DataOutput // interface, but necessary for // DataOutputStream class outstream.flush(); } /** * Writes a byte. Will block until the byte is actually * written. * @param b the byte to be written * @exception IOException If an I/O error has occurred. */ public void write( int byteval ) throws IOException { outstream.write( byteval ); } /** * Writes an array of bytes. * @param b the data to be written * @exception IOException If an I/O error has occurred. */ public void write( byte b[] ) throws IOException { outstream.write( b ); } /** * Writes a subarray of bytes. * @param b the data to be written * @param off the start offset in the data * @param len the number of bytes that are written * @exception IOException If an I/O error has occurred. */ public void write( byte b[], int off, int len ) throws IOException { outstream.write( b, off, len ); } /** * Writes a boolean. * @param v the boolean to be written */ public void writeBoolean( boolean v ) throws IOException { outstream.writeBoolean( v ); } /** * Writes an 8 bit byte. * @param v the byte value to be written */ public void writeByte( int v ) throws IOException { outstream.writeByte( v ); } /** * Writes a String as a sequence of bytes. * @param s the String of bytes to be written */ public void writeBytes( String s ) throws IOException { outstream.writeBytes( s ); } /** * Writes a 16 bit char. * @param v the char value to be written */ public void writeChar( int v ) throws IOException { outstream.writeChar( v ); } /** * Writes a String in UTF format. * @param str the String in UTF format */ public void writeChars( String s ) throws IOException { outstream.writeChars( s ); } /** * Writes a 64 bit double. * @param v the double value to be written */ public void writeDouble( double v ) throws IOException { outstream.writeDouble( v ); } /** * Writes a 32 bit float. * @param v the float value to be written */ public void writeFloat( float v ) throws IOException { outstream.writeFloat( v ); } /** * Writes a 32 bit int. * @param v the integer value to be written */ public void writeInt( int v ) throws IOException { outstream.writeInt( v ); } /** * Writes a 64 bit long. * @param v the long value to be written */ public void writeLong( long v ) throws IOException { outstream.writeLong( v ); } /** * Writes a 16 bit short. * @param v the short value to be written */ public void writeShort( int v ) throws IOException { outstream.writeShort( v ); } /** * Writes a String in UTF format. * @param str the String in UTF format */ public void writeUTF( String str ) throws IOException { outstream.writeUTF( str ); } /** * Reads bytes, blocking until all bytes are read. * @param b the buffer into which the data is read * @exception IOException If other I/O error has occurred. */ public void readFully(byte b[]) throws IOException { instream.readFully( b ); } /** * Reads bytes, blocking until all bytes are read. * @param b the buffer into which the data is read * @param off the start offset of the data * @param len the maximum number of bytes to read * @exception IOException If other I/O error has occurred. */ public void readFully(byte b[], int off, int len) throws IOException { instream.readFully( b, off, len ); } /** * Skips bytes, block until all bytes are skipped. * @param n the number of bytes to be skipped * @return the actual number of bytes skipped. * @exception IOException If other I/O error has occurred. */ public int skipBytes(int n) throws IOException { return instream.skipBytes( n ); } /** * Reads in a boolean. * @return the boolean read. * @exception IOException If other I/O error has occurred. */ public boolean readBoolean() throws IOException { return instream.readBoolean(); } /** * Reads an 8 bit byte. * @return the 8 bit byte read. * @exception IOException If other I/O error has occurred. */ public byte readByte() throws IOException { return instream.readByte(); } /** * Reads an unsigned 8 bit byte. * @return the 8 bit byte read. * @exception IOException If other I/O error has occurred. */ public int readUnsignedByte() throws IOException { return instream.readUnsignedByte(); } /** * Reads a 16 bit short. * @return the 16 bit short read. * @exception IOException If other I/O error has occurred. */ public short readShort() throws IOException { return instream.readShort(); } /** * Reads an unsigned 16 bit short. * @return the 16 bit short read. * @exception IOException If other I/O error has occurred. */ public int readUnsignedShort() throws IOException { return instream.readUnsignedShort(); } /** * Reads a 16 bit char. * @return the 16 bit char read. * @exception IOException If other I/O error has occurred. */ public char readChar() throws IOException { return instream.readChar(); } /** * Reads a 32 bit int. * @return the 32 bit integer read. * @exception IOException If other I/O error has occurred. */ public int readInt() throws IOException { return instream.readInt(); } /** * Reads a 64 bit long. * @return the read 64 bit long. * @exception IOException If other I/O error has occurred. */ public long readLong() throws IOException { return instream.readLong(); } /** * Reads a 32 bit float. * @return the 32 bit float read. * @exception IOException If other I/O error has occurred. */ public float readFloat() throws IOException { return instream.readFloat(); } /** * Reads a 64 bit double. * @return the 64 bit double read. * @exception IOException If other I/O error has occurred. */ public double readDouble() throws IOException { return instream.readDouble(); } /** * Reads from the stream until an end of line is reached. * @return the characters in the line * @exception IOException If other I/O error has occurred. */ public String readLine() throws IOException { return instream.readLine(); } /** * Reads a UTF format String * @return the UTF format String read. * @exception IOException If I/O error has occurred. */ public String readUTF() throws IOException { return instream.readUTF(); } }