1 17 18 package org.objectweb.jac.util; 19 20 import java.io.EOFException ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 25 26 29 public class Streams { 30 31 45 46 public static byte[] readStream(InputStream fin) throws IOException { 47 byte[][] bufs = new byte[8][]; 48 int bufsize = 4096; 49 50 for (int i = 0; i < 8; ++i) { 51 bufs[i] = new byte[bufsize]; 52 int size = 0; 53 int len = 0; 54 do { 55 len = fin.read(bufs[i], size, bufsize - size); 56 if (len >= 0) 57 size += len; 58 else { 59 byte[] result = new byte[bufsize - 4096 + size]; 60 int s = 0; 61 for (int j = 0; j < i; ++j) { 62 System.arraycopy(bufs[j], 0, result, s, s + 4096); 63 s = s + s + 4096; 64 } 65 66 System.arraycopy(bufs[i], 0, result, s, size); 67 return result; 68 } 69 } while (size < bufsize); 70 bufsize *= 2; 71 } 72 throw new IOException ("readStream function has too much data to load."); 73 } 74 75 79 public static long readUInt(InputStream in) throws IOException { 80 long s1 = readUShort(in); 81 long s2 = readUShort(in); 82 return (s2 << 16) | s1; 83 } 84 85 89 public static int readUShort(InputStream in) throws IOException { 90 int b1 = readUByte(in); 91 int b2 = readUByte(in); 92 return (b2 << 8) | b1; 93 } 94 95 99 public static int readUByte(InputStream in) throws IOException { 100 int b = in.read(); 101 if (b == -1) 102 throw new EOFException (); 103 return b; 104 } 105 106 109 public static void copy(InputStream in, OutputStream out) 110 throws IOException 111 { 112 int bufferSize = 1024; 113 byte[] buffer = new byte[bufferSize]; 114 int read; 115 while ((read=in.read(buffer,0,bufferSize))!=-1) { 116 out.write(buffer,0,read); 117 } 118 } 119 } 120 | Popular Tags |