KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > io > FastCharArrayWriter


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.io;
4
5 import java.io.IOException JavaDoc;
6 import java.io.Writer JavaDoc;
7 import java.util.List JavaDoc;
8
9 /**
10  * Similar as {@link jodd.io.FastByteArrayOutputStream} but for {@link Writer}.
11  */

12 public class FastCharArrayWriter extends Writer JavaDoc {
13
14     private List JavaDoc buffers = new java.util.ArrayList JavaDoc();
15     private int currentBufferIndex;
16     private int filledBufferSum;
17     private char[] currentBuffer;
18     private int count;
19
20     /**
21      * Creates a new writer. The buffer capacity is
22      * initially 1024 bytes, though its size increases if necessary.
23      */

24     public FastCharArrayWriter() {
25         this(1024);
26     }
27
28     /**
29      * Creates a new char array writer, with a buffer capacity of
30      * the specified size, in bytes.
31      *
32      * @param size the initial size.
33      * @throws IllegalArgumentException if size is negative.
34      */

35     public FastCharArrayWriter(int size) {
36         if (size < 0) {
37             throw new IllegalArgumentException JavaDoc("Negative initial size: " + size);
38         }
39         needNewBuffer(size);
40     }
41
42     private char[] getBuffer(int index) {
43         return (char[]) buffers.get(index);
44     }
45
46     private void needNewBuffer(int newcount) {
47         if (currentBufferIndex < buffers.size() - 1) {
48             //Recycling old buffer
49
filledBufferSum += currentBuffer.length;
50
51             currentBufferIndex++;
52             currentBuffer = getBuffer(currentBufferIndex);
53         } else {
54             //Creating new buffer
55
int newBufferSize;
56             if (currentBuffer == null) {
57                 newBufferSize = newcount;
58                 filledBufferSum = 0;
59             } else {
60                 newBufferSize = Math.max(
61                         currentBuffer.length << 1,
62                         newcount - filledBufferSum);
63                 filledBufferSum += currentBuffer.length;
64             }
65
66             currentBufferIndex++;
67             currentBuffer = new char[newBufferSize];
68             buffers.add(currentBuffer);
69         }
70     }
71
72     /**
73      * @see java.io.Writer#write(char[], int, int)
74      */

75     public synchronized void write(char[] b, int off, int len) {
76         if ((off < 0)
77                 || (off > b.length)
78                 || (len < 0)
79                 || ((off + len) > b.length)
80                 || ((off + len) < 0)) {
81             throw new IndexOutOfBoundsException JavaDoc();
82         } else if (len == 0) {
83             return;
84         }
85         int newcount = count + len;
86         int remaining = len;
87         int inBufferPos = count - filledBufferSum;
88         while (remaining > 0) {
89             int part = Math.min(remaining, currentBuffer.length - inBufferPos);
90             System.arraycopy(b, off + len - remaining, currentBuffer, inBufferPos, part);
91             remaining -= part;
92             if (remaining > 0) {
93                 needNewBuffer(newcount);
94                 inBufferPos = 0;
95             }
96         }
97         count = newcount;
98     }
99
100     /**
101      * Calls the write(char[]) method.
102      *
103      * @see java.io.Writer#write(int)
104      */

105     public synchronized void write(int b) {
106         write(new char[]{(char) b}, 0, 1);
107     }
108
109     public synchronized void write(String JavaDoc s, int off, int len) {
110         write(s.toCharArray(), off, len);
111     }
112
113     /**
114      * @see java.io.CharArrayWriter#size()
115      */

116     public int size() {
117         return count;
118     }
119
120     /**
121      * Closing a <code>FastCharArrayWriter</code> has no effect. The methods in
122      * this class can be called after the stream has been closed without
123      * generating an <code>IOException</code>.
124      */

125     public void close() {
126         //nop
127
}
128
129     /**
130      * Flushing a <code>FastCharArrayWriter</code> has no effects.
131      */

132     public void flush() {
133         //nop
134
}
135
136     /**
137      * @see java.io.CharArrayWriter#reset()
138      */

139     public synchronized void reset() {
140         count = 0;
141         filledBufferSum = 0;
142         currentBufferIndex = 0;
143         currentBuffer = getBuffer(currentBufferIndex);
144     }
145
146     /**
147      * @see java.io.CharArrayWriter#writeTo(java.io.Writer)
148      */

149     public synchronized void writeTo(Writer JavaDoc out) throws IOException JavaDoc {
150         int remaining = count;
151         for (int i = 0; i < buffers.size(); i++) {
152             char[] buf = getBuffer(i);
153             int c = Math.min(buf.length, remaining);
154             out.write(buf, 0, c);
155             remaining -= c;
156             if (remaining == 0) {
157                 break;
158             }
159         }
160     }
161
162     /**
163      * @see java.io.CharArrayWriter#toCharArray()
164      */

165     public synchronized char[] toCharArray() {
166         int remaining = count;
167         int pos = 0;
168         char newbuf[] = new char[count];
169         for (int i = 0; i < buffers.size(); i++) {
170             char[] buf = getBuffer(i);
171             int c = Math.min(buf.length, remaining);
172             System.arraycopy(buf, 0, newbuf, pos, c);
173             pos += c;
174             remaining -= c;
175             if (remaining == 0) {
176                 break;
177             }
178         }
179         return newbuf;
180     }
181
182     /**
183      * @see java.io.CharArrayWriter#toString()
184      */

185     public String JavaDoc toString() {
186         return new String JavaDoc(toCharArray());
187     }
188 }
189
Popular Tags