01: package pygmy.core;
02:
03: import java.util.List;
04: import java.util.LinkedList;
05: import java.util.Iterator;
06: import java.io.InputStream;
07: import java.io.OutputStream;
08: import java.io.IOException;
09: import java.io.PrintWriter;
10:
11: public class ResponseDataList {
12: List dataStreamList = new LinkedList();
13:
14: public void addResponse(ResponseData data) {
15: dataStreamList.add(data);
16: }
17:
18: public void addResponse(InputStream stream, long length) {
19: addResponse(stream, 0, length);
20: }
21:
22: public void addResponse(InputStream stream, long offset, long length) {
23: addResponse(new InputStreamResponseData(stream, offset, length));
24: }
25:
26: public PrintWriter addPrintWriter() {
27: PrintWriterResponseData data = new PrintWriterResponseData();
28: addResponse(data);
29: return data.getPrintWriter();
30: }
31:
32: public long getTotalLength() {
33: long total = 0;
34: for (Iterator it = dataStreamList.iterator(); it.hasNext();) {
35: ResponseData responseData = (ResponseData) it.next();
36: long len = responseData.getLength();
37: total = (total >= 0 && len > 0) ? total + len : -1;
38: }
39: return total;
40: }
41:
42: public void sendData(OutputStream os, boolean isChunkedOk)
43: throws IOException {
44: try {
45: if (getTotalLength() < 0 && isChunkedOk) {
46: os = new ChunkedEncodingOutputStream(os);
47: }
48: for (Iterator it = dataStreamList.iterator(); it.hasNext();) {
49: ResponseData responseData = (ResponseData) it.next();
50: responseData.send(os);
51: }
52: } finally {
53: dataStreamList.clear();
54: os.flush();
55: os.close();
56: }
57: }
58:
59: public void reset() {
60: dataStreamList.clear();
61: }
62:
63: }
|