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 AbstractHttpConnection _connection;
40 protected final AbstractGenerator _generator;
41 private boolean _closed;
42
43
44 String _characterEncoding;
45 Writer _converter;
46 char[] _chars;
47 ByteArrayOutputStream2 _bytes;
48
49
50 public HttpOutput(AbstractHttpConnection connection)
51 {
52 _connection=connection;
53 _generator=(AbstractGenerator)connection.getGenerator();
54 }
55
56
57 public int getMaxIdleTime()
58 {
59 return _connection.getMaxIdleTime();
60 }
61
62
63 public boolean isWritten()
64 {
65 return _generator.getContentWritten()>0;
66 }
67
68
69
70
71
72 @Override
73 public void close() throws IOException
74 {
75 _closed=true;
76 }
77
78
79 public boolean isClosed()
80 {
81 return _closed;
82 }
83
84
85 public void reopen()
86 {
87 _closed=false;
88 }
89
90
91 @Override
92 public void flush() throws IOException
93 {
94 _generator.flush(getMaxIdleTime());
95 }
96
97
98 @Override
99 public void write(byte[] b, int off, int len) throws IOException
100 {
101 write(new ByteArrayBuffer(b,off,len));
102 }
103
104
105
106
107
108 @Override
109 public void write(byte[] b) throws IOException
110 {
111 write(new ByteArrayBuffer(b));
112 }
113
114
115
116
117
118 @Override
119 public void write(int b) throws IOException
120 {
121 if (_closed)
122 throw new IOException("Closed");
123 if (!_generator.isOpen())
124 throw new EofException();
125
126
127 while (_generator.isBufferFull())
128 {
129 _generator.blockForOutput(getMaxIdleTime());
130 if (_closed)
131 throw new IOException("Closed");
132 if (!_generator.isOpen())
133 throw new EofException();
134 }
135
136
137 if (_generator.addContent((byte)b))
138
139 flush();
140
141 if (_generator.isAllContentWritten())
142 {
143 flush();
144 close();
145 }
146 }
147
148
149 private void write(Buffer buffer) throws IOException
150 {
151 if (_closed)
152 throw new IOException("Closed");
153 if (!_generator.isOpen())
154 throw new EofException();
155
156
157 while (_generator.isBufferFull())
158 {
159 _generator.blockForOutput(getMaxIdleTime());
160 if (_closed)
161 throw new IOException("Closed");
162 if (!_generator.isOpen())
163 throw new EofException();
164 }
165
166
167 _generator.addContent(buffer, Generator.MORE);
168
169
170
171 if (_generator.isAllContentWritten())
172 {
173 flush();
174 close();
175 }
176 else if (_generator.isBufferFull())
177 flush();
178
179
180 while (buffer.length() > 0 && _generator.isOpen())
181 {
182 _generator.blockForOutput(getMaxIdleTime());
183 }
184 }
185
186
187
188
189
190 @Override
191 public void print(String s) throws IOException
192 {
193 write(s.getBytes());
194 }
195 }