The Java I/O System
The Java I/O System
The Java I/O System
InputStream OutputStream
Reader Writer
Object Serialization
[Source: java.sun.com]
Streams in Java
There is a huge (and complicated) hierarchy of stream classes in
Java.
Reader, root in unicode input hierarchy Writer, root in unicode output hierarchy InputStream, root in binary input hierarchy OutputStream, root in binary output hierarchy
component
ConcretComponent
operation()
Decorator operation()
//addOpr(); component.operation()
ConcreteDecorator1
ConcreteDecorator2
operation() addState
OOP: The Java I/O System
operation() addOpr()
What are you talking to (n). The way you are talking to it (m).
Solution no. 1
Make a class for every combination n * m classes, not flexible, hard to extend Java filter streams (decorators) are added dynamically to create the functionality needed. n + m classes Input decorator: FilterInputStream Output decorator: FilterOutputStream
6
Solutions no. 2
InputStream Hierarchy
[Source: java.sun.com]
InputStream, the abstract component root in decorator pattern FileInputStream, etc. the concrete components FilterInputStream, the abstract decorator LineNumberInputStream, DataInputStream, etc. concrete decorators
7
OutputStream Hierarchy
[Source: java.sun.com]
OutputStream, the abstract component root in decorator pattern FileOutputStream, etc. the concrete components FilterOutputStream, the abstract decorator PrintStream, DataOutputStream, etc. concrete decorators
8
InputStream Types
Type of InputStream Reads From ByteArrayInputStream Block of memory StringBufferInputStream String (note not StringBuffer) PipedInputStream Pipe (in another thread) FileInputStream File SequencedInputStream Combines InputStreams ObjectInputStream Objects from an InputStream
Concrete Components
OutputStream Types
Type of OutputStream ByteArrayOutputStream PipedOutputStream FileOutputStream ObjectOutputStream
Reads From Block of memory Pipe (in another thread) File Objects to a OutputStream
Concrete Components
10
FilterInputStream
DataInputStream
Full interface for reading built-in types For portable reading of data between different OS platforms Adds buffering to the stream (do this by default) Only adds line numbers One-character push pack for scanners (lexers)
BufferedInputStream
LineNumberInputStream
PushbackInputStream
Concrete Decorators
11
FilterOutputStream
DataOutputStream
Full interface for writing built-in types For portable writing of data between different OS platforms Example: System.out.println Allows primitive formatting of data for display (not printf!) Not for storage use DataOutputStream for this Adds buffering to output (do this by default!)
PrintStream
BufferedOutputStream
Concrete Decorators
12
OutputStream, Example
import java.io.*; // [Source: java.sun.com] public class DataIODemo { public static void main(String[] args) throws IOException { // where to write to DataOutputStream out = new DataOutputStream( new FileOutputStream("invoice1.txt")); // alternative also using a buffer decorator DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream("invoice1.txt")));
13
InputStream, Example
// read it in again DataInputStream in = new DataInputStream( new FileInputStream("invoice1.txt")); // alternative also using a buffer decorator DataInputStream in = new DataInputStream( new BufferedInputStream ( new FileInputStream("invoice1.txt"))); double price; int unit; StringBuffer desc; double total = 0.0;
15
16
17
Reader, the abstract component root in decorator pattern BufferedReader, etc. the concrete components FilterReader, the abstract decorator PushbackReader, concrete decorators
[Source: java.sun.com]
18
[Source: java.sun.com]
Writer, the abstract component root in decorator pattern BufferedWriter, etc. the concrete components FilterWriter, the abstract decorator No concrete decorators
19
PipedReader, PipedWriter
FileReader, FileWriter
Reads platform characters and delivers Unicode characters to the Java program. Writes Unicode characters to platform dependent characters. Writes Java primitive data types to file.
OutputStreamWriter
PrintWriter
21
Reader
convert: InputStreamReader Writer convert: OutputStreamWriter FileReader FileWriter StringReader (better name) StringWriter CharArrayReader CharArrayWriter PipedReader PipedWriter
23
24
Path separators Permissions in Unix Directories on the Mac Drive letters on Windows To make Java program platform independent.
25
26
27
Object Serialization
Very hard to do in other programming languages!!! Class must implement the Serializable interface Uses
Output: ObjectOutputStream
writeObject() readObject()
Input: ObjectInputStream
All relevant parts (the web of object) are serialized. Lightweight persistence
29
// Read an object from disk ObjectInputStream in = new ObjectInputStream( new FileInputStream("mycars.dat")); Car myToyota = (Car)in.readObject();
30
Summary
Streams a large class hierarchy for input and output.
The decorator pattern is the key to understanding it Very flexible, but requires extra coding in clients. This is annoying
31
FilterStream, Example
import java.io.*; class StreamFilterExample{ public static void main(String[] args) throws IOException { DataInputStream din = new DataInputStream( new BufferedInputStream( new FileInputStream( new File("numbers.dat")))); int i; boolean b; i = din.readInt(); b = din.readBoolean(); System.out.println("i = " + i + ". b = " + b); din.close(); } }
32