IO - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

IO

Description:

communication between producer and consumer (different threads) through a pipe ... Thread fibThread = new FibMaker( new DataOutputStream(out)); fibThread.start ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 23
Provided by: michael742
Category:
Tags: threads

less

Transcript and Presenter's Notes

Title: IO


1
I/O
  • I/O sequential and random access
  • streams
  • sequential
  • uniform specification for any data transfer
  • file I/O
  • network transmission
  • to/from memory
  • InputStream
  • OutputStream

2
Input streams
InputStream
File Input Stream
Filter Input Stream
Piped Input Stream
Object Input Stream
Sequence Input Stream
ByteArray Input Stream
Data Input Stream
Buffered Input Stream
Pushback Input Stream
LineNumber Input Stream
3
Input streams (cont.)
  • abstract class InputStream
  • //read a singe byte from the input
  • int read() throws IOException
  • //read an array of values from the input
  • int read(byte buffer) throws IOException
  • //skip the indicated number of values from input
  • long skip(long n) throws IOException
  • //determine number of bytes readable without
    blocking
  • int available() throws IOException
  • //close this input stream
  • void close() throws IOException

4
Input streams (cont.)
  • Physical input stream
  • ByteArrayInputStream
  • FileInputStream
  • PipedInputStream
  • ByteArrayInputStream(byte buffer)
  • ByteArrayInputStrean(byte buffer, int offset,
    int count)
  • FileInputStream(File f)
  • FileInputStream(String fileName)
  • PipedInputStream(PipedOutputStream p)

5
Input streams (cont.)
  • Virtual input streams
  • SequenceInputStream
  • ObjectInputStream
  • FilterInputStream and its subclasses
  • do not read values from any input area
  • rely on one or more underlying input streams
  • are implementations of the design patter decorator

6
SequenceInputStream - Example
  • InputStream f1 new FileInputStream(file1.txt)
  • InputStream f2 new FileInputStream(file2.txt)
  • InputStream f3 new SequenceInputStream(f1,f2)
  • //f3 now represents the catenation of file1 and
    file2
  • Vector fv new Vector()
  • fv.addElement(f1)
  • fv.addElement(f2)
  • InputStream f4 new SequenceInputStream(fv.elemen
    ts())
  • //f4 also now represents the same catenation

7
Filter
  • add behavior to the stream
  • DataInputStream read bytes from the source and
    return them as a primitive type, e.g.,
  • public int readInt() throws IOException
  • PushbackInputStream allows a single character to
    be unread.
  • BufferedInputStream allows the input operations
    to backed up over a larger range mark(),
    reset()
  • LineNumberInputStream deprecated

8
Output streams
OutputStream
ByteArray Output Stream
File Output Stream
Filter Output Stream
Piped Output Stream
Object Output Stream
Data Output Stream
Buffered Output Stream
Print Stream
9
Output streams (cont.)
  • abstract class OutputStream
  • //write a singe byte value
  • void write(int b) throws IOException
  • //write an array of byte values
  • void write(byte buffer) throws IOException
  • //flush all output from buffers
  • void flush() throws IOException
  • //close this output stream
  • void close() throws IOException

10
Output streams (cont.)
  • Physical output stream
  • ByteArrayOutputStream
  • FileOutputStream
  • PipedOutputStream
  • Virtual output streams
  • ObjectOutputStream
  • FilterOutputStream and its subclasses
  • DataOutputStream the output equivalent
    DataInputStream
  • BufferedOutputStream uses a buffer, values are
    written to the underlying stream only when the
    buffer becomes full or when the output is flushed
  • PrintStream similar to DataOutputStream but
    generates a textual representation rather than a
    binary representation

11
Piped input and output
  • used for producer/consumer relationships
  • communication between producer and consumer
    (different threads) through a pipe
  • each pipe is manifested by a matched pair of
    stream pointers, a PipedInputStream and a
    PipedOutputStream, e.g.,
  • PipedInputStream in new PipedInputStream()
  • PipedOutputStream out new PipedOutputStream(in)
  • Example finding all numbers that are both prime
    numbers and Fibonacci numbers, i.e., a number
    defined by the recursive relation
  • f0 0, f1 1,
  • fn2 fn fn1

12
Piped I/O - Example
  • class FibMaker extends Thread
  • private DataOutputStream out
  • public FibMaker(DataOutputStream o)
  • out o
  • public void run()
  • ?
  • try
  • ?
  • out.writeInt(newValue)
  • ?
  • out.close()
  • catch (IOException e)
  • return

13
  • class PrimeMaker extends Thread
  • private DataOutputStream out
  • public PrimeMaker(DataOutputStream o)
  • out o
  • public void run()
  • ?
  • try
  • ?
  • out.writeInt(newValue)
  • ?
  • out.close()
  • catch (IOException e)
  • return

14
  • class PipeTest
  • public static void main(String args)
  • PipeTest world new PipeTest(System.out)
  • private DataInputStream makeFibs()
  • try
  • PipedInputStream in new PipedInputStream()
  • PipedOutputStream out new PipedOutputStream(i
    n)
  • Thread fibThread new FibMaker(
  • new DataOutputStream(out))
  • fibThread.start()
  • return new DataInputStream(in)
  • catch (IOException e)
  • return null

15
  • private DataInputStream makePrimes()
  • private PipeTest(PrintStream out)
  • DataInputStream fibs makeFibs()
  • DataInputStream primes makePrimes()
  • try
  • int x fibs.readInt()
  • int y primes.readInt()
  • while (x lt 100000)
  • if (x y)
  • out.println()
  • x fibs.readInt()
  • y primes.readInt()
  • else if (x lt y)
  • x fibs.readInt()
  • else
  • y primes.readInt()
  • catch (IOException e)

16
Character Streams
  • Reader / Writer mirror the functionality provided
    by the classes InputStream and OutputStream
  • 8-bit bytes versus 16-bit Unicode character
    values
  • Physical Reader / Writer
  • CharArrayReader / Writer
  • StringReader / Writer
  • FileReader / Writer
  • PipedReader / Writer
  • Virtual Reader / Writer
  • BufferedReader / Writer
  • FilterReader / Writer
  • PrintWriter
  • InputStreamReader / OutputStreamWriter act as
    filter for streams, i.e., they convert streams to
    character streams, e.g.,
  • FileInputStream f new FileInputStream(fileName
    )
  • InputStreamReader r new InputStreamReader(f)

17
Reader
CharArray Reader
InputStream Reader
Filter Reader
Piped Reader
String Reader
Buffered Reader
LineNumber Reader
File Reader
Pushback Reader
Writer
Buffered Writer
CharArray Writer
OutputStream Writer
Filter Writer
Print Writer
String Writer
Piped Writer
File Writer
18
StreamTokenizer
  • StreamTokenizer is neither an InputStream nor a
    Reader
  • provides a useful mechanism for breaking a
    textual file into a sequence of tokens.
  • Example
  • 23-skidoo, kid! yields the output
  • number 23.0
  • token -
  • word skidoo
  • token ,
  • word kid
  • token !

19
  • Reader r new InputStreamReader(System.in)
  • StreamTokenizer tok new StreamTokenizer(r)
  • try
  • while (tok.nextToken() ! tok.TT_EOF)
  • switch (tok.ttype)
  • case tok.TT_NUMBER
  • System.out.println(number tok.nval)
  • break
  • case tok.TT_EOL
  • System.out.println(end of line.)
  • break
  • case tok.TT_WORD
  • System.out.println(word tok.sval)
  • break
  • default
  • System.out.println(token (char)
    tok.ttype)
  • break

20
Random Access I/O
  • RandomAccessFile
  • can read write at same time
  • implements DataInput DataOutput
  • interfaces which are also implemented by
    DataInputStream and DataOutputStream,
    respectively
  • changing current I/O position
  • seek(l) moves the read/write position to the l-th
    byte counting from the beginning of the file
  • skipBytes(i) moves the read/write position i
    bytes relative to the current position
  • storing serialized objects in random access files
  • Idea write object as size followed by serialized
    version
  • Implementation X. Jia, Object-Oriented Software
    Development using Java, 8.4.4

21
Examples
  • copy a file (unfiltered, byte)
  • try
  • FileInputStream in
  • new FileInputStream(source)
  • FileOutputStream out
  • new FileOutputStream(target)
  • int val in.read()
  • while (val ! -1)
  • out.write(val)
  • val in.read()
  • in.close()
  • out.close()
  • catch (IOException e)

22
Examples
  • copy a file (buffered Reader / Writer)
  • try
  • FileReader fin new FileReader(source)
  • BufferedReader in new BufferedReader(fin)
  • FileWriter fout new FileWriter(target)
  • BufferedWriter out new BufferedWriter(fout)
  • String str in.readLine()
  • while (str ! null)
  • out.write(str)
  • str in.readLine()
  • in.close()
  • out.close()
  • catch (IOException e)
Write a Comment
User Comments (0)
About PowerShow.com