Top Banner
Lab 2 Data Streams
21

Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

Lab 2

Data Streams

Page 2: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

Lab 2: Data Streams

Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams

Page 3: 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.

Page 4: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

Byte-level communication is represented in Java by data streams.

1000110110001101

A Data Stream

Provided that the data stream is constructed correctly, what goes in one end comes out the other.

Page 5: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

Streams may be chained together, to provide additional functionality and an easier and more manageable interface.

'a'111101

Two Data Streams

Page 6: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

Streams are divided into two categories: Input streams

Output streams

111101

111101

Page 7: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

Certainly, you should not try to read data from an output stream or write data to an input stream.

Page 8: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

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

Page 9: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

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

Page 10: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

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.

Page 11: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

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.

Page 12: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

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.

Page 13: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

Like input streams, data is communicated sequentially; the first byte in will be the first byte out.

Page 14: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

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.

Page 15: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

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

Page 16: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

Examples of Filter Output Streams

BufferedOutputStreamUses 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.

Page 17: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

DataOutputStreamWrites primitive data types, such as an int,

float, a double, or even a line of text, to an output stream.

PrintStreamOffers 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.

Page 18: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

Example:FileOutputStream fout;DataOutputStream dos;fout = new FileOutputStream("out");dos = new DataOutputStream(fout);dos.writeInt(1024);dos.writeFloat(43.235);

1024 FileOutputStream

DataOutputStream

Fileint bytes

Page 19: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

Examples of Filter Input Streams

BufferedInputStreamUses 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.

Page 20: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

DataInputStreamReads primitive data types, such as an int,

float, a double, or even a line of text, from an input stream.

Page 21: Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

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);

1024FileInputStream

DataInputStream

Fileint bytes