01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.zerog.ia.customcode.util.fileutils;
05:
06: import java.io.*;
07:
08: public class OtherUtils {
09: private static final int BUFFER_SIZE = 64 * 1024;
10: private static byte[] BUFFER = null;
11:
12: /**
13: * Create a buffer of the size indicated, and copy the InputStream to the
14: * OutputStream fully.
15: *
16: * @returns the number of bytes copied
17: */
18: public static long bufStreamCopy(InputStream is, OutputStream os,
19: int bufferSizeInBytes) throws IOException {
20: byte[] b = new byte[bufferSizeInBytes];
21:
22: return bufStreamCopy(is, os, b);
23: }
24:
25: /**
26: * Copy the InputStream to the OutputStream fully, using a shared buffer.
27: *
28: * @returns the number of bytes copied
29: */
30: public static long bufStreamCopy(InputStream is, OutputStream os)
31: throws IOException {
32: /* Lazily allocate this chunk. */
33: if (BUFFER == null) {
34: BUFFER = new byte[BUFFER_SIZE];
35: }
36:
37: synchronized (BUFFER) {
38: return bufStreamCopy(is, os, BUFFER);
39: }
40: }
41:
42: /**
43: * Copy the InputStream to the OutputStream fully, using the
44: * indicated buffer.
45: *
46: * @returns the number of bytes copied
47: */
48: public static long bufStreamCopy(InputStream is, OutputStream os,
49: byte[] b) throws IOException {
50: long bytes = 0;
51:
52: int read = is.read(b, 0, b.length);
53: while (read != -1) {
54: os.write(b, 0, read);
55: bytes += read;
56: read = is.read(b, 0, b.length);
57: }
58:
59: os.flush();
60:
61: return bytes;
62: }
63: }
|