00001
package com.quadcap.http.server22;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.ByteArrayOutputStream;
00042
import java.io.BufferedOutputStream;
00043
import java.io.IOException;
00044
import java.io.OutputStream;
00045
00046
import javax.servlet.ServletOutputStream;
00047
00048
import com.quadcap.net.server.WorkerOutputStream;
00049
00050
import com.quadcap.util.Debug;
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 public class HttpOutputStream extends ServletOutputStream {
00069 WorkerOutputStream
os;
00070 HttpResponse res;
00071 ByteArrayOutputStream
bos =
new ByteArrayOutputStream();
00072 boolean committed =
false;
00073 boolean autoFlush =
true;
00074
00075 int bufferSize = 32 * 1024;
00076
00077
00078
00079
00080
00081
00082
00083 public void reset(WorkerOutputStream os) {
00084
this.os = os;
00085
this.bos.reset();
00086
this.committed =
false;
00087 }
00088
00089
00090
00091
00092
00093
00094 public void write(
int b)
throws IOException {
00095
if (
committed) {
00096
os.write(b);
00097 }
else if (
bos.size() >=
bufferSize) {
00098
res.
writeHeaders();
00099
bos.writeTo(
os);
00100
committed =
true;
00101
os.write(b);
00102 }
else {
00103
bos.write(b);
00104 }
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 public void write(byte[] buf,
int off,
int len)
throws IOException {
00117
if (
committed) {
00118
os.write(buf, off, len);
00119 }
else if (
bos.size() + len >=
bufferSize) {
00120
res.
writeHeaders();
00121
bos.writeTo(
os);
00122
committed =
true;
00123
os.write(buf, off, len);
00124 }
else {
00125
bos.write(buf, off, len);
00126 }
00127 }
00128
00129
00130
00131
00132 public final void flushBuffered() throws IOException {
00133
if (!
committed) {
00134
int len =
bos.size();
00135
if (len > 0) {
00136
res.
maybeSetContentLength(len);
00137 }
00138
res.
writeHeaders();
00139
bos.writeTo(
os);
00140
committed =
true;
00141 }
00142
os.flush();
00143 }
00144
00145
00146
00147
00148 public final void flush() throws IOException {
00149
flushBuffered();
00150 }
00151
00152
00153
00154
00155
00156 public final void close() throws IOException {
00157
flushBuffered();
00158 }
00159
00160 public final void reallyClose() throws IOException {
00161
os.close();
00162 }
00163
00164
00165
00166
00167
00168 public boolean discard() {
00169
if (!
committed) {
00170
bos.reset();
00171
return true;
00172 }
00173
return false;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183 void setResponse(
HttpResponse res) {
00184
this.res = res;
00185 }
00186
00187
00188
00189
00190 WorkerOutputStream
getOutputStream() {
return os; }
00191
00192 final void setBufferSize(
int size)
throws IllegalStateException {
00193
if (
committed) {
00194
throw new IllegalStateException(
"Response already committed");
00195 }
00196
this.bufferSize = size;
00197 }
00198
00199 final int getBufferSize() {
00200
return bufferSize;
00201 }
00202
00203 final boolean isCommitted() {
00204
return committed;
00205 }
00206
00207 final void reset() throws IllegalStateException {
00208
if (
committed) {
00209
throw new IllegalStateException(
"Response already committed");
00210 }
00211
bos.reset();
00212 }
00213
00214 final boolean getAutoFlush() {
00215
return autoFlush;
00216 }
00217
00218 final void setAutoFlush(
boolean autoFlush) {
00219
this.autoFlush = autoFlush;
00220 }
00221
00222 public int getRemaining() {
00223
return bufferSize -
bos.size();
00224 }
00225 }