comparison src/org/eclipse/jetty/server/HttpOutput.java @ 972:5ee36654b383

simplify AbstractHttpConnection
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 15 Oct 2016 22:42:05 -0600
parents 3428c60d7cfc
children dbecd7faa1f5
comparison
equal deleted inserted replaced
971:f997df37cec1 972:5ee36654b383
39 * to a {@link AbstractGenerator}. The class is designed to be reused 39 * to a {@link AbstractGenerator}. The class is designed to be reused
40 * and can be reopened after a close. 40 * and can be reopened after a close.
41 */ 41 */
42 public class HttpOutput extends ServletOutputStream 42 public class HttpOutput extends ServletOutputStream
43 { 43 {
44 protected final AbstractHttpConnection _connection; 44 protected final AbstractHttpConnection _connection;
45 protected final AbstractGenerator _generator; 45 protected final AbstractGenerator _generator;
46 private boolean _closed; 46 private boolean _closed;
47 private ByteArrayBuffer _onebyte; 47 private ByteArrayBuffer _onebyte;
48 48
49 // These are held here for reuse by Writer 49 // These are held here for reuse by Writer
50 String _characterEncoding; 50 String _characterEncoding;
51 Writer _converter; 51 Writer _converter;
52 char[] _chars; 52 char[] _chars;
53 ByteArrayOutputStream2 _bytes; 53 ByteArrayOutputStream2 _bytes;
54 54
55 /* ------------------------------------------------------------ */ 55 /* ------------------------------------------------------------ */
56 public HttpOutput(AbstractHttpConnection connection) 56 public HttpOutput(AbstractHttpConnection connection)
57 { 57 {
58 _connection=connection; 58 _connection = connection;
59 _generator=(AbstractGenerator)connection.getGenerator(); 59 _generator = connection._generator;
60 } 60 }
61 61
62 /* ------------------------------------------------------------ */ 62 /* ------------------------------------------------------------ */
63 public int getMaxIdleTime() 63 public int getMaxIdleTime()
64 { 64 {
65 return _connection.getMaxIdleTime(); 65 return _connection.getMaxIdleTime();
66 } 66 }
67 67
68 /* ------------------------------------------------------------ */ 68 /* ------------------------------------------------------------ */
69 public boolean isWritten() 69 public boolean isWritten()
70 { 70 {
71 return _generator.getContentWritten()>0; 71 return _generator.getContentWritten()>0;
72 } 72 }
73 73
74 /* ------------------------------------------------------------ */ 74 /* ------------------------------------------------------------ */
75 /* 75 /*
76 * @see java.io.OutputStream#close() 76 * @see java.io.OutputStream#close()
77 */ 77 */
78 @Override 78 @Override
79 public void close() throws IOException 79 public void close() throws IOException
80 { 80 {
81 _closed=true; 81 _closed=true;
82 } 82 }
83 83
84 /* ------------------------------------------------------------ */ 84 /* ------------------------------------------------------------ */
85 public boolean isClosed() 85 public boolean isClosed()
86 { 86 {
87 return _closed; 87 return _closed;
88 } 88 }
89 89
90 /* ------------------------------------------------------------ */ 90 /* ------------------------------------------------------------ */
91 public void reopen() 91 public void reopen()
92 { 92 {
93 _closed=false; 93 _closed=false;
94 } 94 }
95 95
96 /* ------------------------------------------------------------ */ 96 /* ------------------------------------------------------------ */
97 @Override 97 @Override
98 public void flush() throws IOException 98 public void flush() throws IOException
99 { 99 {
100 _generator.flush(getMaxIdleTime()); 100 _generator.flush(getMaxIdleTime());
101 } 101 }
102 102
103 /* ------------------------------------------------------------ */ 103 /* ------------------------------------------------------------ */
104 @Override 104 @Override
105 public void write(byte[] b, int off, int len) throws IOException 105 public void write(byte[] b, int off, int len) throws IOException
106 { 106 {
107 write(new ByteArrayBuffer(b,off,len)); 107 write(new ByteArrayBuffer(b,off,len));
108 } 108 }
109 109
110 /* ------------------------------------------------------------ */ 110 /* ------------------------------------------------------------ */
111 /* 111 /*
112 * @see java.io.OutputStream#write(byte[]) 112 * @see java.io.OutputStream#write(byte[])
113 */ 113 */
114 @Override 114 @Override
115 public void write(byte[] b) throws IOException 115 public void write(byte[] b) throws IOException
116 { 116 {
117 write(new ByteArrayBuffer(b)); 117 write(new ByteArrayBuffer(b));
118 } 118 }
119 119
120 120
121 /* ------------------------------------------------------------ */ 121 /* ------------------------------------------------------------ */
122 /* 122 /*
123 * @see java.io.OutputStream#write(int) 123 * @see java.io.OutputStream#write(int)
124 */ 124 */
125 @Override 125 @Override
126 public void write(int b) throws IOException 126 public void write(int b) throws IOException
127 { 127 {
128 if (_onebyte==null) 128 if (_onebyte==null)
129 _onebyte=new ByteArrayBuffer(1); 129 _onebyte=new ByteArrayBuffer(1);
130 else 130 else
131 _onebyte.clear(); 131 _onebyte.clear();
132 _onebyte.put((byte)b); 132 _onebyte.put((byte)b);
133 write(_onebyte); 133 write(_onebyte);
134 } 134 }
135 135
136 /* ------------------------------------------------------------ */ 136 /* ------------------------------------------------------------ */
137 private void write(Buffer buffer) throws IOException 137 private void write(Buffer buffer) throws IOException
138 { 138 {
139 if (_closed) 139 if (_closed)
140 throw new IOException("Closed"); 140 throw new IOException("Closed");
141 if (!_generator.isOpen()) 141 if (!_generator.isOpen())
142 throw new EofException(); 142 throw new EofException();
143 143
144 // Block until we can add _content. 144 // Block until we can add _content.
145 while (_generator.isBufferFull()) 145 while (_generator.isBufferFull())
146 { 146 {
147 _generator.blockForOutput(getMaxIdleTime()); 147 _generator.blockForOutput(getMaxIdleTime());
148 if (_closed) 148 if (_closed)
149 throw new IOException("Closed"); 149 throw new IOException("Closed");
150 if (!_generator.isOpen()) 150 if (!_generator.isOpen())
151 throw new EofException(); 151 throw new EofException();
152 } 152 }
153 153
154 // Add the _content 154 // Add the _content
155 _generator.addContent(buffer, Generator.MORE); 155 _generator.addContent(buffer, Generator.MORE);
156 156
157 // Have to flush and complete headers? 157 // Have to flush and complete headers?
158 158
159 if (_generator.isAllContentWritten()) 159 if (_generator.isAllContentWritten())
160 { 160 {
161 flush(); 161 flush();
162 close(); 162 close();
163 } 163 }
164 else if (_generator.isBufferFull()) 164 else if (_generator.isBufferFull())
165 _connection.commitResponse(Generator.MORE); 165 _connection.commitResponse(Generator.MORE);
166 166
167 // Block until our buffer is free 167 // Block until our buffer is free
168 while (buffer.length() > 0 && _generator.isOpen()) 168 while (buffer.length() > 0 && _generator.isOpen())
169 { 169 {
170 _generator.blockForOutput(getMaxIdleTime()); 170 _generator.blockForOutput(getMaxIdleTime());
171 } 171 }
172 } 172 }
173 173
174 /* ------------------------------------------------------------ */ 174 /* ------------------------------------------------------------ */
175 /* 175 /*
176 * @see javax.servlet.ServletOutputStream#print(java.lang.String) 176 * @see javax.servlet.ServletOutputStream#print(java.lang.String)
177 */ 177 */
178 @Override 178 @Override
179 public void print(String s) throws IOException 179 public void print(String s) throws IOException
180 { 180 {
181 write(s.getBytes()); 181 write(s.getBytes());
182 } 182 }
183 } 183 }