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

Create successful ePaper yourself

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

}<br />

}<br />

synchronized (in) {<br />

synchronized (out) {<br />

BufferedInputStream bin = new BufferedInputStream(in);<br />

BufferedOutputStream bout = new BufferedOutputStream(out);<br />

}<br />

}<br />

while (true) {<br />

int datum = bin.read();<br />

if (datum == -1) break;<br />

bout.write(datum);<br />

}<br />

bout.flush();<br />

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

This copy() method copies byte by byte, which is normally not very efficient. However,<br />

almost all the copies take place in memory, because the input stream and the output stream are<br />

buffered. Therefore, this is reasonably quick.<br />

It wouldn't hurt to read and write byte arrays in the copy() method instead of individual<br />

bytes, as long as the arrays you were reading and writing were significantly smaller than the<br />

buffer size. However, one level of buffering is usually sufficient. Detailed performance<br />

calculations depend on the virtual machine and the host OS, so it's hard to make any definite<br />

conclusions.<br />

Also note that the output stream is deliberately flushed. The data only reaches its eventual<br />

destination in the underlying stream out when the stream is flushed or the buffer fills up.<br />

Therefore, it's important to call flush() explicitly before the method returns.<br />

6.3.1 BufferedInputStream Details<br />

The buffer and the current state of the buffer are stored in protected fields. The buffer itself is<br />

a byte array called buf; the number of bytes in the buffer is an int named count; the index of<br />

the next byte that will be returned by read() is an int called pos; the mark, if any, is an int<br />

called markpos; the read-ahead limit be- fore the mark is invalidated is an int called<br />

marklimit. Subclasses of BufferedInputStream can directly access all these fields, which<br />

can be important for performance.<br />

protected byte[] buf<br />

protected int count<br />

protected int pos<br />

protected int markpos<br />

protected int marklimit<br />

BufferedInputStream only overrides methods from InputStream. It does not declare any<br />

new methods of its own. Marking and resetting are supported.<br />

public synchronized int read() throws <strong>IO</strong>Exception<br />

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

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

public synchronized long skip(long n) throws <strong>IO</strong>Exception<br />

public synchronized int available() throws <strong>IO</strong>Exception<br />

public synchronized void mark(int readLimit)<br />

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

82

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

Saved successfully!

Ooh no, something went wrong!