OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[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 /**
10  * Transmits java data types as bytes over an OutputStream.
11  */
12 public class DataOutputStream extends OutputStream {
13     
14    protected int written;
15    protected OutputStream out;
16    
17    public DataOutputStream(OutputStream out) {
18       this.out = out;
19    }
20
21    /**
22    * Increases the written counter by the specified value
23    * until it reaches Integer.MAX_VALUE.
24    */
25    private void incCount(int value) {
26       int temp = written + value;
27         
28       //if (temp < 0)
29       //temp = Integer.MAX_VALUE;
30       written = temp;
31    }
32
33    /**
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 
37    * <code>1</code>.
38    * <p>
39    * Implements the <code>write</code> method of <code>OutputStream</code>.
40    *
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
44    */
45    public synchronized void write(int b) throws IOException {
46       out.write(b);
47       incCount(1);
48    }
49
50    /**
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>.
55    *
56    * @param      b     the data.
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
61    */
62    public synchronized void write(byte b[], int off, int len) throws IOException {
63       out.write(b, off, len);
64       incCount(len);
65    }
66
67     /**
68      * Flushes this data output stream. This forces any buffered output 
69      * bytes to be written out to the stream. 
70      * <p>
71      * The <code>flush</code> method of <code>DataOuputStream</code> 
72      * calls the <code>flush</code> method of its underlying output stream.
73      *
74      * @exception  IOException  if an I/O error occurs.
75      * @see        java.io.FilterOutputStream#out
76      * @see        java.io.OutputStream#flush()
77      */
78     public void flush() throws IOException {
79         out.flush();
80     }
81     /**
82      * Closes this data output stream. This forces any buffered output 
83      * bytes to be written out to the stream. 
84      * <p>
85      * The <code>close</code> method of <code>DataOuputStream</code> 
86      * calls the <code>close</code> method of its underlying output stream.
87      *
88      * @exception  IOException  if an I/O error occurs.
89      * @see        java.io.FilterOutputStream#out
90      * @see        java.io.OutputStream#flush()
91      */
92     public void close()throws IOException {out.close();}
93    /**
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 
99    * <code>1</code>.
100    *
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
104    */
105    public final void writeBoolean(boolean v) throws IOException {
106       out.write(v ? 1 : 0);
107       incCount(1);
108    }
109
110    /**
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>.
114    *
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
118    */
119    public final void writeByte(int v) throws IOException {
120       out.write(v);
121       incCount(1);
122    }
123
124     /**
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>.
128      *
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
132      */
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);
137       incCount(2);
138    }
139
140    /**
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>.
144    *
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
148    */
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);
153       incCount(2);
154    }
155
156    /**
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>.
160    *
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
164    */
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);
173       incCount(4);
174    }
175
176    /**
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>.
180    *
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
184    */
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);
197       incCount(8);
198    }
199 */
200    /**
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>.
207    *
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)
212    */
213    public final void writeFloat(float v) throws IOException {
214       writeInt(Float.floatToIntBits(v));
215    }
216
217    /**
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>.
224    *
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)
229    */
230    //public final void writeDouble(double v) throws IOException {
231       //writeLong(Double.doubleToLongBits(v));
232    //}
233
234    /**
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.
238    *
239    * @return  the value of the <code>written</code> field.
240    * @see     java.io.DataOutputStream#written
241    */
242    public final int size() {
243       return written;
244    }
245    
246    public final void writeChars (String value) throws IOException
247    {
248            int len = value.length();
249            for (int i = 0; i < len; ++i)
250                    writeChar (value.charAt(i));
251    }
252 }
253    
254