23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

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

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

public boolean markSupported()<br />

<strong>Java</strong> I/O<br />

In <strong>Java</strong> 2 and later, the two multibyte read() methods try to fill the specified array or<br />

subarray completely by reading repeatedly from the underlying input stream. They return only<br />

when the requested number of bytes have been read, the end of stream is reached, or the<br />

underlying stream would block. This is not the case for most input streams (including<br />

buffered input streams in <strong>Java</strong> 1.1.x and earlier), which only attempt one read from the<br />

underlying stream or data source before returning.<br />

6.3.2 BufferedOutputStream Details<br />

BufferedOutputStream also stores the buffer in a protected byte array named buf and the<br />

index of the next place in the array where a byte will be stored in an int field named pos.<br />

BufferedOutputStream does not expose the number of bytes in the buffer.<br />

protected byte buf[]<br />

protected int pos<br />

BufferedOutputStream only overrides three methods from OutputStream. It does not<br />

declare any new methods.<br />

public synchronized void write(int b) throws <strong>IO</strong>Exception<br />

public synchronized void write(byte data[], int offset, int length)<br />

throws <strong>IO</strong>Exception<br />

public synchronized void flush() throws <strong>IO</strong>Exception<br />

These methods are invoked exactly as they would be for any output stream. The only<br />

difference is that writes place data in the buffer rather than directly on the underlying output<br />

stream.<br />

6.4 PushbackInputStream<br />

The java.io.PushbackInputStream class provides a pushback buffer so a program can<br />

"unread" the last several bytes read. The next time data is read from the stream, the unread<br />

bytes are reread.<br />

public void unread(int b) throws <strong>IO</strong>Exception<br />

public void unread(byte[] data, int offset, int length) throws <strong>IO</strong>Exception<br />

public void unread(byte[] data) throws <strong>IO</strong>Exception<br />

By default the buffer is only one byte long, and trying to unread more than one byte throws an<br />

<strong>IO</strong>Exception. However, you can change the default buffer size with the second constructor:<br />

public PushbackInputStream(InputStream in)<br />

public PushbackInputStream(InputStream in, int size)<br />

Although both PushbackInputStream and BufferedInputStream use buffers, only a<br />

PushbackInputStream allows unreading, and only a BufferedInputStream allows marking<br />

and resetting. In a PushbackInputStream , markSupported() returns false.<br />

public boolean markSupported()<br />

83

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

Saved successfully!

Ooh no, something went wrong!