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: MemoryByteArrayOutputStream.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.OutputStream;
034:
035: import org.jfree.util.Log;
036:
037: /**
038: * A string writer that is able to write large amounts of data. The original StringWriter contained in Java doubles
039: * its buffersize everytime the buffer overflows. This is nice with small amounts of data, but awfull for huge
040: * buffers.
041: *
042: * @author Thomas Morgner
043: */
044: public class MemoryByteArrayOutputStream extends OutputStream {
045: private int initialBufferSize;
046: private int maximumBufferIncrement;
047: private int cursor;
048: private byte[] buffer;
049: private byte[] singleIntArray;
050:
051: /**
052: * Create a new character-stream writer whose critical sections will synchronize on the writer itself.
053: */
054: public MemoryByteArrayOutputStream() {
055: this (4096, 65536);
056: }
057:
058: /**
059: * Create a new character-stream writer whose critical sections will synchronize on the writer itself.
060: */
061: public MemoryByteArrayOutputStream(final int bufferSize,
062: final int maximumBufferIncrement) {
063: this .initialBufferSize = bufferSize;
064: this .maximumBufferIncrement = maximumBufferIncrement;
065: this .buffer = new byte[bufferSize];
066: this .singleIntArray = new byte[1];
067: }
068:
069: /**
070: * Write a portion of an array of characters.
071: *
072: * @param cbuf Array of characters
073: * @param off Offset from which to start writing characters
074: * @param len Number of characters to write
075: * @throws java.io.IOException If an I/O error occurs
076: */
077: public synchronized void write(final byte[] cbuf, final int off,
078: final int len) throws IOException {
079: if (len < 0) {
080: throw new IllegalArgumentException();
081: }
082: if (off < 0) {
083: throw new IndexOutOfBoundsException();
084: }
085: if (cbuf == null) {
086: throw new NullPointerException();
087: }
088: if ((len + off) > cbuf.length) {
089: throw new IndexOutOfBoundsException();
090: }
091:
092: ensureSize(cursor + len);
093:
094: System.arraycopy(cbuf, off, this .buffer, cursor, len);
095: cursor += len;
096: }
097:
098: /**
099: * Writes <code>b.length</code> bytes from the specified byte array to this output stream. The general contract for
100: * <code>write(b)</code> is that it should have exactly the same effect as the call <code>write(b, 0,
101: * b.length)</code>.
102: *
103: * @param b the data.
104: * @throws java.io.IOException if an I/O error occurs.
105: * @see java.io.OutputStream#write(byte[], int, int)
106: */
107: public void write(final byte[] b) throws IOException {
108: write(b, 0, b.length);
109: }
110:
111: /**
112: * Writes the specified byte to this output stream. The general contract for <code>write</code> is that one byte is
113: * written to the output stream. The byte to be written is the eight low-order bits of the argument <code>b</code>.
114: * The 24 high-order bits of <code>b</code> are ignored.
115: * <p/>
116: * Subclasses of <code>OutputStream</code> must provide an implementation for this method.
117: *
118: * @param b the <code>byte</code>.
119: * @throws java.io.IOException if an I/O error occurs. In particular, an <code>IOException</code> may be thrown if the
120: * output stream has been closed.
121: */
122: public synchronized void write(final int b) throws IOException {
123: this .singleIntArray[0] = (byte) (0xFF & b);
124: write(singleIntArray, 0, 1);
125: }
126:
127: private void ensureSize(final int size) {
128: if (this .buffer.length >= size) {
129: return;
130: }
131:
132: final int computedSize = (int) Math.min(
133: (this .buffer.length + 1) * 1.5, this .buffer.length
134: + maximumBufferIncrement);
135: final int newSize = Math.max(size, computedSize);
136: final byte[] newBuffer = new byte[newSize];
137: System.arraycopy(this .buffer, 0, newBuffer, 0, cursor);
138: this .buffer = newBuffer;
139: }
140:
141: /**
142: * Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them
143: * immediately to their intended destination. Then, if that destination is another character or byte stream, flush
144: * it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.
145: * <p/>
146: * If the intended destination of this stream is an abstraction provided by the underlying operating system, for
147: * example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to
148: * the operating system for writing; it does not guarantee that they are actually written to a physical device such as
149: * a disk drive.
150: *
151: * @throws java.io.IOException If an I/O error occurs
152: */
153: public void flush() throws IOException {
154: if ((buffer.length - cursor) > 50000) {
155: Log.debug("WASTED: " + (buffer.length - cursor));
156: }
157: }
158:
159: /**
160: * Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will
161: * cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.
162: *
163: * @throws java.io.IOException If an I/O error occurs
164: */
165: public void close() throws IOException {
166: }
167:
168: public synchronized byte[] toByteArray() {
169: final byte[] retval = new byte[cursor];
170: System.arraycopy(buffer, 0, retval, 0, cursor);
171: return retval;
172: }
173:
174: public int getLength() {
175: return cursor;
176: }
177:
178: public byte[] getRaw() {
179: if ((buffer.length - cursor) > 50000) {
180: Log.debug("WASTED: " + (buffer.length - cursor)
181: + " Length: " + buffer.length);
182: }
183: return buffer;
184: }
185: }
|