13.07.2015 Views

I/O Fundamentals

I/O Fundamentals

I/O Fundamentals

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Java Input and OutputPipe ExampleNow, a little example, implementing our analogy. We'll create a quarry that holds1000 rocks. Flint's loader can carry 50 rocks at a time. Bernie's dump truck cancarry 75.First, we'll define classes to help us watch the progress of this scenario. We startwith two filters that report read()s and close()s. Note that here we're usingfilters to simply report what's happening; we're not modifying the data! (Alsonote that these classes need to be defined in separate files)import java.io.*;/** An OutputStream filter that helps us watch what* Flint Fredstone is doing*/public class FlintLogOutputStream extends FilterOutputStream {public FlintLogOutputStream(OutputStream out) {super(out);}public void write(byte b[]) throws IOException {System.out.println("Fred is dumping rocks into Bernie'struck");super.write(b, 0, b.length);System.out.println("Fred is going to dig more rocks");}}public void close() throws IOException {super.close();System.out.println("Flint goes home for the day");}// ----------------------------------------------------------------import java.io.*;/** An input filter that helps us watch what Bernie Rumble* is doing */public class BernieLogInputStream extends FilterInputStream {public BernieLogInputStream(InputStream in) {super(in);}public int read(byte b[]) throws IOException {System.out.println("Bernie is ready to get loaded");int x = super. read(b, 0, b.length);if (x == -1)System.out.println("Nothing more to put in Bernie's© 1996-2003 jGuru.com. All Rights Reserved. Java Input and Output -39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!