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.

7.7 Miscellaneous Methods<br />

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

The DataInputStream and DataOutputStream classes each have one method left to discuss,<br />

skipBytes() and size(), respectively.<br />

7.7.1 Determining the Number of Bytes Written<br />

The DataOutputStream class has a protected field called written that stores the number of<br />

bytes written to the output stream since it was constructed. The value of this field is returned<br />

by the public size() method:<br />

protected int written<br />

public final int size()<br />

Every time you invoke writeInt(), writeBytes(), writeUTF(), or some other write<br />

method, the written field is incremented by the number of bytes written. This might be<br />

useful if for some reason you're trying to limit the number of bytes you write. For instance,<br />

you may prefer to open a new file when you reach some preset size rather than continuing to<br />

write into a very large file.<br />

7.7.2 Skipping Bytes in an Input Stream<br />

The DataInputStream class's skipBytes() method skips over a specified number of bytes<br />

without reading them. Unlike the skip() method of java.io.InputStream that<br />

DataInputStream inherits, skipBytes() either skips over all the bytes it's asked to skip or it<br />

throws an exception:<br />

public final int skipBytes(int n) throws <strong>IO</strong>Exception<br />

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

skipBytes() blocks and waits for more data until n bytes have been skipped (successful<br />

execution) or an exception is thrown. The method returns the number of bytes skipped, which<br />

is always n (because if it's not n , an exception is thrown and nothing is returned). On end of<br />

stream, an EOFException is thrown. An <strong>IO</strong>Exception is thrown if the underlying stream<br />

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

7.8 Reading and Writing Little-Endian Numbers<br />

It's likely that at some point in time you'll need to read a file full of little-endian data,<br />

especially if you're working on Intel hardware or with data written by native code on such a<br />

platform. <strong>Java</strong> has essentially no support for little-endian numbers. The<br />

LittleEndianOutputStream class in Example 7.8 and the LittleEndianInputStream class<br />

in Example 7.9 provide the support you need to do this. These classes are closely modeled on<br />

the java.io.DataInputStream and java.io.DataOutputStream classes. Some of the<br />

methods in these classes do exactly the same thing as the same methods in the<br />

DataInputStream and DataOutputStream classes. After all, a big-endian byte is no different<br />

from a little-endian byte. In fact, these two classes come very close to implementing the<br />

java.io.DataInput and java.io.DataOutput interfaces. Actually doing so would have<br />

been a bad idea, however, because client programmers will expect objects implementing<br />

111

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

Saved successfully!

Ooh no, something went wrong!