KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > util > LEDataOutputStream


1 /*
2  * LEDataOutputStream.java
3  *
4  * Created on 10. Oktober 2005, 20:58
5  */

6 /*
7  * Copyright 2005 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.io.util;
23
24 import java.io.DataOutput JavaDoc;
25 import java.io.FilterOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 /**
30  * A stream to write data in Little Endian (LE) format.
31  * <p>
32  * This class is similar to {@link java.io.DataOutputStream},
33  * but writes data in Little Endian format to its underlying stream.
34  * A noteable difference to <code>DataOutputStream</code> is that the
35  * {@link #size()} method and the {@link #written} field are respectively
36  * return <code>long</code> values and wrap to <code>Long.MAX_VALUE</code>.
37  * <p>
38  * Note that this class is <em>not</em> thread safe.
39  *
40  * @author Christian Schlichtherle
41  */

42 public class LEDataOutputStream
43         extends FilterOutputStream JavaDoc
44         implements DataOutput JavaDoc {
45
46     /** This buffer is used for writing data. */
47     private final byte[] buf = new byte[8];
48
49     /**
50      * The number of bytes written to the data output stream so far.
51      * If this counter overflows, it will be wrapped to Long.MAX_VALUE.
52      */

53     protected volatile long written;
54     
55     /**
56      * Creates a new data output stream to write data to the specified
57      * underlying output stream. The counter <code>written</code> is
58      * set to zero.
59      *
60      * @param out The underlying output stream, to be saved for later use.
61      *
62      * @see java.io.FilterOutputStream#out
63      */

64     public LEDataOutputStream(OutputStream JavaDoc out) {
65         super(out);
66     }
67
68     /**
69      * Increases the written counter by the specified value
70      * until it reaches Long.MAX_VALUE.
71      */

72     private final void incCount(int inc) {
73         final long temp = written + inc;
74         written = temp >= 0 ? temp : Long.MAX_VALUE;
75     }
76
77     /**
78      * Writes the specified byte (the low eight bits of the argument
79      * <code>b</code>) to the underlying output stream. If no exception
80      * is thrown, the counter <code>written</code> is incremented by
81      * <code>1</code>.
82      * <p>
83      * Implements the <code>write</code> method of <code>OutputStream</code>.
84      *
85      * @param b The <code>byte</code> to be written.
86      *
87      * @throws IOException If an I/O error occurs.
88      *
89      * @see java.io.FilterOutputStream#out
90      */

91     public void write(int b) throws IOException JavaDoc {
92     out.write(b);
93         incCount(1);
94     }
95
96     /**
97      * Writes <code>len</code> bytes from the specified byte array
98      * starting at offset <code>off</code> to the underlying output stream.
99      * If no throws is thrown, the counter <code>written</code> is
100      * incremented by <code>len</code>.
101      *
102      * @param b The data.
103      * @param off The start offset in the data.
104      * @param len The number of bytes to write.
105      *
106      * @throws IOException If an I/O error occurs.
107      *
108      * @see java.io.FilterOutputStream#out
109      */

110     public void write(byte b[], int off, int len)
111     throws IOException JavaDoc {
112     out.write(b, off, len);
113     incCount(len);
114     }
115
116     /**
117      * Writes a <code>boolean</code> to the underlying output stream as
118      * a 1-byte value. The value <code>true</code> is written out as the
119      * value <code>(byte)1</code>; the value <code>false</code> is
120      * written out as the value <code>(byte)0</code>. If no exception is
121      * thrown, the counter <code>written</code> is incremented by
122      * <code>1</code>.
123      *
124      * @param b A <code>boolean</code> value to be written.
125      *
126      * @throws IOException If an I/O error occurs.
127      *
128      * @see java.io.FilterOutputStream#out
129      */

130     public final void writeBoolean(boolean b) throws IOException JavaDoc {
131     out.write(b ? 1 : 0);
132     incCount(1);
133     }
134
135     /**
136      * Writes out a <code>byte</code> to the underlying output stream as
137      * a 1-byte value. If no exception is thrown, the counter
138      * <code>written</code> is incremented by <code>1</code>.
139      *
140      * @param b A <code>byte</code> value to be written.
141      *
142      * @throws IOException If an I/O error occurs.
143      *
144      * @see java.io.FilterOutputStream#out
145      */

146     public final void writeByte(int b) throws IOException JavaDoc {
147     out.write(b);
148         incCount(1);
149     }
150
151     /**
152      * Writes a <code>char</code> to the underlying output stream as a
153      * 2-byte value, low byte first. If no exception is thrown, the
154      * counter <code>written</code> is incremented by <code>2</code>.
155      *
156      * @param c A <code>char</code> value to be written.
157      *
158      * @throws IOException If an I/O error occurs.
159      *
160      * @see java.io.FilterOutputStream#out
161      */

162     public final void writeChar(int c) throws IOException JavaDoc {
163         writeShort(c);
164     }
165
166     /**
167      * Writes a <code>short</code> to the underlying output stream as two
168      * bytes, low byte first. If no exception is thrown, the counter
169      * <code>written</code> is incremented by <code>2</code>.
170      *
171      * @param s A <code>short</code> to be written.
172      *
173      * @throws IOException If an I/O error occurs.
174      *
175      * @see java.io.FilterOutputStream#out
176      */

177     public final void writeShort(int s) throws IOException JavaDoc {
178         buf[0] = (byte) s;
179         s >>= 8;
180         buf[1] = (byte) s;
181         out.write(buf, 0, 2);
182         incCount(2);
183     }
184
185     /**
186      * Writes an <code>int</code> to the underlying output stream as four
187      * bytes, low byte first. If no exception is thrown, the counter
188      * <code>written</code> is incremented by <code>4</code>.
189      *
190      * @param i An <code>int</code> to be written.
191      *
192      * @throws IOException If an I/O error occurs.
193      *
194      * @see java.io.FilterOutputStream#out
195      */

196     public final void writeInt(int i) throws IOException JavaDoc {
197         buf[0] = (byte) i;
198         i >>= 8;
199         buf[1] = (byte) i;
200         i >>= 8;
201         buf[2] = (byte) i;
202         i >>= 8;
203         buf[3] = (byte) i;
204         out.write(buf, 0, 4);
205         incCount(4);
206     }
207
208     /**
209      * Writes a <code>long</code> to the underlying output stream as eight
210      * bytes, low byte first. In no exception is thrown, the counter
211      * <code>written</code> is incremented by <code>8</code>.
212      *
213      * @param l A <code>long</code> to be written.
214      *
215      * @throws IOException If an I/O error occurs.
216      *
217      * @see java.io.FilterOutputStream#out
218      */

219     public final void writeLong(long l) throws IOException JavaDoc {
220         buf[0] = (byte) l;
221         l >>= 8;
222         buf[1] = (byte) l;
223         l >>= 8;
224         buf[2] = (byte) l;
225         l >>= 8;
226         buf[3] = (byte) l;
227         l >>= 8;
228         buf[4] = (byte) l;
229         l >>= 8;
230         buf[5] = (byte) l;
231         l >>= 8;
232         buf[6] = (byte) l;
233         l >>= 8;
234         buf[7] = (byte) l;
235         out.write(buf, 0, 8);
236     incCount(8);
237     }
238
239     /**
240      * Converts the float argument to an <code>int</code> using the
241      * <code>floatToIntBits</code> method in class <code>Float</code>,
242      * and then writes that <code>int</code> value to the underlying
243      * output stream as a 4-byte quantity, low byte first. If no
244      * exception is thrown, the counter <code>written</code> is
245      * incremented by <code>4</code>.
246      *
247      * @param f A <code>float</code> value to be written.
248      *
249      * @throws IOException if an I/O error occurs.
250      *
251      * @see java.io.FilterOutputStream#out
252      * @see java.lang.Float#floatToIntBits(float)
253      */

254     public final void writeFloat(float f) throws IOException JavaDoc {
255     writeInt(Float.floatToIntBits(f));
256     }
257
258     /**
259      * Converts the double argument to a <code>long</code> using the
260      * <code>doubleToLongBits</code> method in class <code>Double</code>,
261      * and then writes that <code>long</code> value to the underlying
262      * output stream as an 8-byte quantity, low byte first. If no
263      * exception is thrown, the counter <code>written</code> is
264      * incremented by <code>8</code>.
265      *
266      * @param d A <code>double</code> value to be written.
267      *
268      * @throws IOException If an I/O error occurs.
269      *
270      * @see java.io.FilterOutputStream#out
271      * @see java.lang.Double#doubleToLongBits(double)
272      */

273     public final void writeDouble(double d) throws IOException JavaDoc {
274     writeLong(Double.doubleToLongBits(d));
275     }
276
277     /**
278      * Writes out the string to the underlying output stream as a
279      * sequence of bytes. Each character in the string is written out, in
280      * sequence, by discarding its high eight bits. If no exception is
281      * thrown, the counter <code>written</code> is incremented by the
282      * length of <code>s</code>.
283      *
284      * @param s A string of bytes to be written.
285      *
286      * @throws IOException If an I/O error occurs.
287      *
288      * @see java.io.FilterOutputStream#out
289      */

290     public final void writeBytes(String JavaDoc s) throws IOException JavaDoc {
291     final int len = s.length();
292     for (int i = 0 ; i < len ; i++)
293         writeByte(s.charAt(i));
294     }
295
296     /**
297      * Writes a string to the underlying output stream as a sequence of
298      * characters. Each character is written to the data output stream as
299      * if by the <code>writeChar</code> method. If no exception is
300      * thrown, the counter <code>written</code> is incremented by twice
301      * the length of <code>s</code>.
302      *
303      * @param s A <code>String</code> value to be written.
304      *
305      * @throws IOException If an I/O error occurs.
306      *
307      * @see java.io.DataOutputStream#writeChar(int)
308      * @see java.io.FilterOutputStream#out
309      */

310     public final void writeChars(String JavaDoc s) throws IOException JavaDoc {
311         final int len = s.length();
312         for (int i = 0 ; i < len ; i++)
313             writeShort(s.charAt(i));
314     }
315
316     /**
317      * This method is not implemented.
318      *
319      * @throws UnsupportedOperationException Always.
320      */

321     public void writeUTF(String JavaDoc str) throws IOException JavaDoc {
322         throw new UnsupportedOperationException JavaDoc();
323     }
324
325     /**
326      * Returns the current value of the counter <code>written</code>,
327      * the number of bytes written to this data output stream so far.
328      * If the counter overflows, it will be wrapped to Long.MAX_VALUE.
329      *
330      * @return The value of the <code>written</code> field.
331      *
332      * @see java.io.DataOutputStream#written
333      */

334     public final long size() {
335     return written;
336     }
337 }
338
Popular Tags