1 36 package org.columba.ristretto.io; 37 38 import java.io.ByteArrayInputStream ; 39 import java.io.ByteArrayOutputStream ; 40 import java.io.IOException ; 41 import java.io.InputStream ; 42 import java.io.OutputStream ; 43 import java.nio.ByteBuffer ; 44 import java.nio.charset.Charset ; 45 import java.util.logging.Logger ; 46 47 54 public class StreamUtils { 55 56 private static final Logger LOG = Logger 57 .getLogger("org.columba.ristretto.message.io"); 58 59 60 private static final int BUFFERSIZE = 8000; 61 62 71 public static long streamCopy(InputStream _isInput, OutputStream _osOutput, int _iBufSize) throws IOException { 72 byte[] _aBuffer = new byte[_iBufSize]; 73 int _iBytesRead; 74 long _lBytesCopied = 0; 75 while ((_iBytesRead = _isInput.read(_aBuffer)) > 0 ) { 76 _osOutput.write(_aBuffer, 0, _iBytesRead); 77 _lBytesCopied += _iBytesRead; 78 _osOutput.flush(); 79 } 80 LOG.finer("Copied " + _lBytesCopied + "bytes"); 81 82 return _lBytesCopied; 83 } 84 85 92 public static long streamCopy(InputStream _isInput, OutputStream _osOutput ) throws IOException { 93 return streamCopy( _isInput, _osOutput, BUFFERSIZE ); 94 } 95 96 97 105 public static StringBuffer readInString( InputStream in ) throws IOException { 106 StringBuffer result = new StringBuffer (in.available()); 107 int read = in.read(); 108 109 while (read > 0) { 110 result.append((char) read); 111 read = in.read(); 112 } 113 114 in.close(); 115 116 return result; 117 } 118 119 126 public static InputStream streamClone(InputStream from) throws IOException { 127 ByteArrayOutputStream out = new ByteArrayOutputStream (); 128 streamCopy(from, out); 129 return new ByteArrayInputStream (out.toByteArray()); 130 } 131 132 } 133 | Popular Tags |