JAVA.
IO
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
What is [Link]?
• JAVA represents everything as Objects
• [Link] is set of Objects that are an
abstraction for streaming system resources
such as files
• [Link] also defines input and output
mechanisms for things other than files
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
1
Streams
• Streams are the "fundamental element" of
the [Link] package
• The simplest streams are the abstract
classes InputStream and OutputStream
• You cannot use them directly
• They define i/o in terms of bytes
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
File Streams
• FileInputStream
FileInputStream Fptr =
new FileInputStream("/etc/passed");
int x = [Link]();
[Link];
• FileOutputStream
FileOutputStream Fptr =
new FileOutputStream("/tmp/blah");
[Link](64);
[Link];
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
2
Filtered Streams
• These take a stream as input during the
constructor and add functionality:
FileInputStream Fptr = new FileInputStream("/etc/passwd");
FilteredInputStream FIS = new FilteredInputStream(Fptr);
int x = [Link]();
[Link]();
[Link]();
• You would never actually use
Filtered[In|Out]putStream directly... you
would use classes that extend it
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Buffered[In|Out]putStream
• Buffered Streams cache operations...
• Devices perform better when working with blocks
• Use memory as a "buffer" for the i/o
• Input:
• A large block of the input is read ahead of time and
stored until needed in the buffer memory
• The stream can be reversed to a previous state
provided that the desired state is still in the buffer
• Output:
• Writes are not committed immediately
• Use flush if things need to be committed right away
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
3
Checksums
• A checksum is a first order approximation to
to the problem of verifying data integrity
• Parity count:
0100 0100 1101 0100 :: 1
0111 1111 0000 0101 :: 0
• If the receiver gets the new message with a
single bit "wrong" we know there was a
problem in transit
• If two bits are wrong, we may not detect it
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Checked[In|Out]putStream
• Checksum CK = new CheckSum();
CheckedOutputStream COS =
new CheckedOutputStream(CK, Fptr);
[Link](x);
• CheckSum CK = new CheckSum
CheckedInputStream CIS =
new CheckInputStream(CK, Fptr);
x = [Link]();
if (CK != oldCK) {
// throw some error
}
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
4
Message Digests
• Message digests (one way hash functions)
are a much better data verification methods
• Message digests are strong
• Used for "Digital Signatures" (and passwords)
• SHA1 and MD5 are the most common algorithms
• The idea is that you have some data x, put
that through a function f to get y [y = f(x)]
• You cannot recompute x from y
• Any change (small or large) in x creates a wildly
different (and probabilistically unique) y
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Digest[In|Out]putStream
• These work as you would expect...
• MessageDigest md =
[Link]("SHA");
DigestOutputStream DOS = new
DigestOutputStream(Fptr, md);
[Link](x);
• Reading is symmetric
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
5
Compressed Streams
• Deflator[In|Out]putStream
GZIP[In|Out]putStream
Zip[In|Out]putStream
• Realtime on-the-fly compression and
decompression of all data in the stream
• GZIPOutputStream GOS =
new GZIPOutputStream(Fptr);
[Link](x);
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Progress Monitoring
• Puts a GUI object up with a progress bar
• You must specify where you want the GUI
object to show up as well as a message
• InputStream in =
new BufferedInputStream(
new ProgressMonitorInputStream(
parentComponent,
"Reading " + fileName,
new FileInputStream(fileName)));
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
6
Lists of Streams
• SequenceInputStream allows you to
concatenate streams
• After one stream is completely read, the next
one in the list will be read
• You can construct a SequenceInputStream
using an Enumeration or a pair of
InputStream objects
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Bytes, why bytes?
• Streams use bytes as the unit which can be
read and written
• Bytes are good for hardware, but not good
for software
• We need some abstractions...
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
7
Readers and Writers
• These provide the ability to perform input
and output using characters (Strings)
• Readers work with InputStreams
• Writers work with OutputStreams
• Reader and Writer are abstract classes
• You will once again need to use extensions
of these classes to do useful work
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Streams to Readers/Writers
• InputStreamReader convers an
InputStream into a Reader
• InputStreamReader ISR = new
InputStreamReader(new FileInputStream("..."));
• OutputStreamWriter converts an
OutputStream into a Writer
• OutputStreamWriter OSW = new
OutputStreamWriter(new
FileOutputStream("..."));
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
8
Buffered I/O
• BufferedReader and BufferedWriter are
equivalent to BufferedInputStream and
BufferedOutputStream
• In addition, BufferedReader has the ability
to read a whole line of text (stripping the
newline character(s)) with readLine()
• BufferedWriter has a platform independent
newLine() method for outputting a newline
character into the destination stream
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Using Strings as Devices
• Sometimes you want to use a String rather
than a physical device for input and output
• This is particularly useful for debugging
• StringReader and StringWriter let you read
and write to a String in the same way that
you would to a device
• The String can them be examined/modified
by hand or by the computer
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
9
First Bytes, Then Strings...
• The next step is to be able to work with
JAVA Objects as the data for I/O
• The ObjectOutputStream and
ObjectInputStream classes are designed to
allow us to do this
• Setup your I/O session as usual
• Use readObject / writeObject to do I/O
• Be careful of casting!
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
ObjectOutputStream
MySpecialObject me = // construct the object
FileOutputStream Fptr = new
FileOutputStream("/tmp/blah");
ObjectOutputStream OOS = new
ObjectOutputStream(Fptr);
[Link](me);
[Link]();
[Link]();
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
10
ObjectInputStream
MySpecialObject me = null;
FileInputStream Fptr = new
FileInputStream("/tmp/blah");
ObjectInputStream OOS = new
ObjectInputStream(Fptr);
me = (MySpecialObject)[Link]();
[Link]();
[Link]();
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Serialization
• If you want to read/write an object, it needs
to implement the [Link]
interface
• This interface declares no abstract methods
• So why have it at all?
• Example: RMI... Serializable objects are
passed by value whereas Remote objects
are passed by reference
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
11
One Final Note
Don't forget to try and catch!
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
12