squint
Class DataInputChannel

java.lang.Object
  extended by java.io.InputStream
      extended by squint.DataInputChannel
All Implemented Interfaces:
java.io.Closeable, java.io.DataInput

public class DataInputChannel
extends java.io.InputStream
implements java.io.DataInput

A DataInputChannel is a stream designed to provide mechanisms to access data stored or transmitted using distinct binary encoding schemes for various types of data (i.e. not just ASCII or UniCode). Objects of this class are used by the TCPConnection class for the "in" stream associated with a TCPConnection. It is also possible to create a DataInputStream that provides access to data stored in a disk file. The interface to a DataInputChannel is desiged to be very similar to the interface to a standard DataInputStream, except that the methods of this class never throw checked exceptions. This makes it possible to use this class without inserting try/catch constructs around code that uses the class. In many cases, more complete descriptions of the behavior of the methods of this class can be found in the descriptions of the corresponding methods of the standard Java DataInput interface.


Constructor Summary
DataInputChannel(java.io.File f)
          Create a DataInputStream to access the data stored within a file given a File object describing the desired file.
DataInputChannel(java.io.InputStream source)
          Create a DataInputStream given an InputStream to use to actually perform data transfer.
DataInputChannel(java.lang.String fileName)
          Create a DataInputStream given a pathname within the local file system that identifies the desired file.
 
Method Summary
 int available()
          Determine how many bytes/characters of data are currently available through a channel.
 void close()
          Close the channel.
 int read()
          Access the next byte of data available through the channel.
 int read(byte[] buffer)
          Access any currently available bytes of data received through the channel.
 int read(byte[] buffer, int off, int len)
          Access any currently available bytes of data received through the channel.
 boolean readBoolean()
          Access the next byte of data available through the channel treating it as a boolean value.
 byte readByte()
          Access the next byte of data available through the channel.
 char readChar()
          Access the next character of data available through the channel.
 double readDouble()
          Access the next double value available through the channel.
 float readFloat()
          Access the next float value available through the channel.
 void readFully(byte[] buffer)
          Read some bytes from an input channel and store them into the array buffer.
 void readFully(byte[] buffer, int off, int len)
          Read some bytes from an input channel and store them into the array buffer.
 int readInt()
          Access the next 4 byte int value available through the channel.
 java.lang.String readLine()
          Read a line of text from the input channel treating each byte as an ASCII character.
 long readLong()
          Access the next 8 byte integer value available through the channel.
 short readShort()
          Access the next 2 byte integer value available through the channel.
 java.lang.String readString()
          Access any currently available characters of data received through the channel.
 java.lang.String readString(int desired)
          Access any currently available characters of data received through the channel.
 int readUnsignedByte()
          Access the next byte of data available through the channel.
 int readUnsignedShort()
          Access the next 2 byte integer value available through the channel.
 java.lang.String readUTF()
          Read a string encoded in modified UTF-8 format.
 int skipBytes(int len)
          Skip a specified number of bytes of incoming data .
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataInputChannel

public DataInputChannel(java.io.File f)
Create a DataInputStream to access the data stored within a file given a File object describing the desired file.

Parameters:
f - the file to be accessed.

DataInputChannel

public DataInputChannel(java.lang.String fileName)
Create a DataInputStream given a pathname within the local file system that identifies the desired file. Since the file name provided must be a complete path, in most situations the programmer will want to use a JFileChooser or similar mechanism to deteremine the full path name associated with a file selected by the user.

Parameters:
fileName - the full path name of the file to be accessed through the stream.

DataInputChannel

public DataInputChannel(java.io.InputStream source)
Create a DataInputStream given an InputStream to use to actually perform data transfer. This constructor is used to create the "in" streams associated with TCPConnections. While it can be use in other contexts, it is more likely that novice programmers will use one of the other constructors provided with this class.

Parameters:
source - the underlying InputStream through which the data accessed will pass.
Method Detail

close

public void close()
Close the channel.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream

skipBytes

public int skipBytes(int len)
Skip a specified number of bytes of incoming data .

Specified by:
skipBytes in interface java.io.DataInput
Parameters:
len - the number of bytes of data to skip
Returns:
the actual number of bytes skipped (possibly 0).

available

public int available()
Determine how many bytes/characters of data are currently available through a channel.

Overrides:
available in class java.io.InputStream
Returns:
the number of bytes of data available.

readLine

public java.lang.String readLine()
Read a line of text from the input channel treating each byte as an ASCII character. See the general contract of the readLine method of DataInput for details

Specified by:
readLine in interface java.io.DataInput
Returns:
a string containing the next line data available through the channel or null if no additional input is available.

readString

public java.lang.String readString(int desired)
Access any currently available characters of data received through the channel.

Parameters:
desired - an upper limit on the number of bytes/characters of data desired. The method may return fewer bytes if the desired number are not currently available.
Returns:
a string containing the next bytes of data available through the channel or null if no additional input is available.

readString

public java.lang.String readString()
Access any currently available characters of data received through the channel.

Returns:
a string containing the next bytes of data available through the channel or null if no additional input is available.

readByte

public byte readByte()
Access the next byte of data available through the channel.

Specified by:
readByte in interface java.io.DataInput
Returns:
a single byte of data.

readChar

public char readChar()
Access the next character of data available through the channel.

Specified by:
readChar in interface java.io.DataInput
Returns:
a single character of data.

readDouble

public double readDouble()
Access the next double value available through the channel.

Specified by:
readDouble in interface java.io.DataInput
Returns:
a single double of data.

readFloat

public float readFloat()
Access the next float value available through the channel.

Specified by:
readFloat in interface java.io.DataInput
Returns:
a single float of data.

readBoolean

public boolean readBoolean()
Access the next byte of data available through the channel treating it as a boolean value.

Specified by:
readBoolean in interface java.io.DataInput
Returns:
a single boolean value.

readInt

public int readInt()
Access the next 4 byte int value available through the channel.

Specified by:
readInt in interface java.io.DataInput
Returns:
a single int of data.

readShort

public short readShort()
Access the next 2 byte integer value available through the channel.

Specified by:
readShort in interface java.io.DataInput
Returns:
a single 2 byte integer (short) of data.

readLong

public long readLong()
Access the next 8 byte integer value available through the channel.

Specified by:
readLong in interface java.io.DataInput
Returns:
a single 8 byte integer (long) of data.

readUnsignedByte

public int readUnsignedByte()
Access the next byte of data available through the channel.

Specified by:
readUnsignedByte in interface java.io.DataInput
Returns:
a single byte of data.

readUnsignedShort

public int readUnsignedShort()
Access the next 2 byte integer value available through the channel.

Specified by:
readUnsignedShort in interface java.io.DataInput
Returns:
a single 2 byte integer (short) of data.

read

public int read()
Access the next byte of data available through the channel.

Specified by:
read in class java.io.InputStream
Returns:
a single byte of data.

read

public int read(byte[] buffer)
Access any currently available bytes of data received through the channel.

Overrides:
read in class java.io.InputStream
Parameters:
buffer - An array to hold the data actually retrieved.
Returns:
the number of bytes placed in the parameter array, or -1 if no additional data was available.

read

public int read(byte[] buffer,
                int off,
                int len)
Access any currently available bytes of data received through the channel.

Overrides:
read in class java.io.InputStream
Parameters:
buffer - An array to hold the data actually retrieved.
off - the offset within the buffer array at which the first byte should be placed
len - a limit on the number of bytes to be retrieved
Returns:
the number of bytes placed in the parameter array, or -1 if no additional data was available.

readFully

public void readFully(byte[] buffer)
Read some bytes from an input channel and store them into the array buffer. See the general contract of the readFully method of DataInput for details

Specified by:
readFully in interface java.io.DataInput
Parameters:
buffer - a byte array in which the data obtained should be placed

readFully

public void readFully(byte[] buffer,
                      int off,
                      int len)
Read some bytes from an input channel and store them into the array buffer. See the general contract of the readFully method of DataInput for details

Specified by:
readFully in interface java.io.DataInput
Parameters:
buffer - a byte array in which the data obtained should be placed
off - offset within the buffer array at which first byte should be placed
len - number of bytes to read

readUTF

public java.lang.String readUTF()
Read a string encoded in modified UTF-8 format. See the general contract of the readUTF method of DataInput for details

Specified by:
readUTF in interface java.io.DataInput