001: /*
002: * ContentBuffer.java December 2005
003: *
004: * Copyright (C) 2005, Niall Gallagher <niallg@users.sf.net>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General
016: * Public License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
018: * Boston, MA 02111-1307 USA
019: */
020:
021: package simple.http.serve;
022:
023: import java.io.OutputStream;
024: import java.io.IOException;
025: import simple.util.Buffer;
026:
027: /**
028: * The <code>ContentBuffer</code> object is used to encapsulate a
029: * resources contents, such that the <code>Content</code> data
030: * can be easily converted to a string. This is provided so that
031: * the <code>Content.toString</code> method can produce a UTF-8
032: * string encoding of the targeted resource, which can be useful
033: * if a template object is to be embedded within another string.
034: *
035: * @author Niall Gallagher
036: */
037: final class ContentBuffer extends OutputStream {
038:
039: /**
040: * The content object used to fill this buffers contents.
041: */
042: private Content out;
043:
044: /**
045: * This is the stream used to capture the content output.
046: */
047: private Buffer buf;
048:
049: /**
050: * Constructor for the <code>ContentBuffer</code>. This is
051: * used to capture the output of the issued content, which
052: * can be acquired from UTF-8 using <code>toString</code>.
053: *
054: * @param out the content to capture the output from
055: */
056: public ContentBuffer(Content out) {
057: this .buf = new Buffer();
058: this .out = out;
059: }
060:
061: /**
062: * This method is used to accumulate the bytes for the content
063: * object. The bytes that are buffered are used to convert
064: * the resource to a string using UTF-8. This allows the data
065: * to be embedded into other stringsa and manipulated easily.
066: *
067: * @param b this is the byte to append to the byte buffer
068: */
069: public void write(int b) {
070: buf.append(b);
071: }
072:
073: /**
074: * This method is used to accumulate the bytes for the content
075: * object. The bytes that are buffered are used to convert
076: * the resource to a string using UTF-8. This allows the data
077: * to be embedded into other stringsa and manipulated easily.
078: *
079: * @param b this is the byte array to append to the buffer
080: * @param off this is the index to bytes must be taken from
081: * @param len this is the number of bytes to be taken
082: */
083: public void write(byte[] b, int off, int len) {
084: buf.append(b, off, len);
085: }
086:
087: /**
088: * This method is used so that if there is an exception the
089: * stack trace can be printed in place of the content. This
090: * is very important when it comes to tracking down bugs
091: * within a web application as it highlights the problem.
092: *
093: * @param cause this is the cause of the content error
094: *
095: * @return returns a string representation of the error
096: */
097: public String toString(Exception cause) {
098: return new ExceptionBuffer(cause).getCause();
099: }
100:
101: /**
102: * This method will use the specified <code>Content</code> to
103: * provide a UTF-8 encoding of its contents. This will capture
104: * the contents of the object only once, subsequent requests
105: * will use a cached value of the contents. This is done to
106: * reduce the I/O required to capture the resource contents.
107: *
108: * @return this returns a UTF-8 decoding of the content
109: */
110: public String toString() {
111: if (buf.length() > 0) {
112: return buf.toString();
113: }
114: try {
115: out.write(this );
116: } catch (Exception cause) {
117: return toString(cause);
118: }
119: return buf.toString();
120: }
121: }
|