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.

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

DataInput and DataOutput to use big-endian numbers, and it's best not to go against such<br />

common assumptions.<br />

I also considered making the little-endian classes subclasses of DataInputStream and<br />

DataOutputStream. While this would have eliminated some duplicated methods like<br />

readBoolean() and writeBoolean(), it would also have required the new, little-endian<br />

methods to have unwieldy names like readLittleEndianInt() and<br />

writeLittleEndianInt(). Furthermore, it's unlikely you'll need to read or write both littleendian<br />

and big-endian numbers from the same stream. Most streams will contain one or the<br />

other but not both.<br />

Example 7.8. A LittleEndianOutputStream Class<br />

/*<br />

* @(#)LittleEndianOutputStream.java 1.0 98/08/29<br />

*/<br />

package com.macfaq.io;<br />

import java.io.*;<br />

/**<br />

* A little-endian output stream writes primitive <strong>Java</strong> numbers<br />

* and characters to an output stream in a little-endian format.<br />

* The standard java.io.DataOutputStream class which this class<br />

* imitates uses big-endian integers.<br />

*<br />

* @author Elliotte Rusty Harold<br />

* @version 1.0, 29 Aug 1998<br />

* @see com.macfaq.io.LittleEndianInputStream<br />

* @see java.io.DataOutputStream<br />

*/<br />

public class LittleEndianOutputStream extends FilterOutputStream {<br />

/**<br />

* The number of bytes written so far to the little-endian output stream.<br />

*/<br />

protected int written;<br />

/**<br />

* Creates a new little-endian output stream and chains it to the<br />

* output stream specified by the out argument.<br />

*<br />

* @param out the underlying output stream.<br />

* @see java.io.FilterOutputStream#out<br />

*/<br />

public LittleEndianOutputStream(OutputStream out) {<br />

super(out);<br />

}<br />

/**<br />

* Writes the specified byte value to the underlying output stream.<br />

*<br />

* @param b the byte value to be written.<br />

* @exception <strong>IO</strong>Exception if the underlying stream throws an<br />

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

*/<br />

112

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

Saved successfully!

Ooh no, something went wrong!