210 likes | 241 Views
Lab 2. Data Streams. Lab 2: Data Streams. Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams. Introduction. I/O Streams are commonly used in Java for communicating over networks, with files, and between applications.
E N D
Lab 2 Data Streams
Lab 2: Data Streams • Introduction • Reading from an Input Stream • Writing to an Output Stream • Filter Streams
Introduction • I/O Streams are commonly used in Java for communicating over networks, with files, and between applications. • Almost all network communication (except UDP communication) is conducted over streams. • Hence, a thorough knowledge of I/O streams is critical for network programming in Java.
10001101 10001101 A Data Stream • Byte-level communication is represented in Java by data streams. • Provided that the data stream is constructed correctly, what goes in one end comes out the other.
111101 'a' Two Data Streams • Streams may be chained together, to provide additional functionality and an easier and more manageable interface.
111101 111101 • Streams are divided into two categories: • Input streams • Output streams
Certainly, you should not try to read data from an output stream or write data to an input stream.
Reading from an Input Stream • Six important low-level input streams: • ByteArrayInputStream • Reads bytes of data from an in-memory array • FileInputStream • Reads bytes of data from a file on the local file system • PipedInputStream • Reads bytes of data from a thread pipe
StringBufferInputStream • Reads bytes of data from a string • SequenceInputStream • Reads bytes of data from two or more low-level streams, switching from one stream to the next when the end of the stream is reached. • System.in • Reads bytes of data from the user console
Blocking I/O is used when reading from an input stream. • Blocking I/O is a term applied to any form of input or output that does not immediately return from an operation. • In certain situations, blocking I/O can cause performance problems. This can be alleviated by using data buffering.
Writing to an Output Stream • Six important low-level output streams: • ByteArrayOutputStream • Writes bytes of data to an array of bytes • FileOutputStream • Writes bytes of data to a file on the local file system • PipedOutputStream • Writes bytes of data to a a communications pipe, which will be connected to a PipedInputStream.
StringBufferOutputStream • Writes bytes to a string buffer • System.err • Writes bytes of data to the error stream of the user console, also known as standard error. • System.in • Writes bytes of data to the user console, also known as standard output.
Like input streams, data is communicated sequentially; the first byte in will be the first byte out.
Filter Streams • The basic low-level streams have limited flexibility. • Filter streams add additional functionality to an existing stream. For example, allowing one to read a line of text instead of reading byte by byte.
Filter streams can be connected to any other stream (low-level stream or another filter stream). • Filter stream classes extend from java.io.FilterInputStream or java.io.FilterOutputStream
Examples of Filter Output Streams • BufferedOutputStream • Uses I/O buffering for output to improve system performance. • Outputs to an internal buffer. Buffer contents are dumped to the output stream when it is full or flushed.
DataOutputStream • Writes primitive data types, such as an int, float, a double, or even a line of text, to an output stream. • PrintStream • Offers additional methods for writing lines of text, and other datatypes as text. • Provides a convenient way to print primitive datatypes as text using the print() and println() method.
Example: FileOutputStream fout; DataOutputStream dos; fout = new FileOutputStream("out"); dos = new DataOutputStream(fout); dos.writeInt(1024); dos.writeFloat(43.235); DataOutput Stream FileOutput Stream 1024 File int bytes
Examples of Filter Input Streams • BufferedInputStream • Uses I/O buffering for input to improve system performance. • Tries to reduce the number of times an application blocks for input by reading bytes in batches into a buffer.
DataInputStream • Reads primitive data types, such as an int, float, a double, or even a line of text, from an input stream.
Example: FileInputStream fin; DataInputStream dis; fin = new FileInputStream("out"); dis = new DataInputStream(fin); int intData = dis.readInt(); float floatData = dis.readFloat(); System.out.println("Int data: "+intData); System.out.println("Float data: "+floatData); DataInput Stream FileInput Stream 1024 File int bytes