1 23 package org.objectweb.joram.shared.stream; 24 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.io.IOException ; 28 import java.io.EOFException ; 29 import java.io.InvalidClassException ; 30 31 import java.util.Vector ; 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 private static ThreadLocal perThreadBuffer = new ThreadLocal () { 41 protected synchronized Object initialValue() { 42 return new byte[8]; 43 } 44 }; 45 46 private static byte[] readFully(int length, InputStream is) throws IOException { 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 (); 56 count += nb; 57 } while (count != length); 58 return buf; 59 } 60 61 private static void readFully(byte[] buf, InputStream is) throws IOException { 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 (); 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 81 public static void writeTo(boolean b, OutputStream os) throws IOException { 82 if (b) 83 os.write(TRUE); 84 else 85 os.write(FALSE); 86 } 87 88 94 public static boolean readBooleanFrom(InputStream is) throws IOException { 95 if (is.read() == TRUE) 96 return true; 97 else 98 return false; 99 } 100 101 107 public static void writeTo(byte b, OutputStream os) throws IOException { 108 os.write(b); 109 } 110 111 117 public static byte readByteFrom(InputStream is) throws IOException { 118 return (byte) is.read(); 119 } 120 121 127 public static void writeTo(short s, OutputStream os) throws IOException { 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 140 public static short readShortFrom(InputStream is) throws IOException { 141 byte[] buf = readFully(2, is); 142 return (short) (((buf[0] &0xFF) << 8) | (buf[1] &0xFF)); 143 } 144 145 151 public static void writeTo(int i, OutputStream os) throws IOException { 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 166 public static int readIntFrom(InputStream is) throws IOException { 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 178 public static void writeTo(long l, OutputStream os) throws IOException { 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 197 public static long readLongFrom(InputStream is) throws IOException { 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 211 public static void writeTo(float f, OutputStream os) throws IOException { 212 writeTo(Float.floatToIntBits(f), os); 213 } 214 215 221 public static float readFloatFrom(InputStream is) throws IOException { 222 return Float.intBitsToFloat(readIntFrom(is)); 223 } 224 225 231 public static void writeTo(double d, OutputStream os) throws IOException { 232 writeTo(Double.doubleToLongBits(d), os); 233 } 234 235 241 public static double readDoubleFrom(InputStream is) throws IOException { 242 return Double.longBitsToDouble(readLongFrom(is)); 243 } 244 245 251 public static void writeTo(String str, OutputStream os) throws IOException { 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 EMPTY_STRING = ""; 264 265 271 public static String readStringFrom(InputStream is) throws IOException { 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 (tab, 0, length); 280 } else { 281 throw new IOException ("bad string length"); 282 } 283 } 284 285 static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; 286 287 293 public static void writeTo(byte[] tab, OutputStream os) throws IOException { 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 310 public static byte[] readByteArrayFrom(InputStream is) throws IOException { 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 ("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 342 public static void writeObjectTo(Object obj, OutputStream os) throws IOException { 343 if (obj == null) { 344 writeTo(NULL, os); 345 } else if (obj instanceof Boolean ) { 346 writeTo(BOOLEAN, os); 347 writeTo(((Boolean ) obj).booleanValue(), os); 348 } else if (obj instanceof Byte ) { 349 writeTo(BYTE, os); 350 writeTo(((Byte ) obj).byteValue(), os); 351 } else if (obj instanceof Short ) { 352 writeTo(SHORT, os); 353 writeTo(((Short ) obj).shortValue(), os); 354 } else if (obj instanceof Integer ) { 355 writeTo(INT, os); 356 writeTo(((Integer ) obj).intValue(), os); 357 } else if (obj instanceof Long ) { 358 writeTo(LONG, os); 359 writeTo(((Long ) obj).longValue(), os); 360 } else if (obj instanceof Float ) { 361 writeTo(FLOAT, os); 362 writeTo(((Float ) obj).floatValue(), os); 363 } else if (obj instanceof Double ) { 364 writeTo(DOUBLE, os); 365 writeTo(((Double ) obj).doubleValue(), os); 366 } else if (obj instanceof String ) { 367 writeTo(STRING, os); 368 writeTo((String ) obj, os); 369 } else if (obj instanceof byte[]) { 370 writeTo(BYTEARRAY, os); 371 writeTo((byte[]) obj, os); 372 } else { 373 throw new InvalidClassException ("Bad primitive type"); 374 } 375 } 376 377 383 public static Object readObjectFrom(InputStream is) throws IOException { 384 byte type = readByteFrom(is); 385 switch (type) { 386 case NULL: 387 return null; 388 case BOOLEAN: 389 return new Boolean (readBooleanFrom(is)); 390 case BYTE: 391 return new Byte (readByteFrom(is)); 392 case SHORT: 393 return new Short (readShortFrom(is)); 394 case INT: 395 return new Integer (readIntFrom(is)); 396 case LONG: 397 return new Long (readLongFrom(is)); 398 case FLOAT: 399 return new Float (readFloatFrom(is)); 400 case DOUBLE: 401 return new Double (readDoubleFrom(is)); 402 case STRING: 403 return readStringFrom(is); 404 case BYTEARRAY: 405 return readByteArrayFrom(is); 406 default: 407 throw new InvalidClassException ("Bad primitive type"); 408 } 409 } 410 411 417 public static void writeTo(Properties p, OutputStream os) throws IOException { 418 if (p == null) { 419 writeTo(-1, os); 420 } else { 421 p.writeTo(os); 422 } 423 } 424 425 431 public static Properties readPropertiesFrom(InputStream is) throws IOException { 432 return Properties.readFrom(is); 433 } 434 435 441 public static void writeVectorOfStringTo(Vector v, OutputStream os) throws IOException { 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 ) v.elementAt(i), os); 449 } 450 } 451 } 452 453 459 public static Vector readVectorOfStringFrom(InputStream is) throws IOException { 460 int size = readIntFrom(is); 461 if (size == -1) { 462 return null; 463 } else { 464 Vector v = new Vector (size); 465 for (int i=0; i<size; i++) { 466 v.addElement(readStringFrom(is)); 467 } 468 return v; 469 } 470 } 471 } 472 | Popular Tags |