01: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
02: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
03: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
04: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
05: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
06: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
07: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
08: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
09: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
11: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
12: // POSSIBILITY OF SUCH DAMAGE.
13: //
14: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
15: package com.metaboss.util;
16:
17: import java.io.FileNotFoundException;
18: import java.io.IOException;
19: import java.io.InputStream;
20: import java.io.OutputStream;
21:
22: /** Set of useful utilites to do with streams */
23: public class StreamUtils {
24: /** Copies contents of the from stream to the destination stream. */
25: public static void copyData(InputStream pSourceStream,
26: OutputStream pTargetStream) throws FileNotFoundException,
27: IOException {
28: byte[] lBuffer = new byte[4096]; // 4k chunks
29: int lBytesRead;
30: while ((lBytesRead = pSourceStream.read(lBuffer)) != -1)
31: pTargetStream.write(lBuffer, 0, lBytesRead);
32: }
33:
34: /** Copies certain length of data from the source stream to the destination.
35: * Will block until specified number of bytes is available */
36: public static void copyData(InputStream pSourceStream,
37: OutputStream pTargetStream, long pLength)
38: throws FileNotFoundException, IOException {
39: byte[] lBuffer = new byte[4096]; // 4k chunks
40: long lBytesDone = 0;
41: long lBytesOutstanding = 0;
42: while ((lBytesOutstanding = (pLength - lBytesDone)) > 0) {
43: int lBytesRead = (lBytesOutstanding > lBuffer.length) ? pSourceStream
44: .read(lBuffer)
45: : pSourceStream.read(lBuffer, 0,
46: (int) lBytesOutstanding);
47: if (lBytesRead > 0) {
48: pTargetStream.write(lBuffer, 0, lBytesRead);
49: lBytesDone += lBytesRead;
50: }
51: }
52: }
53: }
|