01: /**
02: * $RCSfile: $
03: * $Revision: $
04: * $Date: $
05: *
06: * Copyright (C) 2006 Jive Software. All rights reserved.
07: * This software is the proprietary information of Jive Software. Use is subject to license terms.
08: */package org.jivesoftware.util;
09:
10: import java.io.InputStream;
11: import java.io.OutputStream;
12: import java.util.concurrent.Callable;
13:
14: /**
15: * Callable which will read from an input stream and write to an output stream.
16: *
17: * @author Alexander Wenckus
18: */
19: public class InputOutputStreamWrapper implements Callable {
20: private static final int DEFAULT_BUFFER_SIZE = 8000;
21:
22: private long amountWritten = 0;
23: private int bufferSize;
24: private InputStream in;
25: private OutputStream out;
26:
27: public InputOutputStreamWrapper(InputStream in, OutputStream out,
28: int bufferSize) {
29: if (bufferSize <= 0) {
30: bufferSize = DEFAULT_BUFFER_SIZE;
31: }
32:
33: this .bufferSize = bufferSize;
34: this .in = in;
35: this .out = out;
36: }
37:
38: public InputOutputStreamWrapper(InputStream in, OutputStream out) {
39: this (in, out, DEFAULT_BUFFER_SIZE);
40: }
41:
42: public Object call() throws Exception {
43: final byte[] b = new byte[bufferSize];
44: int count = 0;
45: amountWritten = 0;
46:
47: do {
48: // write to the output stream
49: out.write(b, 0, count);
50:
51: amountWritten += count;
52:
53: // read more bytes from the input stream
54: count = in.read(b);
55: } while (count >= 0);
56:
57: return amountWritten;
58: }
59:
60: public long getAmountWritten() {
61: return amountWritten;
62: }
63: }
|