1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.server;
15
16 import java.io.IOException;
17 import java.io.Writer;
18
19 import javax.servlet.ServletOutputStream;
20
21 import org.eclipse.jetty.http.AbstractGenerator;
22 import org.eclipse.jetty.http.Generator;
23 import org.eclipse.jetty.io.Buffer;
24 import org.eclipse.jetty.io.ByteArrayBuffer;
25 import org.eclipse.jetty.io.EofException;
26 import org.eclipse.jetty.util.ByteArrayOutputStream2;
27
28
29
30
31
32
33
34
35
36
37 public class HttpOutput extends ServletOutputStream
38 {
39 protected final AbstractGenerator _generator;
40 protected final long _maxIdleTime;
41 protected final ByteArrayBuffer _buf = new ByteArrayBuffer(AbstractGenerator.NO_BYTES);
42 protected boolean _closed;
43
44
45 String _characterEncoding;
46 Writer _converter;
47 char[] _chars;
48 ByteArrayOutputStream2 _bytes;
49
50
51
52 public HttpOutput(AbstractGenerator generator, long maxIdleTime)
53 {
54 _generator=generator;
55 _maxIdleTime=maxIdleTime;
56 }
57
58
59
60
61
62 public void close() throws IOException
63 {
64 _closed=true;
65 }
66
67
68 public void reopen()
69 {
70 _closed=false;
71 }
72
73
74 public void flush() throws IOException
75 {
76 _generator.flush(_maxIdleTime);
77 }
78
79
80 public void write(byte[] b, int off, int len) throws IOException
81 {
82 _buf.wrap(b, off, len);
83 write(_buf);
84 }
85
86
87
88
89
90 public void write(byte[] b) throws IOException
91 {
92 _buf.wrap(b);
93 write(_buf);
94 }
95
96
97
98
99
100 public void write(int b) throws IOException
101 {
102 if (_closed)
103 throw new IOException("Closed");
104 if (!_generator.isOpen())
105 throw new EofException();
106
107
108 while (_generator.isBufferFull())
109 {
110 _generator.blockForOutput(_maxIdleTime);
111 if (_closed)
112 throw new IOException("Closed");
113 if (!_generator.isOpen())
114 throw new EofException();
115 }
116
117
118 if (_generator.addContent((byte)b))
119
120 flush();
121
122 if (_generator.isContentWritten())
123 {
124 flush();
125 close();
126 }
127 }
128
129
130 private void write(Buffer buffer) throws IOException
131 {
132 if (_closed)
133 throw new IOException("Closed");
134 if (!_generator.isOpen())
135 throw new EofException();
136
137
138 while (_generator.isBufferFull())
139 {
140 _generator.blockForOutput(_maxIdleTime);
141 if (_closed)
142 throw new IOException("Closed");
143 if (!_generator.isOpen())
144 throw new EofException();
145 }
146
147
148 _generator.addContent(buffer, Generator.MORE);
149
150
151 if (_generator.isBufferFull())
152 flush();
153
154 if (_generator.isContentWritten())
155 {
156 flush();
157 close();
158 }
159
160
161 while (buffer.length() > 0 && _generator.isOpen())
162 _generator.blockForOutput(_maxIdleTime);
163 }
164
165
166
167
168
169 public void print(String s) throws IOException
170 {
171 write(s.getBytes());
172 }
173 }