| 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: MemoryByteArrayOutputStream.java 10756 2009-12-02 15:58:24Z 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.OutputStream; |
| 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 MemoryByteArrayOutputStream extends OutputStream |
| 45 |
{ |
| 46 |
private int initialBufferSize; |
| 47 |
private int maximumBufferIncrement; |
| 48 |
private int cursor; |
| 49 |
private byte[] buffer; |
| 50 |
private byte[] singleIntArray; |
| 51 |
|
| 52 |
/** |
| 53 |
* Create a new character-stream writer whose critical sections will synchronize on the writer itself. |
| 54 |
*/ |
| 55 |
public MemoryByteArrayOutputStream() |
| 56 |
{ |
| 57 |
this(4096, 65536); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Create a new character-stream writer whose critical sections will synchronize on the writer itself. |
| 62 |
*/ |
| 63 |
public MemoryByteArrayOutputStream(final int bufferSize, final int maximumBufferIncrement) |
| 64 |
{ |
| 65 |
this.initialBufferSize = bufferSize; |
| 66 |
this.maximumBufferIncrement = maximumBufferIncrement; |
| 67 |
this.buffer = new byte[bufferSize]; |
| 68 |
this.singleIntArray = new byte[1]; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Write a portion of an array of characters. |
| 74 |
* |
| 75 |
* @param cbuf Array of characters |
| 76 |
* @param off Offset from which to start writing characters |
| 77 |
* @param len Number of characters to write |
| 78 |
* @throws java.io.IOException If an I/O error occurs |
| 79 |
*/ |
| 80 |
public synchronized void write(final byte cbuf[], final int off, final int len) throws IOException |
| 81 |
{ |
| 82 |
if (len < 0) |
| 83 |
{ |
| 84 |
throw new IllegalArgumentException(); |
| 85 |
} |
| 86 |
if (off < 0) |
| 87 |
{ |
| 88 |
throw new IndexOutOfBoundsException(); |
| 89 |
} |
| 90 |
if (cbuf == null) |
| 91 |
{ |
| 92 |
throw new NullPointerException(); |
| 93 |
} |
| 94 |
if ((len + off) > cbuf.length) |
| 95 |
{ |
| 96 |
throw new IndexOutOfBoundsException(); |
| 97 |
} |
| 98 |
|
| 99 |
ensureSize (cursor + len); |
| 100 |
|
| 101 |
System.arraycopy(cbuf, off, this.buffer, cursor, len); |
| 102 |
cursor += len; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Writes <code>b.length</code> bytes from the specified byte array to this output stream. The general contract for |
| 107 |
* <code>write(b)</code> is that it should have exactly the same effect as the call <code>write(b, 0, |
| 108 |
* b.length)</code>. |
| 109 |
* |
| 110 |
* @param b the data. |
| 111 |
* @throws java.io.IOException if an I/O error occurs. |
| 112 |
* @see java.io.OutputStream#write(byte[], int, int) |
| 113 |
*/ |
| 114 |
public void write(byte b[]) throws IOException |
| 115 |
{ |
| 116 |
write(b, 0, b.length); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Writes the specified byte to this output stream. The general contract for <code>write</code> is that one byte is |
| 121 |
* written to the output stream. The byte to be written is the eight low-order bits of the argument <code>b</code>. |
| 122 |
* The 24 high-order bits of <code>b</code> are ignored. |
| 123 |
* <p/> |
| 124 |
* Subclasses of <code>OutputStream</code> must provide an implementation for this method. |
| 125 |
* |
| 126 |
* @param b the <code>byte</code>. |
| 127 |
* @throws java.io.IOException if an I/O error occurs. In particular, an <code>IOException</code> may be thrown if the |
| 128 |
* output stream has been closed. |
| 129 |
*/ |
| 130 |
public synchronized void write(final int b) throws IOException |
| 131 |
{ |
| 132 |
this.singleIntArray[0] = (byte) (0xFF & b); |
| 133 |
write(singleIntArray, 0, 1); |
| 134 |
} |
| 135 |
|
| 136 |
private void ensureSize(final int size) |
| 137 |
{ |
| 138 |
if (this.buffer.length >= size) |
| 139 |
{ |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
final int computedSize = (int) Math.min ((this.buffer.length + 1) * 1.5, this.buffer.length + maximumBufferIncrement); |
| 144 |
final int newSize = Math.max (size, computedSize); |
| 145 |
final byte[] newBuffer = new byte[newSize]; |
| 146 |
System.arraycopy(this.buffer, 0, newBuffer, 0, cursor); |
| 147 |
this.buffer = newBuffer; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them |
| 152 |
* immediately to their intended destination. Then, if that destination is another character or byte stream, flush |
| 153 |
* it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams. |
| 154 |
* <p/> |
| 155 |
* If the intended destination of this stream is an abstraction provided by the underlying operating system, for |
| 156 |
* example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to |
| 157 |
* the operating system for writing; it does not guarantee that they are actually written to a physical device such as |
| 158 |
* a disk drive. |
| 159 |
* |
| 160 |
* @throws java.io.IOException If an I/O error occurs |
| 161 |
*/ |
| 162 |
public void flush() throws IOException |
| 163 |
{ |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will |
| 168 |
* cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect. |
| 169 |
* |
| 170 |
* @throws java.io.IOException If an I/O error occurs |
| 171 |
*/ |
| 172 |
public void close() throws IOException |
| 173 |
{ |
| 174 |
} |
| 175 |
|
| 176 |
public synchronized byte[] toByteArray() |
| 177 |
{ |
| 178 |
final byte[] retval = new byte[cursor]; |
| 179 |
System.arraycopy(buffer, 0, retval, 0, cursor); |
| 180 |
return retval; |
| 181 |
} |
| 182 |
|
| 183 |
public int getLength() |
| 184 |
{ |
| 185 |
return cursor; |
| 186 |
} |
| 187 |
|
| 188 |
public byte[] getRaw() |
| 189 |
{ |
| 190 |
return buffer; |
| 191 |
} |
| 192 |
} |