2
* Copyright 1999,2004 The Apache Software Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
package org.jboss.remoting.transport.coyote;
21
import java.io.IOException;
22
import java.io.Writer;
23
import java.security.AccessController;
24
import java.security.PrivilegedActionException;
25
import java.security.PrivilegedExceptionAction;
26
import java.util.HashMap;
27
import org.apache.coyote.ActionCode;
28
import org.apache.coyote.Response;
29
import org.apache.tomcat.util.buf.ByteChunk;
30
import org.apache.tomcat.util.buf.C2BConverter;
31
import org.apache.tomcat.util.buf.CharChunk;
32
import org.jboss.remoting.util.SecurityUtility;
36
* The buffer used by Tomcat response. This is a derivative of the Tomcat 3.3
37
* OutputBuffer, with the removal of some of the state handling (which in
38
* Coyote is mostly the Processor's responsability).
40
* @author Costin Manolache
41
* @author Remy Maucherat
43
public class OutputBuffer extends Writer
44
implements ByteChunk.ByteOutputChannel, CharChunk.CharOutputChannel
47
// -------------------------------------------------------------- Constants
50
public static final String DEFAULT_ENCODING =
51
org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING;
52
public static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
55
// The buffer can be used for byte[] and char[] writing
56
// ( this is needed to support ServletOutputStream and for
57
// efficient implementations of templating systems )
58
public final int INITIAL_STATE = 0;
59
public final int CHAR_STATE = 1;
60
public final int BYTE_STATE = 2;
62
// ----------------------------------------------------- Instance Variables
78
* State of the output buffer.
80
private int state = 0;
84
* Number of bytes written.
86
private int bytesWritten = 0;
90
* Number of chars written.
92
private int charsWritten = 0;
96
* Flag which indicates if the output buffer is closed.
98
private boolean closed = false;
102
* Do a flush on the next operation.
104
private boolean doFlush = false;
108
* Byte chunk used to output bytes.
110
private ByteChunk outputChunk = new ByteChunk();
122
private boolean gotEnc = false;
128
protected HashMap encoders = new HashMap();
132
* Current char to byte converter.
134
protected C2BConverter conv;
138
* Associated Coyote response.
140
private Response coyoteResponse;
144
* Suspended flag. All output bytes will be swallowed if this is true.
146
private boolean suspended = false;
148
// ----------------------------------------------------------- Constructors
152
* Default constructor. Allocate the buffer with the default buffer size.
154
public OutputBuffer()
157
this(DEFAULT_BUFFER_SIZE);
163
* Alternate constructor which allows specifying the initial buffer size.
165
* @param size Buffer size to use
167
public OutputBuffer(int size)
170
bb = new ByteChunk(size);
171
// Allow buffer to grow, to avoid using chunking
172
// FIXME: Allowing chunking could be a better idea
174
bb.setByteOutputChannel(this);
175
cb = new CharChunk(size);
176
cb.setCharOutputChannel(this);
181
// ------------------------------------------------------------- Properties
185
* Associated Coyote response.
187
* @param coyoteResponse Associated Coyote response
189
public void setResponse(Response coyoteResponse)
191
this.coyoteResponse = coyoteResponse;
196
* Get associated Coyote response.
198
* @return the associated Coyote response
200
public Response getResponse()
202
return this.coyoteResponse;
207
* Is the response output suspended ?
209
* @return suspended flag value
211
public boolean isSuspended()
213
return this.suspended;
218
* Set the suspended flag.
220
* @param suspended New suspended flag value
222
public void setSuspended(boolean suspended)
224
this.suspended = suspended;
227
// --------------------------------------------------------- Public Methods
231
* Recycle the output buffer.
233
public void recycle()
236
state = INITIAL_STATE;
257
* Close the output buffer. This tries to calculate the response size if
258
* the response has not been committed yet.
260
* @throws IOException An underlying IOException occurred
275
if((!coyoteResponse.isCommitted())
276
&& (coyoteResponse.getContentLengthLong() == -1))
278
// Flushing the char buffer
279
if(state == CHAR_STATE)
284
// If this didn't cause a commit of the response, the final content
285
// length can be calculated
286
if(!coyoteResponse.isCommitted())
288
coyoteResponse.setContentLength(bb.getLength());
295
coyoteResponse.finish();
301
* Flush bytes or chars contained in the buffer.
303
* @throws IOException An underlying IOException occurred
313
* Flush bytes or chars contained in the buffer.
315
* @throws IOException An underlying IOException occurred
317
protected void doFlush(boolean realFlush)
327
if(state == CHAR_STATE)
333
else if(state == BYTE_STATE)
337
else if(state == INITIAL_STATE)
339
// If the buffers are empty, commit the response header
340
coyoteResponse.sendHeaders();
346
coyoteResponse.action(ActionCode.ACTION_CLIENT_FLUSH,
348
// If some exception occurred earlier, or if some IOE occurred
349
// here, notify the servlet with an IOE
350
if(coyoteResponse.isExceptionPresent())
352
throw new ClientAbortException
353
(coyoteResponse.getErrorException());
359
// ------------------------------------------------- Bytes Handling Methods
363
* Sends the buffer data to the client output, checking the
364
* state of Response and calling the right interceptors.
366
* @param buf Byte buffer to be written to the response
369
* @throws IOException An underlying IOException occurred
371
public void realWriteBytes(byte buf[], int off, int cnt)
379
if(coyoteResponse == null)
384
// If we really have something to write
387
// real write to the adapter
388
outputChunk.setBytes(buf, off, cnt);
391
coyoteResponse.doWrite(outputChunk);
395
// An IOException on a write is almost always due to
396
// the remote client aborting the request. Wrap this
397
// so that it can be handled better by the error dispatcher.
398
throw new ClientAbortException(e);
405
public void write(byte b[], int off, int len) throws IOException
413
if(state == CHAR_STATE)
418
writeBytes(b, off, len);
423
private void writeBytes(byte b[], int off, int len)
432
bb.append(b, off, len);
435
// if called from within flush(), then immediately flush
445
public void writeByte(int b)
454
if(state == CHAR_STATE)
465
// ------------------------------------------------- Chars Handling Methods
468
public void write(int c)
485
public void write(char c[])
494
write(c, 0, c.length);
499
public void write(char c[], int off, int len)
510
cb.append(c, off, len);
516
public void write(StringBuffer sb)
527
int len = sb.length();
535
* Append a string to the buffer
537
public void write(String s, int off, int len)
553
cb.append(s, off, len);
558
public void write(String s)
572
write(s, 0, s.length());
577
public void flushChars()
587
public boolean flushCharsNeeded()
589
return state == CHAR_STATE;
593
public void setEncoding(String s)
599
public void realWriteChars(char c[], int off, int len)
608
// The following has been updated to conform to jbossweb 2.1.
609
cb.setChars(c, off, len);
610
while (cb.getLength() > 0) {
611
conv.setByteChunk(bb);
612
conv.convert(cb.getChars(), 0, cb.getLength());
613
if (cb.getLength() > 0) {
621
public void checkConverter()
633
protected void setConverter()
637
if(coyoteResponse != null)
639
enc = coyoteResponse.getCharacterEncoding();
645
enc = DEFAULT_ENCODING;
647
conv = (C2BConverter) encoders.get(enc);
649
// The following has been updated to conform to jbossweb 2.1.
653
if(!SecurityUtility.skipAccessControl())
657
conv = (C2BConverter) AccessController.doPrivileged(
658
new PrivilegedExceptionAction()
661
public Object run() throws IOException
663
return new C2BConverter(enc);
669
catch(PrivilegedActionException ex)
671
Exception e = ex.getException();
672
if(e instanceof IOException)
674
throw (IOException) e;
680
conv = new C2BConverter(enc);
683
encoders.put(enc, conv);
688
// -------------------- BufferedOutputStream compatibility
692
* Real write - this buffer will be sent to the client
694
public void flushBytes()
703
public int getBytesWritten()
709
public int getCharsWritten()
715
public int getContentWritten()
717
return bytesWritten + charsWritten;
722
* True if this buffer hasn't been used ( since recycle() ) -
723
* i.e. no chars or bytes have been added to the buffer.
725
public boolean isNew()
727
return (bytesWritten == 0) && (charsWritten == 0);
731
public void setBufferSize(int size)
733
if(size > bb.getLimit())
750
state = INITIAL_STATE;
754
public int getBufferSize()
756
return bb.getLimit();