Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawServletOutputStream.java, revision 1.16
1.1 abaird 1: // JigsawServletOutputStream.java
1.16 ! ylafon 2: // $Id: JigsawServletOutputStream.java,v 1.15 2003/02/04 16:22:16 ylafon Exp $
1.1 abaird 3: // (c) COPYRIGHT MIT and INRIA, 1996.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
1.6 bmahe 6: package org.w3c.jigsaw.servlet;
1.1 abaird 7:
1.12 ylafon 8: import java.io.DataOutputStream;
9: import java.io.IOException;
10: import javax.servlet.ServletOutputStream;
1.8 bmahe 11: import org.w3c.jigsaw.http.Reply;
12:
1.1 abaird 13: /**
1.10 bmahe 14: * A Bufferred ServletOutputStream.
1.16 ! ylafon 15: *
1.10 bmahe 16: * @author Benoît Mahé <bmahe@w3.org>
1.1 abaird 17: */
18:
19: class JigsawServletOutputStream extends ServletOutputStream {
1.13 bmahe 20:
1.16 ! ylafon 21: DataOutputStream out = null;
! 22: JigsawHttpServletResponse resp = null;
! 23: Reply reply = null;
! 24:
! 25: byte buffer[] = null;
! 26: int count = 0;
! 27: boolean committed = false;
1.11 bmahe 28: boolean writerUsed = false;
1.10 bmahe 29:
1.16 ! ylafon 30: byte ln[] = {(byte) '\r', (byte) '\n'};
1.8 bmahe 31:
32: /**
1.10 bmahe 33: * Flush the internal buffer.
1.8 bmahe 34: */
1.16 ! ylafon 35: private void flushBuffer()
! 36: throws IOException {
! 37: if (!committed) {
! 38: resp.notifyClient();
! 39: }
! 40: committed = true;
! 41: if (out == null) {
! 42: if (reply != null)
! 43: out = new DataOutputStream(reply.getOutputStream());
! 44: }
! 45: if (count > 0) {
! 46: out.write(buffer, 0, count);
! 47: count = 0;
! 48: }
1.8 bmahe 49: }
1.1 abaird 50:
51: public void print(int i)
1.16 ! ylafon 52: throws IOException {
! 53: write(i);
1.1 abaird 54: }
55:
1.7 bmahe 56: public void print(double i)
1.16 ! ylafon 57: throws IOException {
! 58: print(Double.toString(i));
1.7 bmahe 59: }
60:
1.5 bmahe 61: public void print(long l)
1.16 ! ylafon 62: throws IOException {
! 63: print(Long.toString(l));
1.1 abaird 64: }
65:
1.16 ! ylafon 66: public void print(String s)
! 67: throws IOException {
! 68: write(s.getBytes());
1.1 abaird 69: }
70:
1.16 ! ylafon 71: public void println()
! 72: throws IOException {
! 73: write(ln);
1.1 abaird 74: }
75:
76: public void println(int i)
1.16 ! ylafon 77: throws IOException {
! 78: print(i);
! 79: println();
1.7 bmahe 80: }
81:
82: public void println(double i)
1.16 ! ylafon 83: throws IOException {
! 84: print(i);
! 85: println();
1.1 abaird 86: }
87:
1.16 ! ylafon 88: public void println(long l)
! 89: throws IOException {
! 90: print(l);
! 91: println();
1.1 abaird 92: }
93:
94: public void println(String s)
1.16 ! ylafon 95: throws IOException {
! 96: print(s);
! 97: println();
! 98: }
! 99:
! 100: public void write(int b)
! 101: throws IOException {
! 102: write((byte) b);
! 103: }
! 104:
! 105: protected void write(byte b)
! 106: throws IOException {
! 107: if (count >= buffer.length) {
! 108: flushBuffer();
! 109: }
! 110: buffer[count++] = b;
! 111: }
! 112:
! 113: public void write(byte b[])
! 114: throws IOException {
! 115: write(b, 0, b.length);
! 116: }
! 117:
! 118: public void write(byte b[], int off, int len)
! 119: throws IOException {
! 120: if (len >= buffer.length) {
! 121: flushBuffer();
! 122: out.write(b, off, len);
! 123: return;
! 124: }
! 125: if (len > buffer.length - count) {
! 126: flushBuffer();
! 127: }
! 128: System.arraycopy(b, off, buffer, count, len);
! 129: count += len;
! 130: }
! 131:
! 132: public void flush()
! 133: throws IOException {
! 134: if (!writerUsed) {
! 135: flushBuffer();
! 136: out.flush();
! 137: }
! 138: }
! 139:
! 140: public void realFlush()
! 141: throws IOException {
! 142: flushBuffer();
! 143: out.flush();
! 144: }
! 145:
! 146: public void close()
! 147: throws IOException {
! 148: flushBuffer();
! 149: out.close();
! 150: }
! 151:
! 152: public void reset()
! 153: throws IllegalStateException {
! 154: if (committed) {
! 155: throw new IllegalStateException("Response already committed");
! 156: }
! 157: // empty the buffer
! 158: count = 0;
1.10 bmahe 159: }
160:
161: public boolean isCommitted() {
1.16 ! ylafon 162: return committed;
1.10 bmahe 163: }
164:
165: /**
166: * Creates a new buffered JigsawServletOutputStream.
1.16 ! ylafon 167: *
! 168: * @param resp the Response
! 169: * @param out The underlying output stream
1.10 bmahe 170: * @param bufsize the buffer size
1.16 ! ylafon 171: * @throws IllegalArgumentException if bufsize <= 0.
1.10 bmahe 172: */
1.9 ylafon 173: JigsawServletOutputStream(JigsawHttpServletResponse resp,
1.16 ! ylafon 174: DataOutputStream out,
! 175: int bufsize,
! 176: boolean writerUsed) {
! 177: this.out = out;
! 178: this.resp = resp;
! 179: this.writerUsed = writerUsed;
! 180: if (bufsize <= 0) {
! 181: throw new IllegalArgumentException("Buffer size <= 0");
! 182: }
! 183: this.buffer = new byte[bufsize];
! 184: this.count = 0;
! 185: this.committed = false;
1.8 bmahe 186: }
187:
1.10 bmahe 188: /**
189: * Creates a new buffered JigsawServletOutputStream.
1.16 ! ylafon 190: *
! 191: * @param resp the Response
! 192: * @param reply the Jigsaw reply
1.10 bmahe 193: * @param bufsize the buffer size
1.16 ! ylafon 194: * @throws IllegalArgumentException if bufsize <= 0.
1.10 bmahe 195: */
1.16 ! ylafon 196: JigsawServletOutputStream(JigsawHttpServletResponse resp,
! 197: Reply reply,
! 198: int bufsize,
! 199: boolean writerUsed) {
! 200: this.resp = resp;
! 201: this.reply = reply;
! 202: this.writerUsed = writerUsed;
! 203: if (bufsize <= 0) {
! 204: throw new IllegalArgumentException("Buffer size <= 0");
! 205: }
! 206: this.buffer = new byte[bufsize];
! 207: this.count = 0;
! 208: this.committed = false;
1.1 abaird 209: }
210:
211: }
1.12 ylafon 212:
1.1 abaird 213:
Webmaster