--- /dev/null
+// JigsawServletOutputStream.java\r
+// $Id: JigsawServletOutputStream.java,v 1.1 2010/06/15 12:24:13 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1996.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigsaw.servlet;\r
+\r
+import java.io.DataOutputStream;\r
+import java.io.FilterOutputStream;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+\r
+import javax.servlet.ServletOutputStream;\r
+\r
+import org.w3c.jigsaw.http.Reply;\r
+\r
+/**\r
+ * A Bufferred ServletOutputStream.\r
+ * @author Benoît Mahé <bmahe@w3.org>\r
+ */\r
+\r
+class JigsawServletOutputStream extends ServletOutputStream {\r
+\r
+ DataOutputStream out = null;\r
+ JigsawHttpServletResponse resp = null;\r
+ Reply reply = null;\r
+\r
+ byte buffer[] = null;\r
+ int count = 0;\r
+ boolean committed = false;\r
+ boolean writerUsed = false;\r
+\r
+ byte ln[] = { (byte)'\r' , (byte)'\n' };\r
+\r
+ /**\r
+ * Flush the internal buffer.\r
+ */\r
+ private void flushBuffer() \r
+ throws IOException\r
+ {\r
+ if (!committed) {\r
+ resp.notifyClient();\r
+ }\r
+ committed = true;\r
+ if (out == null) {\r
+ if (reply != null)\r
+ out = new DataOutputStream(reply.getOutputStream());\r
+ }\r
+ if (count > 0) {\r
+ out.write(buffer, 0, count);\r
+ count = 0;\r
+ }\r
+ }\r
+\r
+ public void print(int i)\r
+ throws IOException\r
+ {\r
+ write(i);\r
+ }\r
+\r
+ public void print(double i)\r
+ throws IOException\r
+ {\r
+ print(Double.toString(i));\r
+ }\r
+\r
+ public void print(long l)\r
+ throws IOException\r
+ {\r
+ print(Long.toString(l));\r
+ }\r
+\r
+ public void print(String s) \r
+ throws IOException \r
+ {\r
+ write(s.getBytes());\r
+ }\r
+\r
+ public void println() \r
+ throws IOException\r
+ {\r
+ write(ln);\r
+ }\r
+\r
+ public void println(int i)\r
+ throws IOException\r
+ {\r
+ print(i); println();\r
+ }\r
+\r
+ public void println(double i)\r
+ throws IOException\r
+ {\r
+ print(i); println();\r
+ }\r
+\r
+ public void println(long l) \r
+ throws IOException\r
+ {\r
+ print(l); println();\r
+ }\r
+\r
+ public void println(String s)\r
+ throws IOException\r
+ {\r
+ print(s); println();\r
+ }\r
+\r
+ public void write(int b) \r
+ throws IOException \r
+ {\r
+ write((byte)b);\r
+ }\r
+\r
+ protected void write(byte b) \r
+ throws IOException \r
+ {\r
+ if (count >= buffer.length) {\r
+ flushBuffer();\r
+ }\r
+ buffer[count++] = b;\r
+ }\r
+\r
+ public void write(byte b[]) \r
+ throws IOException \r
+ {\r
+ write(b, 0, b.length);\r
+ }\r
+\r
+ public void write(byte b[], int off, int len) \r
+ throws IOException \r
+ {\r
+ if (len >= buffer.length) {\r
+ flushBuffer();\r
+ out.write(b, off, len);\r
+ return;\r
+ }\r
+ if (len > buffer.length - count) {\r
+ flushBuffer();\r
+ }\r
+ System.arraycopy(b, off, buffer, count, len);\r
+ count += len;\r
+ }\r
+\r
+ public void flush() \r
+ throws IOException \r
+ {\r
+ if (! writerUsed) {\r
+ flushBuffer();\r
+ out.flush();\r
+ }\r
+ }\r
+\r
+ public void realFlush() \r
+ throws IOException \r
+ {\r
+ flushBuffer();\r
+ out.flush();\r
+ }\r
+\r
+ public void close() \r
+ throws IOException \r
+ {\r
+ flushBuffer();\r
+ out.close();\r
+ }\r
+\r
+ public void reset() \r
+ throws IllegalStateException\r
+ {\r
+ if (committed) {\r
+ throw new IllegalStateException("Response already committed");\r
+ }\r
+ // empty the buffer\r
+ count = 0;\r
+ }\r
+\r
+ public boolean isCommitted() {\r
+ return committed;\r
+ }\r
+\r
+ /**\r
+ * Creates a new buffered JigsawServletOutputStream.\r
+ * @param resp the Response\r
+ * @param out The underlying output stream\r
+ * @param bufsize the buffer size\r
+ * @exception IllegalArgumentException if bufsize <= 0.\r
+ */\r
+ JigsawServletOutputStream(JigsawHttpServletResponse resp,\r
+ DataOutputStream out,\r
+ int bufsize,\r
+ boolean writerUsed)\r
+ {\r
+ this.out = out;\r
+ this.resp = resp;\r
+ this.writerUsed = writerUsed;\r
+ if (bufsize <= 0) {\r
+ throw new IllegalArgumentException("Buffer size <= 0");\r
+ }\r
+ this.buffer = new byte[bufsize];\r
+ this.count = 0;\r
+ this.committed = false;\r
+ }\r
+\r
+ /**\r
+ * Creates a new buffered JigsawServletOutputStream.\r
+ * @param resp the Response\r
+ * @param reply the Jigsaw reply\r
+ * @param bufsize the buffer size\r
+ * @exception IllegalArgumentException if bufsize <= 0.\r
+ */\r
+ JigsawServletOutputStream(JigsawHttpServletResponse resp, \r
+ Reply reply,\r
+ int bufsize,\r
+ boolean writerUsed)\r
+ {\r
+ this.resp = resp;\r
+ this.reply = reply;\r
+ this.writerUsed = writerUsed;\r
+ if (bufsize <= 0) {\r
+ throw new IllegalArgumentException("Buffer size <= 0");\r
+ }\r
+ this.buffer = new byte[bufsize];\r
+ this.count = 0;\r
+ this.committed = false;\r
+ }\r
+\r
+}\r
+\r
+\r