Debian Codegraph @

source/org/jfree/report/util/MemoryStringWriter.java

raw
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 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.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(char cbuf[], int off, int len) throws IOException
76 {
77 if (len < 0) throw new IllegalArgumentException();
78 if (off < 0)
79 {
80 throw new IndexOutOfBoundsException();
81 }
82 if (cbuf == null)
83 {
84 throw new NullPointerException();
85 }
86 if ((len + off) > cbuf.length)
87 {
88 throw new IndexOutOfBoundsException();
89 }
90
91 ensureSize (cursor + len);
92
93 System.arraycopy(cbuf, off, this.buffer, cursor, len);
94 cursor += len;
95 }
96
97 private void ensureSize(final int size)
98 {
99 if (this.buffer.length >= size)
100 {
101 return;
102 }
103
104 final int newSize = Math.max (size, this.buffer.length + bufferIncrement);
105 final char[] newBuffer = new char[newSize];
106 System.arraycopy(this.buffer, 0, newBuffer, 0, cursor);
107 }
108
109 /**
110 * Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them
111 * immediately to their intended destination. Then, if that destination is another character or byte stream, flush
112 * it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.
113 * <p/>
114 * If the intended destination of this stream is an abstraction provided by the underlying operating system, for
115 * example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to
116 * the operating system for writing; it does not guarantee that they are actually written to a physical device such as
117 * a disk drive.
118 *
119 * @throws java.io.IOException If an I/O error occurs
120 */
121 public void flush() throws IOException
122 {
123
124 }
125
126 /**
127 * Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will
128 * cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.
129 *
130 * @throws java.io.IOException If an I/O error occurs
131 */
132 public void close() throws IOException
133 {
134 }
135
136 public String toString ()
137 {
138 return new String (buffer, 0, cursor);
139 }
140 }