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 Outputbe able to only dump a partial load in Bernie's truck, then he must wait for Bernieto return so he can dump the rest of the load.PipedOutputStream acts exactly like Flint Fredstone in this analogy. You can useit as the final target of any stream output. PipedInputStream acts exactly likeBernie Rumble. A few key points about this analogy:• All data storage is done in the PipedInputStream, just like Bernie's dumptruck.• PipedInputStream can hold only a fixed amount of data, then it forces thePipedOutputStream to wait until it processes some of that data. Thisblocking occurs until there is enough room for the data thePipedOutputStream wants to dump.• When a PipedInputStream wants to read() data, it is blocked until there isat least one byte available to read. If less than the desired amount of data isavailable, but at least one byte is available, it will read() the available amountand not wait for more data.• Before any I/O can be performed, a PipedOutputStream must be connected toa PipedInputStream. Otherwise, Flint is simply dumping his rocks in a bigpile...• Either end can close its stream first• Think of it as Flint or Bernie leaving work.• If the PipedOutputStream closes first, the PipedInputStream can keepreading the data it's stored, or close at any time. If Flint goes home first, Berniecan still take the rocks in his truck to the processing plant, or call it a dayhimself.• If the PipedInputStream closes first, the PipedOutputStream cannot writeany more data! If Bernie goes home first, Flint has nowhere to dump hisrocks!A final note before an example: Flint and Bernie must be independent entities.They each have their own keys for their vehicles and are extremely territorial!PipedInputStream and PipedOutputStream use Java's thread synchronizationsupport, and because of the manner in which they are implemented, if both thePipedInputStream and PipedOutputStream are used in the same thread theymay cause a deadlock!Java Input and Output -38© 1996-2003 jGuru.com. All Rights Reserved.

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

Saved successfully!

Ooh no, something went wrong!