Frames | No Frames |
1: /** 2: * ======================================== 3: * JFreeReport : a free Java report library 4: * ======================================== 5: * 6: * Project Info: http://reporting.pentaho.org/ 7: * 8: * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. 9: * 10: * This library is free software; you can redistribute it and/or modify it under the terms 11: * of the GNU Lesser General Public License as published by the Free Software Foundation; 12: * either version 2.1 of the License, or (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 15: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16: * See the GNU Lesser General Public License for more details. 17: * 18: * You should have received a copy of the GNU Lesser General Public License along with this 19: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20: * Boston, MA 02111-1307, USA. 21: * 22: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 23: * in the United States and other countries.] 24: * 25: * ------------ 26: * $Id: MemoryStringWriter.java 3525 2007-10-16 11:43:48Z tmorgner $ 27: * ------------ 28: * (C) Copyright 2000-2005, by Object Refinery Limited. 29: * (C) Copyright 2005-2007, by Pentaho Corporation. 30: */ 31: 32: package org.jfree.report.util; 33: 34: import java.io.IOException; 35: import java.io.Writer; 36: 37: /** 38: * A string writer that is able to write large amounts of data. The original StringWriter contained in Java doubles 39: * its buffersize everytime the buffer overflows. This is nice with small amounts of data, but awfull for huge 40: * buffers. 41: * 42: * @author Thomas Morgner 43: */ 44: public class MemoryStringWriter extends Writer 45: { 46: private int bufferIncrement; 47: private int cursor; 48: private char[] buffer; 49: 50: /** 51: * Create a new character-stream writer whose critical sections will synchronize on the writer itself. 52: */ 53: public MemoryStringWriter() 54: { 55: this(4096); 56: } 57: 58: /** 59: * Create a new character-stream writer whose critical sections will synchronize on the writer itself. 60: */ 61: public MemoryStringWriter(final int bufferSize) 62: { 63: this.bufferIncrement = bufferSize; 64: this.buffer = new char[bufferSize]; 65: } 66: 67: /** 68: * Write a portion of an array of characters. 69: * 70: * @param cbuf Array of characters 71: * @param off Offset from which to start writing characters 72: * @param len Number of characters to write 73: * @throws java.io.IOException If an I/O error occurs 74: */ 75: public synchronized void write(final char[] cbuf, final int off, final int len) throws IOException 76: { 77: if (len < 0) 78: { 79: throw new IllegalArgumentException(); 80: } 81: if (off < 0) 82: { 83: throw new IndexOutOfBoundsException(); 84: } 85: if (cbuf == null) 86: { 87: throw new NullPointerException(); 88: } 89: if ((len + off) > cbuf.length) 90: { 91: throw new IndexOutOfBoundsException(); 92: } 93: 94: ensureSize (cursor + len); 95: 96: System.arraycopy(cbuf, off, this.buffer, cursor, len); 97: cursor += len; 98: } 99: 100: private void ensureSize(final int size) 101: { 102: if (this.buffer.length >= size) 103: { 104: return; 105: } 106: 107: final int newSize = Math.max (size, this.buffer.length + bufferIncrement); 108: final char[] newBuffer = new char[newSize]; 109: System.arraycopy(this.buffer, 0, newBuffer, 0, cursor); 110: } 111: 112: /** 113: * Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them 114: * immediately to their intended destination. Then, if that destination is another character or byte stream, flush 115: * it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams. 116: * <p/> 117: * If the intended destination of this stream is an abstraction provided by the underlying operating system, for 118: * example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to 119: * the operating system for writing; it does not guarantee that they are actually written to a physical device such as 120: * a disk drive. 121: * 122: * @throws java.io.IOException If an I/O error occurs 123: */ 124: public void flush() throws IOException 125: { 126: 127: } 128: 129: /** 130: * Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will 131: * cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect. 132: * 133: * @throws java.io.IOException If an I/O error occurs 134: */ 135: public void close() throws IOException 136: { 137: } 138: 139: public String toString () 140: { 141: return new String (buffer, 0, cursor); 142: } 143: }