13.07.2015 Views

I/O Fundamentals

I/O Fundamentals

I/O Fundamentals

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Java Input and Outputtruck!");elseSystem.out.println("Bernie has dropped off " + x +" at the processing plant");return x;}}public void close() throws IOException {super.close();System.out.println("Bernie goes home for the day");}Next we'll subclass PipedOutputStream to modify the size of its buffer. For thisexample, we want to treat its buffer as the same size as the chunks that Bernie isreading. Luckily, the buffer is represented by a protected instance variable so wecan access it.import java.io.*;/** Subclass of PipedInputStream so we can control* the buffer size */public class BerniePipedInputStream extends PipedInputStream {/** Shrink the buffer to 75 rocks... */public BerniePipedInputStream(PipedOutputStream src)throws IOException {super(src);buffer = new byte[75];}}Now we define two threads to represent the jobs that Flint and Bernie perform.Flint digs a certain number of rocks and dumps them somewhere (note that all heknows is that he's dumping them to an OutputStream!) Bernie takes any rocks hehas been given to the processing plant, in groups of 75 if possible. Note thatBernie also has no idea where the data is coming from; all he knows is that it is anInputStream.import java.io.*;/** A thread that represents Flint Fredstone's job.* He digs up a certain number of rocks and drops them off* in a stream*/public class FlintThread extends Thread {private byte[] rocks = new byte[50];private OutputStream out;public FlintThread(OutputStream out) {this.out = out;}Java Input and Output -40© 1996-2003 jGuru.com. All Rights Reserved.

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

Saved successfully!

Ooh no, something went wrong!