1 17 package org.columba.core.io; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 25 28 public class StreamUtils { 29 private static final int BUFFERSIZE = 8000; 30 31 44 public static int streamCopy(InputStream in, OutputStream out, int length) 45 throws IOException { 46 byte[] buffer = new byte[BUFFERSIZE]; 47 int read; 48 int copied = 0; 49 50 while ((read = in 51 .read(buffer, 0, Math.min(BUFFERSIZE, length - copied))) > 0) { 52 out.write(buffer, 0, read); 53 copied += read; 54 } 55 56 return copied; 57 } 58 59 71 public static long streamCopy(InputStream in, OutputStream out) 72 throws IOException { 73 byte[] buffer = new byte[BUFFERSIZE]; 74 int read; 75 long copied = 0; 76 77 while ((read = in.read(buffer)) > 0) { 78 out.write(buffer, 0, read); 79 copied += read; 80 } 81 82 return copied; 83 } 84 85 93 public static StringBuffer readCharacterStream(InputStream in) 94 throws IOException { 95 StringBuffer result = new StringBuffer (in.available()); 96 int read = in.read(); 97 98 while (read > 0) { 99 result.append((char) read); 100 read = in.read(); 101 } 102 103 in.close(); 104 105 return result; 106 } 107 108 public static byte[] readInByteArray(InputStream in) throws IOException { 109 byte[] result = new byte[in.available()]; 110 111 in.read(result); 112 113 in.close(); 114 115 return result; 116 } 117 118 130 public static InputStream streamClone(InputStream from) throws IOException { 131 ByteArrayOutputStream out = new ByteArrayOutputStream (); 132 streamCopy(from, out); 133 134 return new ByteArrayInputStream (out.toByteArray()); 135 } 136 } 137 | Popular Tags |