001: // JigsawServletOutputStream.java
002: // $Id: JigsawServletOutputStream.java,v 1.15 2003/02/04 16:22:16 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.servlet;
007:
008: import java.io.DataOutputStream;
009: import java.io.FilterOutputStream;
010: import java.io.IOException;
011: import java.io.OutputStream;
012:
013: import javax.servlet.ServletOutputStream;
014:
015: import org.w3c.jigsaw.http.Reply;
016:
017: /**
018: * A Bufferred ServletOutputStream.
019: * @author Benoît Mahé <bmahe@w3.org>
020: */
021:
022: class JigsawServletOutputStream extends ServletOutputStream {
023:
024: DataOutputStream out = null;
025: JigsawHttpServletResponse resp = null;
026: Reply reply = null;
027:
028: byte buffer[] = null;
029: int count = 0;
030: boolean committed = false;
031: boolean writerUsed = false;
032:
033: byte ln[] = { (byte) '\r', (byte) '\n' };
034:
035: /**
036: * Flush the internal buffer.
037: */
038: private void flushBuffer() throws IOException {
039: if (!committed) {
040: resp.notifyClient();
041: }
042: committed = true;
043: if (out == null) {
044: if (reply != null)
045: out = new DataOutputStream(reply.getOutputStream());
046: }
047: if (count > 0) {
048: out.write(buffer, 0, count);
049: count = 0;
050: }
051: }
052:
053: public void print(int i) throws IOException {
054: write(i);
055: }
056:
057: public void print(double i) throws IOException {
058: print(Double.toString(i));
059: }
060:
061: public void print(long l) throws IOException {
062: print(Long.toString(l));
063: }
064:
065: public void print(String s) throws IOException {
066: write(s.getBytes());
067: }
068:
069: public void println() throws IOException {
070: write(ln);
071: }
072:
073: public void println(int i) throws IOException {
074: print(i);
075: println();
076: }
077:
078: public void println(double i) throws IOException {
079: print(i);
080: println();
081: }
082:
083: public void println(long l) throws IOException {
084: print(l);
085: println();
086: }
087:
088: public void println(String s) throws IOException {
089: print(s);
090: println();
091: }
092:
093: public void write(int b) throws IOException {
094: write((byte) b);
095: }
096:
097: protected void write(byte b) throws IOException {
098: if (count >= buffer.length) {
099: flushBuffer();
100: }
101: buffer[count++] = b;
102: }
103:
104: public void write(byte b[]) throws IOException {
105: write(b, 0, b.length);
106: }
107:
108: public void write(byte b[], int off, int len) throws IOException {
109: if (len >= buffer.length) {
110: flushBuffer();
111: out.write(b, off, len);
112: return;
113: }
114: if (len > buffer.length - count) {
115: flushBuffer();
116: }
117: System.arraycopy(b, off, buffer, count, len);
118: count += len;
119: }
120:
121: public void flush() throws IOException {
122: if (!writerUsed) {
123: flushBuffer();
124: out.flush();
125: }
126: }
127:
128: public void realFlush() throws IOException {
129: flushBuffer();
130: out.flush();
131: }
132:
133: public void close() throws IOException {
134: flushBuffer();
135: out.close();
136: }
137:
138: public void reset() throws IllegalStateException {
139: if (committed) {
140: throw new IllegalStateException(
141: "Response already committed");
142: }
143: // empty the buffer
144: count = 0;
145: }
146:
147: public boolean isCommitted() {
148: return committed;
149: }
150:
151: /**
152: * Creates a new buffered JigsawServletOutputStream.
153: * @param resp the Response
154: * @param out The underlying output stream
155: * @param bufsize the buffer size
156: * @exception IllegalArgumentException if bufsize <= 0.
157: */
158: JigsawServletOutputStream(JigsawHttpServletResponse resp,
159: DataOutputStream out, int bufsize, boolean writerUsed) {
160: this .out = out;
161: this .resp = resp;
162: this .writerUsed = writerUsed;
163: if (bufsize <= 0) {
164: throw new IllegalArgumentException("Buffer size <= 0");
165: }
166: this .buffer = new byte[bufsize];
167: this .count = 0;
168: this .committed = false;
169: }
170:
171: /**
172: * Creates a new buffered JigsawServletOutputStream.
173: * @param resp the Response
174: * @param reply the Jigsaw reply
175: * @param bufsize the buffer size
176: * @exception IllegalArgumentException if bufsize <= 0.
177: */
178: JigsawServletOutputStream(JigsawHttpServletResponse resp,
179: Reply reply, int bufsize, boolean writerUsed) {
180: this .resp = resp;
181: this .reply = reply;
182: this .writerUsed = writerUsed;
183: if (bufsize <= 0) {
184: throw new IllegalArgumentException("Buffer size <= 0");
185: }
186: this .buffer = new byte[bufsize];
187: this .count = 0;
188: this .committed = false;
189: }
190:
191: }
|