2 * @(#)DataOutputStream.java 1.00 01/12/14
4 * Adapted from the original Sun Microsystems code for leJOS.
5 * @author Brian Bagnall
10 * Transmits java data types as bytes over an OutputStream.
12 public class DataOutputStream extends OutputStream {
14 protected int written;
15 protected OutputStream out;
17 public DataOutputStream(OutputStream out) {
22 * Increases the written counter by the specified value
23 * until it reaches Integer.MAX_VALUE.
25 private void incCount(int value) {
26 int temp = written + value;
29 //temp = Integer.MAX_VALUE;
34 * Writes the specified byte (the low eight bits of the argument
35 * <code>b</code>) to the underlying output stream. If no exception
36 * is thrown, the counter <code>written</code> is incremented by
39 * Implements the <code>write</code> method of <code>OutputStream</code>.
41 * @param b the <code>byte</code> to be written.
42 * @exception IOException if an I/O error occurs.
43 * @see java.io.FilterOutputStream#out
45 public synchronized void write(int b) throws IOException {
51 * Writes <code>len</code> bytes from the specified byte array
52 * starting at offset <code>off</code> to the underlying output stream.
53 * If no exception is thrown, the counter <code>written</code> is
54 * incremented by <code>len</code>.
57 * @param off the start offset in the data.
58 * @param len the number of bytes to write.
59 * @exception IOException if an I/O error occurs.
60 * @see java.io.FilterOutputStream#out
62 public synchronized void write(byte b[], int off, int len) throws IOException {
63 out.write(b, off, len);
68 * Flushes this data output stream. This forces any buffered output
69 * bytes to be written out to the stream.
71 * The <code>flush</code> method of <code>DataOuputStream</code>
72 * calls the <code>flush</code> method of its underlying output stream.
74 * @exception IOException if an I/O error occurs.
75 * @see java.io.FilterOutputStream#out
76 * @see java.io.OutputStream#flush()
78 public void flush() throws IOException {
82 * Closes this data output stream. This forces any buffered output
83 * bytes to be written out to the stream.
85 * The <code>close</code> method of <code>DataOuputStream</code>
86 * calls the <code>close</code> method of its underlying output stream.
88 * @exception IOException if an I/O error occurs.
89 * @see java.io.FilterOutputStream#out
90 * @see java.io.OutputStream#flush()
92 public void close()throws IOException {out.close();}
94 * Writes a <code>boolean</code> to the underlying output stream as
95 * a 1-byte value. The value <code>true</code> is written out as the
96 * value <code>(byte)1</code>; the value <code>false</code> is
97 * written out as the value <code>(byte)0</code>. If no exception is
98 * thrown, the counter <code>written</code> is incremented by
101 * @param v a <code>boolean</code> value to be written.
102 * @exception IOException if an I/O error occurs.
103 * @see java.io.FilterOutputStream#out
105 public final void writeBoolean(boolean v) throws IOException {
106 out.write(v ? 1 : 0);
111 * Writes out a <code>byte</code> to the underlying output stream as
112 * a 1-byte value. If no exception is thrown, the counter
113 * <code>written</code> is incremented by <code>1</code>.
115 * @param v a <code>byte</code> value to be written.
116 * @exception IOException if an I/O error occurs.
117 * @see java.io.FilterOutputStream#out
119 public final void writeByte(int v) throws IOException {
125 * Writes a <code>short</code> to the underlying output stream as two
126 * bytes, high byte first. If no exception is thrown, the counter
127 * <code>written</code> is incremented by <code>2</code>.
129 * @param v a <code>short</code> to be written.
130 * @exception IOException if an I/O error occurs.
131 * @see java.io.FilterOutputStream#out
133 public final void writeShort(int v) throws IOException {
134 OutputStream out = this.out;
135 out.write((v >>> 8) & 0xFF);
136 out.write((v >>> 0) & 0xFF);
141 * Writes a <code>char</code> to the underlying output stream as a
142 * 2-byte value, high byte first. If no exception is thrown, the
143 * counter <code>written</code> is incremented by <code>2</code>.
145 * @param v a <code>char</code> value to be written.
146 * @exception IOException if an I/O error occurs.
147 * @see java.io.FilterOutputStream#out
149 public final void writeChar(int v) throws IOException {
150 OutputStream out = this.out;
151 out.write((v >>> 8) & 0xFF);
152 out.write((v >>> 0) & 0xFF);
157 * Writes an <code>int</code> to the underlying output stream as four
158 * bytes, high byte first. If no exception is thrown, the counter
159 * <code>written</code> is incremented by <code>4</code>.
161 * @param v an <code>int</code> to be written.
162 * @exception IOException if an I/O error occurs.
163 * @see java.io.FilterOutputStream#out
165 public final void writeInt(int v) throws IOException {
166 OutputStream out = this.out;
167 //for(byte i=24;i>=0;i-=8)
168 // out.write((int)(v >>> i) & 0xFF);
169 out.write((v >>> 24) & 0xFF);
170 out.write((v >>> 16) & 0xFF);
171 out.write((v >>> 8) & 0xFF);
172 out.write((v >>> 0) & 0xFF);
177 * Writes a <code>long</code> to the underlying output stream as eight
178 * bytes, high byte first. In no exception is thrown, the counter
179 * <code>written</code> is incremented by <code>8</code>.
181 * @param v a <code>long</code> to be written.
182 * @exception IOException if an I/O error occurs.
183 * @see java.io.FilterOutputStream#out
185 /* public final void writeLong(long v) throws IOException {
186 OutputStream out = this.out;
187 out.write((int)(v >>> 56) & 0xFF);
188 out.write((int)(v >>> 48) & 0xFF);
189 out.write((int)(v >>> 40) & 0xFF);
190 out.write((int)(v >>> 32) & 0xFF);
191 out.write((int)(v >>> 24) & 0xFF);
192 out.write((int)(v >>> 16) & 0xFF);
193 out.write((int)(v >>> 8) & 0xFF);
194 out.write((int)(v >>> 0) & 0xFF);
195 //for(byte i=56;i>=0;i-=8)
196 // out.write((int)(v >>> i) & 0xFF);
201 * Converts the float argument to an <code>int</code> using the
202 * <code>floatToIntBits</code> method in class <code>Float</code>,
203 * and then writes that <code>int</code> value to the underlying
204 * output stream as a 4-byte quantity, high byte first. If no
205 * exception is thrown, the counter <code>written</code> is
206 * incremented by <code>4</code>.
208 * @param v a <code>float</code> value to be written.
209 * @exception IOException if an I/O error occurs.
210 * @see java.io.FilterOutputStream#out
211 * @see java.lang.Float#floatToIntBits(float)
213 public final void writeFloat(float v) throws IOException {
214 writeInt(Float.floatToIntBits(v));
218 * Converts the double argument to a <code>long</code> using the
219 * <code>doubleToLongBits</code> method in class <code>Double</code>,
220 * and then writes that <code>long</code> value to the underlying
221 * output stream as an 8-byte quantity, high byte first. If no
222 * exception is thrown, the counter <code>written</code> is
223 * incremented by <code>8</code>.
225 * @param v a <code>double</code> value to be written.
226 * @exception IOException if an I/O error occurs.
227 * @see java.io.FilterOutputStream#out
228 * @see java.lang.Double#doubleToLongBits(double)
230 //public final void writeDouble(double v) throws IOException {
231 //writeLong(Double.doubleToLongBits(v));
235 * Returns the current value of the counter <code>written</code>,
236 * the number of bytes written to this data output stream so far.
237 * If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
239 * @return the value of the <code>written</code> field.
240 * @see java.io.DataOutputStream#written
242 public final int size() {
246 public final void writeChars (String value) throws IOException
248 int len = value.length();
249 for (int i = 0; i < len; ++i)
250 writeChar (value.charAt(i));