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 @Override
63 public void close() throws IOException
64 {
65 _closed=true;
66 }
67
68
69 public void reopen()
70 {
71 _closed=false;
72 }
73
74
75 @Override
76 public void flush() throws IOException
77 {
78 _generator.flush(_maxIdleTime);
79 }
80
81
82 @Override
83 public void write(byte[] b, int off, int len) throws IOException
84 {
85 _buf.wrap(b, off, len);
86 write(_buf);
87 _buf.wrap(AbstractGenerator.NO_BYTES);
88 }
89
90
91
92
93
94 @Override
95 public void write(byte[] b) throws IOException
96 {
97 _buf.wrap(b);
98 write(_buf);
99 _buf.wrap(AbstractGenerator.NO_BYTES);
100 }
101
102
103
104
105
106 @Override
107 public void write(int b) throws IOException
108 {
109 if (_closed)
110 throw new IOException("Closed");
111 if (!_generator.isOpen())
112 throw new EofException();
113
114
115 while (_generator.isBufferFull())
116 {
117 _generator.blockForOutput(_maxIdleTime);
118 if (_closed)
119 throw new IOException("Closed");
120 if (!_generator.isOpen())
121 throw new EofException();
122 }
123
124
125 if (_generator.addContent((byte)b))
126
127 flush();
128
129 if (_generator.isContentWritten())
130 {
131 flush();
132 close();
133 }
134 }
135
136
137 private void write(Buffer buffer) throws IOException
138 {
139 if (_closed)
140 throw new IOException("Closed");
141 if (!_generator.isOpen())
142 throw new EofException();
143
144
145 while (_generator.isBufferFull())
146 {
147 _generator.blockForOutput(_maxIdleTime);
148 if (_closed)
149 throw new IOException("Closed");
150 if (!_generator.isOpen())
151 throw new EofException();
152 }
153
154
155 _generator.addContent(buffer, Generator.MORE);
156
157
158 if (_generator.isBufferFull())
159 flush();
160
161 if (_generator.isContentWritten())
162 {
163 flush();
164 close();
165 }
166
167
168 while (buffer.length() > 0 && _generator.isOpen())
169 _generator.blockForOutput(_maxIdleTime);
170 }
171
172
173
174
175
176 @Override
177 public void print(String s) throws IOException
178 {
179 write(s.getBytes());
180 }
181 }