OSDN Git Service

bb8da1c521fd4d8a0b9d5aa7b274a97fe4889edb
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / classes / java / io / DataOutputStream.java
1  /*
2  * @(#)DataOutputStream.java   1.00 01/12/14
3  *
4  * Adapted from the original Sun Microsystems code for leJOS.
5  * @author Brian Bagnall
6  */
7 package java.io;
8
9 public class DataOutputStream extends OutputStream {
10     
11    protected int written;
12    protected OutputStream out;
13    
14    public DataOutputStream(OutputStream out) {
15       this.out = out;
16    }
17
18    /**
19    * Increases the written counter by the specified value
20    * until it reaches Integer.MAX_VALUE.
21    */
22    private void incCount(int value) {
23       int temp = written + value;
24         
25       //if (temp < 0)
26       //temp = Integer.MAX_VALUE;
27       written = temp;
28    }
29
30    /**
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 
34    * <code>1</code>.
35    * <p>
36    * Implements the <code>write</code> method of <code>OutputStream</code>.
37    *
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
41    */
42    public synchronized void write(int b) throws IOException {
43       out.write(b);
44       incCount(1);
45    }
46
47    /**
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>.
52    *
53    * @param      b     the data.
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
58    */
59    public synchronized void write(byte b[], int off, int len) throws IOException {
60       out.write(b, off, len);
61       incCount(len);
62    }
63
64     /**
65      * Flushes this data output stream. This forces any buffered output 
66      * bytes to be written out to the stream. 
67      * <p>
68      * The <code>flush</code> method of <code>DataOuputStream</code> 
69      * calls the <code>flush</code> method of its underlying output stream.
70      *
71      * @exception  IOException  if an I/O error occurs.
72      * @see        java.io.FilterOutputStream#out
73      * @see        java.io.OutputStream#flush()
74      */
75     public void flush() throws IOException {
76         out.flush();
77     }
78     /**
79      * Closes this data output stream. This forces any buffered output 
80      * bytes to be written out to the stream. 
81      * <p>
82      * The <code>close</code> method of <code>DataOuputStream</code> 
83      * calls the <code>close</code> method of its underlying output stream.
84      *
85      * @exception  IOException  if an I/O error occurs.
86      * @see        java.io.FilterOutputStream#out
87      * @see        java.io.OutputStream#flush()
88      */
89     public void close()throws IOException {out.close();}
90    /**
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 
96    * <code>1</code>.
97    *
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
101    */
102    public final void writeBoolean(boolean v) throws IOException {
103       out.write(v ? 1 : 0);
104       incCount(1);
105    }
106
107    /**
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>.
111    *
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
115    */
116    public final void writeByte(int v) throws IOException {
117       out.write(v);
118       incCount(1);
119    }
120
121     /**
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>.
125      *
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
129      */
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);
134       incCount(2);
135    }
136
137    /**
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>.
141    *
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
145    */
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);
150       incCount(2);
151    }
152
153    /**
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>.
157    *
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
161    */
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);
170       incCount(4);
171    }
172
173    /**
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>.
177    *
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
181    */
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);
194       incCount(8);
195    }
196 */
197    /**
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>.
204    *
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)
209    */
210    public final void writeFloat(float v) throws IOException {
211       writeInt(Float.floatToIntBits(v));
212    }
213
214    /**
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>.
221    *
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)
226    */
227    //public final void writeDouble(double v) throws IOException {
228       //writeLong(Double.doubleToLongBits(v));
229    //}
230
231    /**
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.
235    *
236    * @return  the value of the <code>written</code> field.
237    * @see     java.io.DataOutputStream#written
238    */
239    public final int size() {
240       return written;
241    }
242    
243    public final void writeChars (String value) throws IOException
244    {
245            int len = value.length();
246            for (int i = 0; i < len; ++i)
247                    writeChar (value.charAt(i));
248    }
249 }
250    
251