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 com.caucho.log.Log;
032: import com.caucho.server.connection.AbstractResponseStream;
033: import com.caucho.util.L10N;
034: import com.caucho.vfs.Encoding;
035: import com.caucho.vfs.TempBuffer;
036:
037: import java.io.IOException;
038: import java.io.InputStream;
039: import java.io.Reader;
040: import java.io.Writer;
041: import java.util.logging.Level;
042: import java.util.logging.Logger;
043:
044: public class BodyResponseStream extends AbstractResponseStream {
045: static final Logger log = Logger.getLogger(BodyResponseStream.class
046: .getName());
047:
048: static final L10N L = new L10N(BodyResponseStream.class);
049:
050: private static final int SIZE = TempBuffer.SIZE;
051:
052: private final byte[] _byteBuffer = new byte[SIZE];
053: private final char[] _charBuffer = new char[SIZE];
054:
055: private Writer _out;
056: private String _encoding;
057:
058: private BufferInputStream _in;
059: private Reader _encodingReader;
060:
061: public BodyResponseStream() {
062: }
063:
064: /**
065: * Returns true for a caucho response stream.
066: */
067: public boolean isCauchoResponseStream() {
068: return true;
069: }
070:
071: public void setWriter(Writer out) {
072: _out = out;
073: _encoding = null;
074: _encodingReader = null;
075: }
076:
077: public void setEncoding(String encoding) {
078: _encoding = encoding;
079: }
080:
081: /**
082: * Sets the buffer size.
083: */
084: public void setBufferSize(int size) {
085: }
086:
087: /**
088: * Gets the buffer size.
089: */
090: public int getBufferSize() {
091: return SIZE;
092: }
093:
094: /**
095: * Returns the remaining buffer entries.
096: */
097: public int getRemaining() {
098: return SIZE;
099: }
100:
101: /**
102: * Returns the char buffer.
103: */
104: public char[] getCharBuffer() {
105: return _charBuffer;
106: }
107:
108: /**
109: * Returns the char buffer offset.
110: */
111: public int getCharOffset() {
112: return 0;
113: }
114:
115: /**
116: * Sets the char buffer offset.
117: */
118: public void setCharOffset(int offset) {
119: if (offset > 0) {
120: try {
121: _out.write(_charBuffer, 0, offset);
122: } catch (IOException e) {
123: log.log(Level.FINER, e.toString(), e);
124: }
125: }
126: }
127:
128: /**
129: * Returns the next char buffer.
130: */
131: public char[] nextCharBuffer(int offset) throws IOException {
132: if (offset > 0)
133: _out.write(_charBuffer, 0, offset);
134:
135: return _charBuffer;
136: }
137:
138: /**
139: * Writes the next chunk of data to the response stream.
140: *
141: * @param buf the buffer containing the data
142: * @param offset start offset into the buffer
143: * @param length length of the data in the buffer
144: */
145: public void print(char[] buf, int offset, int length)
146: throws IOException {
147: if (length == 0)
148: return;
149:
150: _out.write(buf, offset, length);
151: }
152:
153: /**
154: * Writes the next chunk of data to the response stream.
155: *
156: * @param buf the buffer containing the data
157: * @param offset start offset into the buffer
158: * @param length length of the data in the buffer
159: */
160: public void print(int ch) throws IOException {
161: _out.write(ch);
162: }
163:
164: /**
165: * Returns the buffer offset.
166: */
167: public int getBufferOffset() {
168: return 0;
169: }
170:
171: /**
172: * Sets the byte buffer offset.
173: */
174: public void setBufferOffset(int offset) {
175: if (offset > 0) {
176: try {
177: write(_byteBuffer, 0, offset);
178: } catch (IOException e) {
179: log.log(Level.FINE, e.toString(), e);
180: }
181: }
182: }
183:
184: /**
185: * Gets the byte buffer
186: */
187: public byte[] getBuffer() {
188: return _byteBuffer;
189: }
190:
191: /**
192: * Returns the next buffer.
193: */
194: public byte[] nextBuffer(int offset) throws IOException {
195: if (offset > 0)
196: write(_byteBuffer, 0, offset);
197:
198: return _byteBuffer;
199: }
200:
201: /**
202: * Writes the next chunk of data to the response stream.
203: *
204: * @param buf the buffer containing the data
205: * @param offset start offset into the buffer
206: * @param length length of the data in the buffer
207: */
208: public void write(byte[] buf, int offset, int length)
209: throws IOException {
210: if (length == 0)
211: return;
212:
213: if (_encodingReader == null) {
214: if (_in == null)
215: _in = new BufferInputStream();
216: _encodingReader = Encoding.getReadEncoding(_in, _encoding);
217: }
218:
219: if (_encodingReader == null) {
220: for (; length > 0; length--) {
221: print((char) (buf[offset++] & 0xff));
222: }
223: return;
224: }
225:
226: _in.init(buf, offset, length);
227:
228: int ch;
229: while ((ch = _encodingReader.read()) >= 0) {
230: print(ch);
231: }
232: }
233:
234: /**
235: * Writes the next chunk of data to the response stream.
236: *
237: * @param buf the buffer containing the data
238: * @param offset start offset into the buffer
239: * @param length length of the data in the buffer
240: */
241: public void write(int ch) throws IOException {
242: if (_encodingReader == null) {
243: if (_in == null)
244: _in = new BufferInputStream();
245: _byteBuffer[0] = (byte) ch;
246: _encodingReader = Encoding.getReadEncoding(_in, _encoding);
247: }
248:
249: if (_encodingReader == null) {
250: print((char) ch);
251: return;
252: }
253:
254: _in.init(_byteBuffer, 0, 1);
255:
256: while ((ch = _encodingReader.read()) >= 0) {
257: print(ch);
258: }
259: }
260:
261: /**
262: * Flushes the buffer.
263: */
264: public void flushBuffer() throws IOException {
265: }
266:
267: /**
268: * Clears the buffer.
269: */
270: public void clearBuffer() {
271: }
272:
273: static class BufferInputStream extends InputStream {
274: private byte[] _buffer;
275: private int _offset;
276: private int _length;
277:
278: void init(byte[] buffer, int offset, int length) {
279: _buffer = buffer;
280: _offset = offset;
281: _length = length;
282: }
283:
284: public int read() {
285: if (_offset < _length)
286: return _buffer[_offset++] & 0xff;
287: else
288: return -1;
289: }
290: }
291: }
|