2 * @(#)DataOutputStream.java 1.00 01/12/14
4 * Adapted from the original Sun Microsystems code for leJOS.
5 * @author Brian Bagnall
9 public class DataOutputStream extends OutputStream {
11 protected int written;
12 protected OutputStream out;
14 public DataOutputStream(OutputStream out) {
19 * Increases the written counter by the specified value
20 * until it reaches Integer.MAX_VALUE.
22 private void incCount(int value) {
23 int temp = written + value;
26 //temp = Integer.MAX_VALUE;
31 * Writes the specified byte (the low eight bits of the argument
32 * <code>b</code>) to the underlying output stream. If no exception
33 * is thrown, the counter <code>written</code> is incremented by
36 * Implements the <code>write</code> method of <code>OutputStream</code>.
38 * @param b the <code>byte</code> to be written.
39 * @exception IOException if an I/O error occurs.
40 * @see java.io.FilterOutputStream#out
42 public synchronized void write(int b) throws IOException {
48 * Writes <code>len</code> bytes from the specified byte array
49 * starting at offset <code>off</code> to the underlying output stream.
50 * If no exception is thrown, the counter <code>written</code> is
51 * incremented by <code>len</code>.
54 * @param off the start offset in the data.
55 * @param len the number of bytes to write.
56 * @exception IOException if an I/O error occurs.
57 * @see java.io.FilterOutputStream#out
59 public synchronized void write(byte b[], int off, int len) throws IOException {
60 out.write(b, off, len);
65 * Flushes this data output stream. This forces any buffered output
66 * bytes to be written out to the stream.
68 * The <code>flush</code> method of <code>DataOuputStream</code>
69 * calls the <code>flush</code> method of its underlying output stream.
71 * @exception IOException if an I/O error occurs.
72 * @see java.io.FilterOutputStream#out
73 * @see java.io.OutputStream#flush()
75 public void flush() throws IOException {
79 * Closes this data output stream. This forces any buffered output
80 * bytes to be written out to the stream.
82 * The <code>close</code> method of <code>DataOuputStream</code>
83 * calls the <code>close</code> method of its underlying output stream.
85 * @exception IOException if an I/O error occurs.
86 * @see java.io.FilterOutputStream#out
87 * @see java.io.OutputStream#flush()
89 public void close()throws IOException {out.close();}
91 * Writes a <code>boolean</code> to the underlying output stream as
92 * a 1-byte value. The value <code>true</code> is written out as the
93 * value <code>(byte)1</code>; the value <code>false</code> is
94 * written out as the value <code>(byte)0</code>. If no exception is
95 * thrown, the counter <code>written</code> is incremented by
98 * @param v a <code>boolean</code> value to be written.
99 * @exception IOException if an I/O error occurs.
100 * @see java.io.FilterOutputStream#out
102 public final void writeBoolean(boolean v) throws IOException {
103 out.write(v ? 1 : 0);
108 * Writes out a <code>byte</code> to the underlying output stream as
109 * a 1-byte value. If no exception is thrown, the counter
110 * <code>written</code> is incremented by <code>1</code>.
112 * @param v a <code>byte</code> value to be written.
113 * @exception IOException if an I/O error occurs.
114 * @see java.io.FilterOutputStream#out
116 public final void writeByte(int v) throws IOException {
122 * Writes a <code>short</code> to the underlying output stream as two
123 * bytes, high byte first. If no exception is thrown, the counter
124 * <code>written</code> is incremented by <code>2</code>.
126 * @param v a <code>short</code> to be written.
127 * @exception IOException if an I/O error occurs.
128 * @see java.io.FilterOutputStream#out
130 public final void writeShort(int v) throws IOException {
131 OutputStream out = this.out;
132 out.write((v >>> 8) & 0xFF);
133 out.write((v >>> 0) & 0xFF);
138 * Writes a <code>char</code> to the underlying output stream as a
139 * 2-byte value, high byte first. If no exception is thrown, the
140 * counter <code>written</code> is incremented by <code>2</code>.
142 * @param v a <code>char</code> value to be written.
143 * @exception IOException if an I/O error occurs.
144 * @see java.io.FilterOutputStream#out
146 public final void writeChar(int v) throws IOException {
147 OutputStream out = this.out;
148 out.write((v >>> 8) & 0xFF);
149 out.write((v >>> 0) & 0xFF);
154 * Writes an <code>int</code> to the underlying output stream as four
155 * bytes, high byte first. If no exception is thrown, the counter
156 * <code>written</code> is incremented by <code>4</code>.
158 * @param v an <code>int</code> to be written.
159 * @exception IOException if an I/O error occurs.
160 * @see java.io.FilterOutputStream#out
162 public final void writeInt(int v) throws IOException {
163 OutputStream out = this.out;
164 //for(byte i=24;i>=0;i-=8)
165 // out.write((int)(v >>> i) & 0xFF);
166 out.write((v >>> 24) & 0xFF);
167 out.write((v >>> 16) & 0xFF);
168 out.write((v >>> 8) & 0xFF);
169 out.write((v >>> 0) & 0xFF);
174 * Writes a <code>long</code> to the underlying output stream as eight
175 * bytes, high byte first. In no exception is thrown, the counter
176 * <code>written</code> is incremented by <code>8</code>.
178 * @param v a <code>long</code> to be written.
179 * @exception IOException if an I/O error occurs.
180 * @see java.io.FilterOutputStream#out
182 /* public final void writeLong(long v) throws IOException {
183 OutputStream out = this.out;
184 out.write((int)(v >>> 56) & 0xFF);
185 out.write((int)(v >>> 48) & 0xFF);
186 out.write((int)(v >>> 40) & 0xFF);
187 out.write((int)(v >>> 32) & 0xFF);
188 out.write((int)(v >>> 24) & 0xFF);
189 out.write((int)(v >>> 16) & 0xFF);
190 out.write((int)(v >>> 8) & 0xFF);
191 out.write((int)(v >>> 0) & 0xFF);
192 //for(byte i=56;i>=0;i-=8)
193 // out.write((int)(v >>> i) & 0xFF);
198 * Converts the float argument to an <code>int</code> using the
199 * <code>floatToIntBits</code> method in class <code>Float</code>,
200 * and then writes that <code>int</code> value to the underlying
201 * output stream as a 4-byte quantity, high byte first. If no
202 * exception is thrown, the counter <code>written</code> is
203 * incremented by <code>4</code>.
205 * @param v a <code>float</code> value to be written.
206 * @exception IOException if an I/O error occurs.
207 * @see java.io.FilterOutputStream#out
208 * @see java.lang.Float#floatToIntBits(float)
210 public final void writeFloat(float v) throws IOException {
211 writeInt(Float.floatToIntBits(v));
215 * Converts the double argument to a <code>long</code> using the
216 * <code>doubleToLongBits</code> method in class <code>Double</code>,
217 * and then writes that <code>long</code> value to the underlying
218 * output stream as an 8-byte quantity, high byte first. If no
219 * exception is thrown, the counter <code>written</code> is
220 * incremented by <code>8</code>.
222 * @param v a <code>double</code> value to be written.
223 * @exception IOException if an I/O error occurs.
224 * @see java.io.FilterOutputStream#out
225 * @see java.lang.Double#doubleToLongBits(double)
227 //public final void writeDouble(double v) throws IOException {
228 //writeLong(Double.doubleToLongBits(v));
232 * Returns the current value of the counter <code>written</code>,
233 * the number of bytes written to this data output stream so far.
234 * If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
236 * @return the value of the <code>written</code> field.
237 * @see java.io.DataOutputStream#written
239 public final int size() {
243 public final void writeChars (String value) throws IOException
245 int len = value.length();
246 for (int i = 0; i < len; ++i)
247 writeChar (value.charAt(i));