1 16 17 package org.jboss.axis.utils; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 23 public class IOUtils 24 { 25 28 public static int readFully(InputStream in, byte[] b) 29 throws IOException 30 { 31 return readFully(in, b, 0, b.length); 32 } 33 34 45 public static int readFully(InputStream in, byte[] b, int off, int len) 46 throws IOException 47 { 48 int total = 0; 49 for (; ;) 50 { 51 int got = in.read(b, off + total, len - total); 52 if (got < 0) 53 { 54 return (total == 0) ? -1 : total; 55 } 56 else 57 { 58 total += got; 59 if (total == len) 60 return total; 61 } 62 } 63 } 64 65 67 public static byte[] toByteArray(InputStream is) 68 throws IOException 69 { 70 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 71 byte[] bytes = new byte[1024]; 72 int read = is.read(bytes); 73 while (read > 0) 74 { 75 baos.write(bytes, 0, read); 76 read = is.read(bytes); 77 } 78 bytes = baos.toByteArray(); 79 return bytes; 80 } 81 } | Popular Tags |