JAVA STREAMS
Streams provide the data transfer mechanism on top of sockets. To use a telephone structure, sockets are like the telephone connection and streams are like voice exchanges. When you speak to a friend on the telephone, you create an output stream; your voice is picked up on the receiving end by an input stream in this case, your friend’s ear. You simply write data in one end of a stream and the recipient reads it from the other end. You will find that it is typically easier to write to a stream than to read from it. The writer simply writes to the stream and puts the data in the stream. The reader, on the other hand, must be prepared to deal with slow writers without blocking an entire program waiting for data to arrive. The reader must also be able to detect the end of a stream. The java.io package provides a large number of classes most are derived from the InputStream or OutputStream abstract classes. Stream classes are almost always paired there is usually a corresponding output stream for each input stream type. Figure 10-8 shows the stream classes in java.io.
The Socket class provides two methods that let you associate a socket with a pair of streams; getOutputStream and getInputStream. The first method returns an OutputStream object, and the second returns an InputStream object. You can then extend these objects to support any of the specialized input/output stream pairs we show in Figure 10-8.
The Java Output Stream Classes
Figure 10-9 shows the output stream class hierarchy. All the output stream classes are derived from the OutputStream abstract class. The two important classes are DataOutputStream and BufferedOutputStream. They are both filtering streams derived from FilterOutputStream.
OutputStream is the abstract class that defines the basic output methods that all output stream classes must provide. You invoke write to write a single byte or an array or subarray of bytes. You invoke flush to force any buffered data to be sent. Finally, you invoke close to close the stream and free up the system resources it owns
FilterOutputStream is the class that lets you chain together and filter output streams. The class simply overrides all methods of OutputStream with versions that pass all requests to the underlying output stream. Subclasses of FilterOutputStream may further override some of these methods as well as provide additional methods and fields.
DataOutputStream is the class that implements a filtered stream enabling you to write Java primitive data types to an output stream in their binary representations. A recipient application uses a DataInputStream to read the data back into Java. You invoke the constructor DataOutputStream to create a data output stream filter; you must specify the OutputStream object to be filtered. You invoke size to obtain the number of bytes you wrote so far. You invoke write to write a single byte. You can write multiple bytes by specifying an offset within a buffer and the number of bytes to write. The class provides methods for writing all the basic Java types to a stream in their binary representation. These methods are self-explanatory except for writeUTF. You invoke writeUTF to write a Java string of Unicode characters using an ASCII compatible encoding scheme called UTF-8.
BufferedOutputStream is the class that implements a buffered output stream filter. Instead of calling the stream for each byte you write, it stores the data in a buffer. It then writes the data to the output stream if the buffer reaches its capacity. You can explicitly flush a buffer’s contents by using flush or close. You invoke the constructor BufferedOutputStream to create a buffered stream filter; you must specify the OutputStream object to be filtered. The constructor provides a 512-byte default buffer that you can optionally override with a size you specify. You invoke write to write a single byte; you can write multiple bytes by specifying an offset within a buffer and the number of bytes to write. A buffered filter gives you better performance; it lets you write data to streams in chunks instead of writing it one byte at a time.
Here are the class’s Methods descriptions for OutputStream, FilterOutputStream, DataOutputStream, and the BufferedOutputStream:
Class OutputStream | |
OutputStream() | |
Void close() | Closes this output stream and releases any system resources associated with this stream. |
Void flush() | Flushes this output stream and forces any buffered output bytes to be written out. |
Void write(byte[] b) | Writes b.length bytes from the specified byte array to this output stream. |
Void write(byte[] b, int off, int len) | Writes len bytes from the specified byte array starting at offset off to this output stream. |
abstract void write(int b) | Writes the specified byte to this output stream. |
Listing 10-7: The OutputStream Class
Class FilterOutputStream | |
FilterOutputStream(OutputStream out) | Creates an output stream filter built on top of the specified underlying output stream. |
Void close() | Closes this output stream and releases any system resources associated with the stream |
Void flush() | Flushes this output stream and forces any buffered output bytes to be written out to the stream. |
Void write(byte[] b) | Writes b.length bytes to this output stream. |
Void write(byte[] b, int off, int len) | Writes len bytes from the specified byte array starting at offset off to this output stream. |
Void write(int b) | Writes the specified byte to this output stream. |
Listing 10-8: The FilterOutputStream Class
Class DataOutputStream | |
DataOutputStream(OutputStream out) | Creates a new data output stream to write data to the specified underlying output stream. |
Void flush() | Flushes this data output stream. |
Int size() | Returns the current value of the counter written, the number of bytes written to this data output stream so far. |
Void write(byte[] b, int off, int len) | Writes len bytes from the specified byte array starting at offset off to the underlying output stream. |
Void write(int b) | Writes the specified byte (the low eight bits of the argument b) to the underlying output stream. |
Void writeBoolean(boolean v) | Writes a boolean to the underlying output stream as a 1-byte value. |
Void writeByte(int v) | Writes out a byte to the underlying output stream as a 1-byte value. |
Void writeBytes(String s) | Writes out the string to the underlying output stream as a sequence of bytes. |
Void writeChar(int v) | Writes a char to the underlying output stream as a 2-byte value, high byte first. |
Void writeChars(String s) | Writes a string to the underlying output stream as a sequence of characters. |
Void writeDouble(double v) | Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first. |
Void writeFloat(float v) | Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first. |
Void writeInt(int v) | Writes an int to the underlying output stream as four bytes, high byte first. |
Void writeLong(long v) | Writes a long to the underlying output stream as eight bytes, high byte first. |
Void writeShort(int v) | Writes a short to the underlying output stream as two bytes, high byte first. |
Void writeUTF(String str) | Writes a string to the underlying output stream using Java modified UTF-8 encoding in a machine-independent manner. |
Listing 10-9: The DataOutputStream Class
Class BufferedOutputStream | |
BufferedOutputStream(OutputStream out) | Creates a new buffered output stream to write data to the specified underlying output stream with a default 512-byte buffer size. |
BufferedOutputStream(OutputStream out, int size) | Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size. |
Void flush() | Flushes this buffered output stream. |
Void write(byte[] b, int off, int len) | Writes len bytes from the specified byte array starting at offset off to this buffered output stream. |
Void write(int b) | Writes the specified byte to this buffered output stream. |
Listing 10-10: The BufferedOutputStream Class
The Java InputStream Class
Figure 10-10 shows the input stream class hierarchy. All the input stream classes are derived from the InputStream abstract class. The two classes that interest us the most are DataInputStream and BufferedInputStream. They are both filtering streams derived from FilterInputStream.
InputStream is the abstract class that defines the basic input methods that all input stream classes must provide. You invoke read to read the next byte from the stream. If you pass read an array, it will fill it as much as possible with data from the stream and then return the number of bytes it actually read. You can also specify an offset to start reading from, as well as the number of bytes you want to read. You invoke available to obtain the number of bytes that are already in the stream. You invoke skip to skip over the next number of bytes within a stream. You invoke mark to mark the current position within a stream; you can specify the number of bytes to be read before the mark position is invalidated. You can subsequently invoke reset to reposition the stream to the marked position. You invoke markSupported to check if the stream supports marking; it will return true if it does. Finally, you invoke close to close the stream and free up the system resources it consumes.
FilterInputStream is the class that simply overrides all methods of InputStream with versions that pass all requests to the underlying input stream. Subclasses of FilterInputStream may further override some of these methods as well as provide additional methods and fields.
DataInputStream Is the class that implements a filtered stream that lets you read Java primitive data types from an input stream. You invoke the constructor DataInpntStream to create a data input stream filter; you must specify the InputStream object to be filtered. You invoke read to read a single byte. You can read multiple bytes by specifying a buffer and the number of bytes to read. The method will block until some input is available; it will then return the number of bytes actually read. You invoke readFully to read data into an array of bytes; it will block until all the data is available. The class also provides methods for reading all the basic Java types from a stream in their binary representation. These methods are self-explanatory, except for two readLine and readUTF. The readLine method reads characters from a stream until it encounters a newline or a carriage return. The readUTF method reads a Java string of Unicode text encoded in the UTF-8 format.
BufferedInputStream is the class that implements a buffered input stream filter. It reads in data and stores it in a buffer. When you request data, it is usually available in the buffer. The idea is to block data fetches and avoid dipping into the stream for each byte you request. You invoke the constructor BufferedInputStream to create a new buffered input stream filter; you must pass it the InputStream object to be filtered and an optional buffer size the default is 512 bytes. You invoke read to read a single byte, or you can read multiple bytes by specifying an offset within a buffer and the number of bytes to read. Again, the reason we’re doing all this is to improve stream performance by reading in chunks of data instead of reading in one byte at a time.
Here are the class declarations for InputStream, FilterInputStream, DataInputStream and BufferedlnputStream:
Class InputStream | |
InputStream() | |
Int available() | Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. |
Void close() | Closes this input stream and releases any system resources associated with the stream. |
Void mark(int readlimit) | Marks the current position in this input stream. |
Boolean markSupported() | Tests if this input stream supports the mark and reset methods. |
abstract int read() | Reads the next byte of data from the input stream. |
Int read(byte[] b) | Reads some number of bytes from the input stream and stores them into the buffer array b. |
Int read(byte[] b, int off, int len) | Reads up to len bytes of data from the input stream into an array of bytes. |
Void reset() | Repositions this stream to the position at the time the mark method was last called on this input stream. |
Long skip(long n) | Skips over and discards n bytes of data from this input stream. |
Listing 10-11: The InputStream Class
Class FilterInputStream | |
FilterInputStream(InputStream in) | Creates a FilterInputStream by assigning the argument in to the field this.in so as to remember it for later use. |
Int available() | Returns the number of bytes that can be read from this input stream without blocking. |
Void close() | Closes this input stream and releases any system resources associated with the stream. |
Void mark(int readlimit) | Marks the current position in this input stream. |
Boolean markSupported() | Tests if this input stream supports the mark and reset methods. |
Int read() | Reads the next byte of data from this input stream. |
Int read(byte[] b) | Reads up to byte.length bytes of data from this input stream into an array of bytes. |
Int read(byte[] b, int off, int len) | Reads up to len bytes of data from this input stream into an array of bytes. |
Void reset() | Repositions this stream to the position at the time the mark method was last called on this input stream. |
Long skip(long n) | Skips over and discards n bytes of data from the input stream. |
Listing 10-12: The FilterInputStream Class
Class DataInputStream | |
DataInputStream(InputStream in) | Creates a DataInputStream that uses the specified underlying InputStream. |
Int read(byte[] b) | Reads some number of bytes from the contained input stream and stores them into the buffer array b. |
Int read(byte[] b, int off, int len) | Reads up to len bytes of data from the contained input stream into an array of bytes. |
Boolean readBoolean() | See the general contract of the readBoolean method of DataInput. |
Byte readByte() | See the general contract of the readByte method of DataInput. |
Char readChar() | See the general contract of the readChar method of DataInput. |
Double readDouble() | See the general contract of the readDouble method of DataInput. |
Float readFloat() | See the general contract of the readFloat method of DataInput. |
Void readFully(byte[] b) | See the general contract of the readFully method of DataInput. |
Void readFully(byte[] b, int off, int len) | See the general contract of the readFully method of DataInput. |
Int readInt() | See the general contract of the readInt method of DataInput. |
String readLine() | Deprecated. |
Long readLong() | See the general contract of the readLong method of DataInput. |
Short readShort() | See the general contract of the readShort method of DataInput. |
Int readUnsignedByte() | See the general contract of the readUnsignedByte method of DataInput. |
Int readUnsignedShort() | See the general contract of the readUnsignedShort method of DataInput. |
String readUTF() | See the general contract of the readUTF method of DataInput. |
static String readUTF(DataInput in) | Reads from the stream in a representation of a Unicode character string encoded in Java modified UTF-8 format; this string of characters is then returned as a String. |
Int skipBytes(int n) | See the general contract of the skipBytes method of DataInput. |
Listing 10-13: The DataInputStream Class
Class BufferedInputStream | |
BufferedInputStream(InputStream in) | Creates a BufferedInputStream and saves its argument, the input stream in, for later use. |
BufferedInputStream(InputStream in, int size) | Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use. |
Int available() | Returns the number of bytes that can be read from this input stream without blocking. |
Void close() | Closes this input stream and releases any system resources associated with the stream. |
Void mark(int readlimit) | See the general contract of the mark method of InputStream. |
Boolean markSupported() | Tests if this input stream supports the mark and reset methods. |
Int read() | See the general contract of the read method of InputStream. |
Int read(byte[] b, int off, int len) | Reads bytes from this byte-input stream into the specified byte array, starting at the given offset. |
Void reset() | See the general contract of the reset method of InputStream. |
Long skip(long n) | See the general contract of the skip method of InputStream. |
Listing 10-14: The BufferedInputStream Class