001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp;
030:
031: import javax.servlet.jsp.JspWriter;
032: import java.io.IOException;
033: import java.io.PrintWriter;
034: import java.io.Writer;
035:
036: /**
037: * A JSP writer encapsulating a Writer.
038: */
039: class StreamJspWriter extends AbstractBodyContent {
040: // the underlying writer
041: private Writer _writer;
042:
043: private JspPrintWriter _printWriter;
044:
045: private JspWriter _parent;
046:
047: // the page context
048: private PageContextImpl _pageContext;
049:
050: private boolean _isClosed;
051:
052: /**
053: * Creates a new StreamJspWriter
054: */
055: StreamJspWriter() {
056: }
057:
058: /**
059: * Initialize the JSP writer
060: *
061: * @param os the underlying stream
062: */
063: void init(JspWriter parent, Writer writer) {
064: _parent = parent;
065: _writer = writer;
066: _isClosed = false;
067: }
068:
069: /**
070: * Returns the print writer.
071: */
072: public PrintWriter getWriter() {
073: if (_printWriter == null)
074: _printWriter = new JspPrintWriter(this );
075:
076: _printWriter.init(this );
077:
078: return _printWriter;
079: }
080:
081: /**
082: * Writes a character array to the writer.
083: *
084: * @param buf the buffer to write.
085: * @param off the offset into the buffer
086: * @param len the number of characters to write
087: */
088: final public void write(char[] buf, int offset, int length)
089: throws IOException {
090: if (_isClosed)
091: return;
092:
093: _writer.write(buf, offset, length);
094: }
095:
096: /**
097: * Writes a character to the output.
098: *
099: * @param buf the buffer to write.
100: */
101: final public void write(int ch) throws IOException {
102: if (_isClosed)
103: return;
104:
105: _writer.write(ch);
106: }
107:
108: /**
109: * Writes the newline character.
110: */
111: final public void newLine() throws IOException {
112: if (_isClosed)
113: return;
114:
115: _writer.write('\n');
116: }
117:
118: /**
119: * Prints a character.
120: */
121: final public void print(char ch) throws IOException {
122: if (_isClosed)
123: return;
124:
125: _writer.write(ch);
126: }
127:
128: /**
129: * Prints the newline.
130: */
131: final public void println() throws IOException {
132: _writer.write('\n');
133: }
134:
135: public void clear() throws IOException {
136: clearBuffer();
137: }
138:
139: public void clearBuffer() throws IOException {
140: }
141:
142: public void flushBuffer() throws IOException {
143: if (_isClosed)
144: return;
145:
146: _writer.flush();
147: }
148:
149: public void flush() throws IOException {
150: _writer.flush();
151: }
152:
153: final public void close() throws IOException {
154: _isClosed = true;
155: }
156:
157: public int getBufferSize() {
158: return 0;
159: }
160:
161: public int getRemaining() {
162: return 0;
163: }
164:
165: AbstractJspWriter popWriter() {
166: AbstractJspWriter parent = super.popWriter();
167:
168: return parent;
169: }
170: }
|