Learning Objectives
After completing this session, you will be able to:
Define an I/O stream
Identify the types of streams
Explain the Stream class hierarchy
I/O Streams
An I/O Stream represents an input source or an output destination.
A stream can represent many different kinds of sources and destinations like disk files,
devices, other programs, a network socket, and memory arrays.
Streams support many different kinds of data like simple bytes, primitive data types,
localized characters, and objects.
Some streams simply pass on data, others manipulate and transform the data in
useful ways.
No matter how they work internally, all streams present the same simple model to
programs that use them. A stream is a sequence of data.
Input Stream
A program uses an input stream to read data from a source, one item at a time.
Output Stream
A program uses an output stream to write data to a destination, one item at time.
General Stream Types
The general stream types are:
Character and Byte Streams: Character streams are the streams that read and write
16-bit characters whereas Byte streams are the streams that read and write 8-bit
bytes.
Input and Output Streams: Based on source or destination
Node and Filter Streams: Whether the data on a stream is manipulated or transformed
or not.
Character and Byte Streams
Byte streams:For binary data
Root classes for byte streams:
oThe InputStream class
oThe OutputStream class
oBoth classes are abstract
Character streams:For Unicode characters
Root classes for character streams:
oThe Reader class
oThe Writer class
oBoth classes are abstract
Input and Output Streams
Input or source streams: Can read from these streams
Root classes of all input streams:
oThe InputStream class
oThe Reader class
Output or sink (destination) streams: Can write to these streams
Root classes of all output streams:
oThe OutputStream class
oThe Writer class
Node and Filter Streams
Node streams (Data sink stream):Contain the basic functionality of reading or
writing from a specific location
Types of node streams include files, memory, and pipes
Filter streams (Processing stream):Layered onto node streams between threads or
processes
For additional functionality like altering or managing data in the stream
Adding layers to a node stream is called stream chaining
Streams
Streams are shown in the following diagram:
Tips and Tricks:
How many bits are used torepresent Unicode, ASCII, UTF-16, and UTF-8 characters?
What is the default character encoding of your platform?
Solution:
Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set
uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using
8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.
If you are running Java on English Windows platforms, then it is probably Cp1252. If
you are running Java on English Solaris platforms, then it is most likely 8859_1.
Summary
The central building block of the Java I/O framework is the stream. “A stream is a
flowing sequence of characters”.
In other words, a stream is usually considered to be an abstraction for the capability to
move bytes from a source to a sink.
Input operations begin by openinga stream from the source and using a read()
method to obtain the data via the stream. Similarly, output operations begin by
opening a stream to the destination and using a write() method to send the data.
The package named java.iocontains a set of input and output stream classes that can
be used to read and write data.
The java.iopackage also provides specialized InputStream and OutputStream
subclasses that are used for specialized types of input and output.
The InputStream class and OutputStream class are abstract superclasses that define
the behavior for sequential input and output streams in Java.
Classes in java.iopackage are designed to be “chained” or “wrapped”. (This is a
common use of the decorator design pattern.)
Test Your Understanding
1.What is a ‘stream’?
2.Name some important input and output stream classes.
3.OutputStream and InputStreamclasses serve as the base classes for all byte stream
classes. Comment on this statement.
After completing this session, you will be able to:
Define an I/O stream
Identify the types of streams
Explain the Stream class hierarchy
I/O Streams
An I/O Stream represents an input source or an output destination.
A stream can represent many different kinds of sources and destinations like disk files,
devices, other programs, a network socket, and memory arrays.
Streams support many different kinds of data like simple bytes, primitive data types,
localized characters, and objects.
Some streams simply pass on data, others manipulate and transform the data in
useful ways.
No matter how they work internally, all streams present the same simple model to
programs that use them. A stream is a sequence of data.
Input Stream
A program uses an input stream to read data from a source, one item at a time.
Output Stream
A program uses an output stream to write data to a destination, one item at time.
General Stream Types
The general stream types are:
Character and Byte Streams: Character streams are the streams that read and write
16-bit characters whereas Byte streams are the streams that read and write 8-bit
bytes.
Input and Output Streams: Based on source or destination
Node and Filter Streams: Whether the data on a stream is manipulated or transformed
or not.
Character and Byte Streams
Byte streams:For binary data
Root classes for byte streams:
oThe InputStream class
oThe OutputStream class
oBoth classes are abstract
Character streams:For Unicode characters
Root classes for character streams:
oThe Reader class
oThe Writer class
oBoth classes are abstract
Input and Output Streams
Input or source streams: Can read from these streams
Root classes of all input streams:
oThe InputStream class
oThe Reader class
Output or sink (destination) streams: Can write to these streams
Root classes of all output streams:
oThe OutputStream class
oThe Writer class
Node and Filter Streams
Node streams (Data sink stream):Contain the basic functionality of reading or
writing from a specific location
Types of node streams include files, memory, and pipes
Filter streams (Processing stream):Layered onto node streams between threads or
processes
For additional functionality like altering or managing data in the stream
Adding layers to a node stream is called stream chaining
Streams
Streams are shown in the following diagram:
Tips and Tricks:
How many bits are used torepresent Unicode, ASCII, UTF-16, and UTF-8 characters?
What is the default character encoding of your platform?
Solution:
Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set
uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using
8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.
If you are running Java on English Windows platforms, then it is probably Cp1252. If
you are running Java on English Solaris platforms, then it is most likely 8859_1.
Summary
The central building block of the Java I/O framework is the stream. “A stream is a
flowing sequence of characters”.
In other words, a stream is usually considered to be an abstraction for the capability to
move bytes from a source to a sink.
Input operations begin by openinga stream from the source and using a read()
method to obtain the data via the stream. Similarly, output operations begin by
opening a stream to the destination and using a write() method to send the data.
The package named java.iocontains a set of input and output stream classes that can
be used to read and write data.
The java.iopackage also provides specialized InputStream and OutputStream
subclasses that are used for specialized types of input and output.
The InputStream class and OutputStream class are abstract superclasses that define
the behavior for sequential input and output streams in Java.
Classes in java.iopackage are designed to be “chained” or “wrapped”. (This is a
common use of the decorator design pattern.)
Test Your Understanding
1.What is a ‘stream’?
2.Name some important input and output stream classes.
3.OutputStream and InputStreamclasses serve as the base classes for all byte stream
classes. Comment on this statement.
No comments:
Post a Comment