/*
* @(#)DataOutputStream.java 1.00 01/12/14
*
* Adapted from the original Sun Microsystems code for leJOS.
* @author Brian Bagnall
*/
package java.io;
public class DataOutputStream extends OutputStream {
protected int written;
protected OutputStream out;
public DataOutputStream(OutputStream out) {
this.out = out;
}
/**
* Increases the written counter by the specified value
* until it reaches Integer.MAX_VALUE.
*/
private void incCount(int value) {
int temp = written + value;
//if (temp < 0)
//temp = Integer.MAX_VALUE;
written = temp;
}
/**
* Writes the specified byte (the low eight bits of the argument
* b
) to the underlying output stream. If no exception
* is thrown, the counter written
is incremented by
* 1
.
*
* Implements the write
method of OutputStream
.
*
* @param b the byte
to be written.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
public synchronized void write(int b) throws IOException {
out.write(b);
incCount(1);
}
/**
* Writes len
bytes from the specified byte array
* starting at offset off
to the underlying output stream.
* If no exception is thrown, the counter written
is
* incremented by len
.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
public synchronized void write(byte b[], int off, int len) throws IOException {
out.write(b, off, len);
incCount(len);
}
/**
* Flushes this data output stream. This forces any buffered output
* bytes to be written out to the stream.
*
* The flush
method of DataOuputStream
* calls the flush
method of its underlying output stream.
*
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
* @see java.io.OutputStream#flush()
*/
public void flush() throws IOException {
out.flush();
}
/**
* Writes a boolean
to the underlying output stream as
* a 1-byte value. The value true
is written out as the
* value (byte)1
; the value false
is
* written out as the value (byte)0
. If no exception is
* thrown, the counter written
is incremented by
* 1
.
*
* @param v a boolean
value to be written.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
public final void writeBoolean(boolean v) throws IOException {
out.write(v ? 1 : 0);
incCount(1);
}
/**
* Writes out a byte
to the underlying output stream as
* a 1-byte value. If no exception is thrown, the counter
* written
is incremented by 1
.
*
* @param v a byte
value to be written.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
public final void writeByte(int v) throws IOException {
out.write(v);
incCount(1);
}
/**
* Writes a short
to the underlying output stream as two
* bytes, high byte first. If no exception is thrown, the counter
* written
is incremented by 2
.
*
* @param v a short
to be written.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
public final void writeShort(int v) throws IOException {
OutputStream out = this.out;
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
incCount(2);
}
/**
* Writes a char
to the underlying output stream as a
* 2-byte value, high byte first. If no exception is thrown, the
* counter written
is incremented by 2
.
*
* @param v a char
value to be written.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
public final void writeChar(int v) throws IOException {
OutputStream out = this.out;
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
incCount(2);
}
/**
* Writes an int
to the underlying output stream as four
* bytes, high byte first. If no exception is thrown, the counter
* written
is incremented by 4
.
*
* @param v an int
to be written.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
public final void writeInt(int v) throws IOException {
OutputStream out = this.out;
//for(byte i=24;i>=0;i-=8)
// out.write((int)(v >>> i) & 0xFF);
out.write((v >>> 24) & 0xFF);
out.write((v >>> 16) & 0xFF);
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
incCount(4);
}
/**
* Writes a long
to the underlying output stream as eight
* bytes, high byte first. In no exception is thrown, the counter
* written
is incremented by 8
.
*
* @param v a long
to be written.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
*/
/* public final void writeLong(long v) throws IOException {
OutputStream out = this.out;
out.write((int)(v >>> 56) & 0xFF);
out.write((int)(v >>> 48) & 0xFF);
out.write((int)(v >>> 40) & 0xFF);
out.write((int)(v >>> 32) & 0xFF);
out.write((int)(v >>> 24) & 0xFF);
out.write((int)(v >>> 16) & 0xFF);
out.write((int)(v >>> 8) & 0xFF);
out.write((int)(v >>> 0) & 0xFF);
//for(byte i=56;i>=0;i-=8)
// out.write((int)(v >>> i) & 0xFF);
incCount(8);
}
*/
/**
* Converts the float argument to an int
using the
* floatToIntBits
method in class Float
,
* and then writes that int
value to the underlying
* output stream as a 4-byte quantity, high byte first. If no
* exception is thrown, the counter written
is
* incremented by 4
.
*
* @param v a float
value to be written.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
* @see java.lang.Float#floatToIntBits(float)
*/
public final void writeFloat(float v) throws IOException {
writeInt(Float.floatToIntBits(v));
}
/**
* Converts the double argument to a long
using the
* doubleToLongBits
method in class Double
,
* and then writes that long
value to the underlying
* output stream as an 8-byte quantity, high byte first. If no
* exception is thrown, the counter written
is
* incremented by 8
.
*
* @param v a double
value to be written.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
* @see java.lang.Double#doubleToLongBits(double)
*/
//public final void writeDouble(double v) throws IOException {
//writeLong(Double.doubleToLongBits(v));
//}
/**
* Returns the current value of the counter written
,
* the number of bytes written to this data output stream so far.
* If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
*
* @return the value of the written
field.
* @see java.io.DataOutputStream#written
*/
public final int size() {
return written;
}
}