001: /**
002: * ========================================
003: * JFreeReport : a free Java report library
004: * ========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * $Id: MemoryStringWriter.java 3525 2007-10-16 11:43:48Z tmorgner $
027: * ------------
028: * (C) Copyright 2000-2005, by Object Refinery Limited.
029: * (C) Copyright 2005-2007, by Pentaho Corporation.
030: */package org.jfree.report.util;
031:
032: import java.io.IOException;
033: import java.io.Writer;
034:
035: /**
036: * A string writer that is able to write large amounts of data. The original StringWriter contained in Java doubles
037: * its buffersize everytime the buffer overflows. This is nice with small amounts of data, but awfull for huge
038: * buffers.
039: *
040: * @author Thomas Morgner
041: */
042: public class MemoryStringWriter extends Writer {
043: private int bufferIncrement;
044: private int cursor;
045: private char[] buffer;
046:
047: /**
048: * Create a new character-stream writer whose critical sections will synchronize on the writer itself.
049: */
050: public MemoryStringWriter() {
051: this (4096);
052: }
053:
054: /**
055: * Create a new character-stream writer whose critical sections will synchronize on the writer itself.
056: */
057: public MemoryStringWriter(final int bufferSize) {
058: this .bufferIncrement = bufferSize;
059: this .buffer = new char[bufferSize];
060: }
061:
062: /**
063: * Write a portion of an array of characters.
064: *
065: * @param cbuf Array of characters
066: * @param off Offset from which to start writing characters
067: * @param len Number of characters to write
068: * @throws java.io.IOException If an I/O error occurs
069: */
070: public synchronized void write(final char[] cbuf, final int off,
071: final int len) throws IOException {
072: if (len < 0) {
073: throw new IllegalArgumentException();
074: }
075: if (off < 0) {
076: throw new IndexOutOfBoundsException();
077: }
078: if (cbuf == null) {
079: throw new NullPointerException();
080: }
081: if ((len + off) > cbuf.length) {
082: throw new IndexOutOfBoundsException();
083: }
084:
085: ensureSize(cursor + len);
086:
087: System.arraycopy(cbuf, off, this .buffer, cursor, len);
088: cursor += len;
089: }
090:
091: private void ensureSize(final int size) {
092: if (this .buffer.length >= size) {
093: return;
094: }
095:
096: final int newSize = Math.max(size, this .buffer.length
097: + bufferIncrement);
098: final char[] newBuffer = new char[newSize];
099: System.arraycopy(this .buffer, 0, newBuffer, 0, cursor);
100: }
101:
102: /**
103: * Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them
104: * immediately to their intended destination. Then, if that destination is another character or byte stream, flush
105: * it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.
106: * <p/>
107: * If the intended destination of this stream is an abstraction provided by the underlying operating system, for
108: * example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to
109: * the operating system for writing; it does not guarantee that they are actually written to a physical device such as
110: * a disk drive.
111: *
112: * @throws java.io.IOException If an I/O error occurs
113: */
114: public void flush() throws IOException {
115:
116: }
117:
118: /**
119: * Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will
120: * cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.
121: *
122: * @throws java.io.IOException If an I/O error occurs
123: */
124: public void close() throws IOException {
125: }
126:
127: public String toString() {
128: return new String(buffer, 0, cursor);
129: }
130: }
|