KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > shared > stream > StreamUtil


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2006 ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): ScalAgent Distributed Technologies
21  * Contributor(s):
22  */

23 package org.objectweb.joram.shared.stream;
24
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.EOFException JavaDoc;
29 import java.io.InvalidClassException JavaDoc;
30
31 import java.util.Vector JavaDoc;
32
33 import org.objectweb.joram.shared.util.Properties;
34
35 import org.objectweb.joram.shared.JoramTracing;
36 import org.objectweb.util.monolog.api.BasicLevel;
37
38 public final class StreamUtil {
39   // Per-thread buffer for conversion
40
private static ThreadLocal JavaDoc perThreadBuffer = new ThreadLocal JavaDoc() {
41     protected synchronized Object JavaDoc initialValue() {
42       return new byte[8];
43     }
44   };
45
46   // Be careful, the buffer will be reused..
47
private static byte[] readFully(int length, InputStream JavaDoc is) throws IOException JavaDoc {
48     int count = 0;
49     byte[] buf = (byte[]) (perThreadBuffer.get());
50     if (length > buf.length) buf = new byte[length];
51     
52     int nb = -1;
53     do {
54       nb = is.read(buf, count, length-count);
55       if (nb < 0) throw new EOFException JavaDoc();
56       count += nb;
57     } while (count != length);
58     return buf;
59   }
60
61   private static void readFully(byte[] buf, InputStream JavaDoc is) throws IOException JavaDoc {
62     int count = 0;
63     
64     int nb = -1;
65     do {
66       nb = is.read(buf, count, buf.length-count);
67       if (nb < 0) throw new EOFException JavaDoc();
68       count += nb;
69     } while (count != buf.length);
70   }
71
72   public static final int TRUE = 0;
73   public static final int FALSE = 1;
74
75   /**
76    * This method allows to write a boolean to the output stream.
77    *
78    * @param b the boolean to write
79    * @param os the stream to write the object to
80    */

81   public static void writeTo(boolean b, OutputStream JavaDoc os) throws IOException JavaDoc {
82     if (b)
83       os.write(TRUE);
84     else
85       os.write(FALSE);
86   }
87
88   /**
89    * This method allows to restore a boolean from the input stream.
90    *
91    * @param is the stream to read data from in order to restore the object
92    * @return the boolean
93    */

94   public static boolean readBooleanFrom(InputStream JavaDoc is) throws IOException JavaDoc {
95     if (is.read() == TRUE)
96       return true;
97     else
98       return false;
99   }
100
101   /**
102    * This method allows to write a byte to the output stream.
103    *
104    * @param b the byte to write
105    * @param os the stream to write the object to
106    */

107   public static void writeTo(byte b, OutputStream JavaDoc os) throws IOException JavaDoc {
108     os.write(b);
109   }
110
111   /**
112    * This method allows to restore a byte from the input stream.
113    *
114    * @param is the stream to read data from in order to restore the object
115    * @return the byte
116    */

117   public static byte readByteFrom(InputStream JavaDoc is) throws IOException JavaDoc {
118     return (byte) is.read();
119   }
120
121   /**
122    * This method allows to write a short to the output stream.
123    *
124    * @param s the short to write
125    * @param os the stream to write the object to
126    */

127   public static void writeTo(short s, OutputStream JavaDoc os) throws IOException JavaDoc {
128     byte[] buf = (byte[]) (perThreadBuffer.get());
129     buf[0] = (byte) (s >>> 8);
130     buf[1] = (byte) (s >>> 0);
131     os.write(buf, 0, 2);
132   }
133
134   /**
135    * This method allows to restore a short from the input stream.
136    *
137    * @param is the stream to read data from in order to restore the object
138    * @return the short
139    */

140   public static short readShortFrom(InputStream JavaDoc is) throws IOException JavaDoc {
141     byte[] buf = readFully(2, is);
142     return (short) (((buf[0] &0xFF) << 8) | (buf[1] &0xFF));
143   }
144
145   /**
146    * This method allows to write an integer to the output stream.
147    *
148    * @param i the integer to write
149    * @param os the stream to write the object to
150    */

151   public static void writeTo(int i, OutputStream JavaDoc os) throws IOException JavaDoc {
152     byte[] buf = (byte[]) (perThreadBuffer.get());
153     buf[0] = (byte) (i >>> 24);
154     buf[1] = (byte) (i >>> 16);
155     buf[2] = (byte) (i >>> 8);
156     buf[3] = (byte) (i >>> 0);
157     os.write(buf, 0, 4);
158   }
159
160   /**
161    * This method allows to restore an integer from the input stream.
162    *
163    * @param is the stream to read data from in order to restore the object
164    * @return the integer
165    */

166   public static int readIntFrom(InputStream JavaDoc is) throws IOException JavaDoc {
167     byte[] buf = readFully(4, is);
168     return (((buf[0] &0xFF) << 24) | ((buf[1] &0xFF) << 16) |
169             ((buf[2] &0xFF) << 8) | (buf[3] &0xFF));
170   }
171
172   /**
173    * This method allows to write a long to the output stream.
174    *
175    * @param l the long to write
176    * @param os the stream to write the object to
177    */

178   public static void writeTo(long l, OutputStream JavaDoc os) throws IOException JavaDoc {
179     byte[] buf = (byte[]) (perThreadBuffer.get());
180     buf[0] = (byte) (l >>> 56);
181     buf[1] = (byte) (l >>> 48);
182     buf[2] = (byte) (l >>> 40);
183     buf[3] = (byte) (l >>> 32);
184     buf[4] = (byte) (l >>> 24);
185     buf[5] = (byte) (l >>> 16);
186     buf[6] = (byte) (l >>> 8);
187     buf[7] = (byte) (l >>> 0);
188     os.write(buf, 0, 8);
189   }
190
191   /**
192    * This method allows to restore a long from the input stream.
193    *
194    * @param is the stream to read data from in order to restore the object
195    * @return the long
196    */

197   public static long readLongFrom(InputStream JavaDoc is) throws IOException JavaDoc {
198     byte[] buf = readFully(8, is);
199     return ((((long) buf[0]) &0xFFL) << 56) | ((((long) buf[1]) &0xFFL) << 48) |
200       ((((long) buf[2]) &0xFFL) << 40) | ((((long) buf[3]) &0xFFL) << 32) |
201       ((((long) buf[4]) &0xFFL) << 24) | ((((long) buf[5]) &0xFFL) << 16) |
202       ((((long) buf[6]) &0xFFL) << 8) | (((long) buf[7]) &0xFFL);
203   }
204
205   /**
206    * This method allows to write a float to the output stream.
207    *
208    * @param f the float to write
209    * @param os the stream to write the object to
210    */

211   public static void writeTo(float f, OutputStream JavaDoc os) throws IOException JavaDoc {
212     writeTo(Float.floatToIntBits(f), os);
213   }
214
215   /**
216    * This method allows to restore a float from the input stream.
217    *
218    * @param is the stream to read data from in order to restore the object
219    * @return the float
220    */

221   public static float readFloatFrom(InputStream JavaDoc is) throws IOException JavaDoc {
222     return Float.intBitsToFloat(readIntFrom(is));
223   }
224
225   /**
226    * This method allows to write a double to the output stream.
227    *
228    * @param d the double to write
229    * @param os the stream to write the object to
230    */

231   public static void writeTo(double d, OutputStream JavaDoc os) throws IOException JavaDoc {
232     writeTo(Double.doubleToLongBits(d), os);
233   }
234
235   /**
236    * This method allows to restore a double from the input stream.
237    *
238    * @param is the stream to read data from in order to restore the object
239    * @return the double
240    */

241   public static double readDoubleFrom(InputStream JavaDoc is) throws IOException JavaDoc {
242     return Double.longBitsToDouble(readLongFrom(is));
243   }
244
245   /**
246    * This method allows to write a String to the output stream.
247    *
248    * @param str the String to write
249    * @param os the stream to write the object to
250    */

251   public static void writeTo(String JavaDoc str, OutputStream JavaDoc os) throws IOException JavaDoc {
252     if (str == null) {
253       writeTo(-1, os);
254     } else if (str.length() == 0) {
255       writeTo(0, os);
256     } else {
257       byte[] buf = str.getBytes();
258       writeTo(buf.length, os);
259       os.write(buf);
260     }
261   }
262
263   static final String JavaDoc EMPTY_STRING = "";
264
265   /**
266    * This method allows to restore a String from the input stream.
267    *
268    * @param is the stream to read data from in order to restore the object
269    * @return the String object or null
270    */

271   public static String JavaDoc readStringFrom(InputStream JavaDoc is) throws IOException JavaDoc {
272     int length = readIntFrom(is);
273     if (length == -1) {
274       return null;
275     } else if (length == 0) {
276       return EMPTY_STRING;
277     } else if (length > 0) {
278       byte[] tab = readFully(length, is);
279       return new String JavaDoc(tab, 0, length);
280     } else {
281       throw new IOException JavaDoc("bad string length");
282     }
283   }
284
285   static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
286
287   /**
288    * This method allows to write byte array to the output stream.
289    *
290    * @param tab the byte array to write
291    * @param os the stream to write the object to
292    */

293   public static void writeTo(byte[] tab, OutputStream JavaDoc os) throws IOException JavaDoc {
294     if (tab == null) {
295       writeTo(-1, os);
296     } else if (tab.length == 0) {
297       writeTo(0, os);
298     } else {
299       writeTo(tab.length, os);
300       os.write(tab);
301     }
302   }
303
304   /**
305    * This method allows to restore a byte array from the input stream.
306    *
307    * @param is the stream to read data from in order to restore the object
308    * @return the byte array object or null
309    */

310   public static byte[] readByteArrayFrom(InputStream JavaDoc is) throws IOException JavaDoc {
311     int length = readIntFrom(is);
312     if (length == -1) {
313       return null;
314     } else if (length == 0) {
315       return EMPTY_BYTE_ARRAY;
316     } else if (length > 0) {
317       byte[] tab = new byte[length];
318       readFully(tab, is);
319       return tab;
320     } else {
321       throw new IOException JavaDoc("bad array length");
322     }
323   }
324
325   static final byte NULL = -1;
326   static final byte BOOLEAN = 1;
327   static final byte BYTE = 2;
328   static final byte SHORT = 3;
329   static final byte INT = 4;
330   static final byte LONG = 5;
331   static final byte FLOAT = 6;
332   static final byte DOUBLE = 7;
333   static final byte STRING = 8;
334   static final byte BYTEARRAY = 9;
335
336   /**
337    * This method allows to write an object to the output stream.
338    *
339    * @param obj the object to write
340    * @param os the stream to write the object to
341    */

342   public static void writeObjectTo(Object JavaDoc obj, OutputStream JavaDoc os) throws IOException JavaDoc {
343     if (obj == null) {
344       writeTo(NULL, os);
345     } else if (obj instanceof Boolean JavaDoc) {
346       writeTo(BOOLEAN, os);
347       writeTo(((Boolean JavaDoc) obj).booleanValue(), os);
348     } else if (obj instanceof Byte JavaDoc) {
349       writeTo(BYTE, os);
350       writeTo(((Byte JavaDoc) obj).byteValue(), os);
351     } else if (obj instanceof Short JavaDoc) {
352       writeTo(SHORT, os);
353       writeTo(((Short JavaDoc) obj).shortValue(), os);
354     } else if (obj instanceof Integer JavaDoc) {
355       writeTo(INT, os);
356       writeTo(((Integer JavaDoc) obj).intValue(), os);
357     } else if (obj instanceof Long JavaDoc) {
358       writeTo(LONG, os);
359       writeTo(((Long JavaDoc) obj).longValue(), os);
360     } else if (obj instanceof Float JavaDoc) {
361       writeTo(FLOAT, os);
362       writeTo(((Float JavaDoc) obj).floatValue(), os);
363     } else if (obj instanceof Double JavaDoc) {
364       writeTo(DOUBLE, os);
365       writeTo(((Double JavaDoc) obj).doubleValue(), os);
366     } else if (obj instanceof String JavaDoc) {
367       writeTo(STRING, os);
368       writeTo((String JavaDoc) obj, os);
369     } else if (obj instanceof byte[]) {
370       writeTo(BYTEARRAY, os);
371       writeTo((byte[]) obj, os);
372     } else {
373       throw new InvalidClassException JavaDoc("Bad primitive type");
374     }
375   }
376
377   /**
378    * This method allows to restore an object from the input stream.
379    *
380    * @param is the stream to read data from in order to restore the object
381    * @return the object or null
382    */

383   public static Object JavaDoc readObjectFrom(InputStream JavaDoc is) throws IOException JavaDoc {
384     byte type = readByteFrom(is);
385     switch (type) {
386     case NULL:
387       return null;
388     case BOOLEAN:
389       return new Boolean JavaDoc(readBooleanFrom(is));
390     case BYTE:
391       return new Byte JavaDoc(readByteFrom(is));
392     case SHORT:
393       return new Short JavaDoc(readShortFrom(is));
394     case INT:
395       return new Integer JavaDoc(readIntFrom(is));
396     case LONG:
397       return new Long JavaDoc(readLongFrom(is));
398     case FLOAT:
399       return new Float JavaDoc(readFloatFrom(is));
400     case DOUBLE:
401       return new Double JavaDoc(readDoubleFrom(is));
402     case STRING:
403       return readStringFrom(is);
404     case BYTEARRAY:
405       return readByteArrayFrom(is);
406     default:
407       throw new InvalidClassException JavaDoc("Bad primitive type");
408     }
409   }
410
411   /**
412    * This method allows to write a Properties object to the output stream.
413    *
414    * @param p the Properties object to write
415    * @param os the stream to write the object to
416    */

417   public static void writeTo(Properties p, OutputStream JavaDoc os) throws IOException JavaDoc {
418     if (p == null) {
419       writeTo(-1, os);
420     } else {
421       p.writeTo(os);
422     }
423   }
424
425   /**
426    * This method allows to restore a Properties object from the input stream.
427    *
428    * @param is the stream to read data from in order to restore the object
429    * @return the Properties object or null
430    */

431   public static Properties readPropertiesFrom(InputStream JavaDoc is) throws IOException JavaDoc {
432       return Properties.readFrom(is);
433   }
434
435   /**
436    * This method allows to write a Properties object to the output stream.
437    *
438    * @param p the Properties object to write
439    * @param os the stream to write the object to
440    */

441   public static void writeVectorOfStringTo(Vector JavaDoc v, OutputStream JavaDoc os) throws IOException JavaDoc {
442     if (v == null) {
443       writeTo(-1, os);
444     } else {
445       int size = v.size();
446       writeTo(size, os);
447       for (int i=0; i<size; i++) {
448         writeTo((String JavaDoc) v.elementAt(i), os);
449       }
450     }
451   }
452
453   /**
454    * This method allows to restore a Properties object from the input stream.
455    *
456    * @param is the stream to read data from in order to restore the object
457    * @return the Properties object or null
458    */

459   public static Vector JavaDoc readVectorOfStringFrom(InputStream JavaDoc is) throws IOException JavaDoc {
460     int size = readIntFrom(is);
461     if (size == -1) {
462       return null;
463     } else {
464       Vector JavaDoc v = new Vector JavaDoc(size);
465       for (int i=0; i<size; i++) {
466         v.addElement(readStringFrom(is));
467       }
468       return v;
469     }
470   }
471 }
472
Popular Tags