1 3 package jodd.io; 4 5 import java.io.IOException ; 6 import java.io.Writer ; 7 import java.util.List ; 8 9 12 public class FastCharArrayWriter extends Writer { 13 14 private List buffers = new java.util.ArrayList (); 15 private int currentBufferIndex; 16 private int filledBufferSum; 17 private char[] currentBuffer; 18 private int count; 19 20 24 public FastCharArrayWriter() { 25 this(1024); 26 } 27 28 35 public FastCharArrayWriter(int size) { 36 if (size < 0) { 37 throw new IllegalArgumentException ("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 filledBufferSum += currentBuffer.length; 50 51 currentBufferIndex++; 52 currentBuffer = getBuffer(currentBufferIndex); 53 } else { 54 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 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 (); 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 105 public synchronized void write(int b) { 106 write(new char[]{(char) b}, 0, 1); 107 } 108 109 public synchronized void write(String s, int off, int len) { 110 write(s.toCharArray(), off, len); 111 } 112 113 116 public int size() { 117 return count; 118 } 119 120 125 public void close() { 126 } 128 129 132 public void flush() { 133 } 135 136 139 public synchronized void reset() { 140 count = 0; 141 filledBufferSum = 0; 142 currentBufferIndex = 0; 143 currentBuffer = getBuffer(currentBufferIndex); 144 } 145 146 149 public synchronized void writeTo(Writer out) throws IOException { 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 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 185 public String toString() { 186 return new String (toCharArray()); 187 } 188 } 189 | Popular Tags |