01: package pygmy.core;
02:
03: import java.io.OutputStream;
04: import java.io.IOException;
05: import java.io.InputStream;
06:
07: public class InputStreamResponseData implements ResponseData {
08: InputStream theData;
09: long offset = 0;
10: long length = -1;
11:
12: private static final int SEND_BUFFER_SIZE = 4096;
13:
14: public InputStreamResponseData(InputStream theData, long length) {
15: this .theData = theData;
16: this .length = length;
17: }
18:
19: public InputStreamResponseData(InputStream theData, long offset,
20: long length) {
21: this .theData = theData;
22: this .offset = offset;
23: this .length = length;
24: }
25:
26: public long getLength() {
27: if (offset < length) {
28: return length - offset;
29: } else {
30: return -1;
31: }
32: }
33:
34: public void send(OutputStream os) throws IOException {
35: theData.skip(offset);
36: byte[] buffer = new byte[Math.min(SEND_BUFFER_SIZE,
37: (int) (length > 0L ? length : Integer.MAX_VALUE))];
38: int totalSent = 0;
39: try {
40: // startTransfer();
41: while (true) {
42: int bufLen = theData.read(buffer);
43: if (bufLen < 0) {
44: break;
45: }
46: // notifyListeners( totalSent, length );
47: os.write(buffer, 0, bufLen);
48: totalSent += bufLen;
49: }
50: } catch (IOException e) {
51: throw e;
52: } finally {
53: // endTransfer();
54: }
55:
56: }
57: }
|