1 package org.apache.velocity.io; 2 3 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 22 31 public final class VelocityWriter extends Writer 32 { 33 36 public static final int NO_BUFFER = 0; 37 38 42 public static final int DEFAULT_BUFFER = -1; 43 44 48 public static final int UNBOUNDED_BUFFER = -2; 49 50 protected int bufferSize; 51 protected boolean autoFlush; 52 53 private Writer writer; 54 55 private char cb[]; 56 private int nextChar; 57 58 private static int defaultCharBufferSize = 8 * 1024; 59 60 private boolean flushed = false; 61 62 68 public VelocityWriter(Writer writer) 69 { 70 this(writer, defaultCharBufferSize, true); 71 } 72 73 76 private VelocityWriter(int bufferSize, boolean autoFlush) 77 { 78 this.bufferSize = bufferSize; 79 this.autoFlush = autoFlush; 80 } 81 82 87 public int getBufferSize() { return bufferSize; } 88 89 95 public boolean isAutoFlush() { return autoFlush; } 96 97 106 public VelocityWriter(Writer writer, int sz, boolean autoFlush) 107 { 108 this(sz, autoFlush); 109 if (sz < 0) 110 throw new IllegalArgumentException ("Buffer size <= 0"); 111 this.writer = writer; 112 cb = sz == 0 ? null : new char[sz]; 113 nextChar = 0; 114 } 115 116 private final void init( Writer writer, int sz, boolean autoFlush ) 117 { 118 this.writer= writer; 119 if( sz > 0 && ( cb == null || sz > cb.length ) ) 120 cb=new char[sz]; 121 nextChar = 0; 122 this.autoFlush=autoFlush; 123 this.bufferSize=sz; 124 } 125 126 131 private final void flushBuffer() throws IOException 132 { 133 if (bufferSize == 0) 134 return; 135 flushed = true; 136 if (nextChar == 0) 137 return; 138 writer.write(cb, 0, nextChar); 139 nextChar = 0; 140 } 141 142 145 public final void clear() 146 { 147 nextChar = 0; 148 } 149 150 private final void bufferOverflow() throws IOException 151 { 152 throw new IOException ("overflow"); 153 } 154 155 159 public final void flush() throws IOException 160 { 161 flushBuffer(); 162 if (writer != null) 163 { 164 writer.flush(); 165 } 166 } 167 168 172 public final void close() throws IOException { 173 if (writer == null) 174 return; 175 flush(); 176 } 177 178 181 public final int getRemaining() 182 { 183 return bufferSize - nextChar; 184 } 185 186 190 public final void write(int c) throws IOException 191 { 192 if (bufferSize == 0) 193 { 194 writer.write(c); 195 } 196 else 197 { 198 if (nextChar >= bufferSize) 199 if (autoFlush) 200 flushBuffer(); 201 else 202 bufferOverflow(); 203 cb[nextChar++] = (char) c; 204 } 205 } 206 207 212 private final int min(int a, int b) 213 { 214 return (a < b ? a : b); 215 } 216 217 232 public final void write(char cbuf[], int off, int len) 233 throws IOException 234 { 235 if (bufferSize == 0) 236 { 237 writer.write(cbuf, off, len); 238 return; 239 } 240 241 if (len == 0) 242 { 243 return; 244 } 245 246 if (len >= bufferSize) 247 { 248 251 if (autoFlush) 252 flushBuffer(); 253 else 254 bufferOverflow(); 255 writer.write(cbuf, off, len); 256 return; 257 } 258 259 int b = off, t = off + len; 260 while (b < t) 261 { 262 int d = min(bufferSize - nextChar, t - b); 263 System.arraycopy(cbuf, b, cb, nextChar, d); 264 b += d; 265 nextChar += d; 266 if (nextChar >= bufferSize) 267 if (autoFlush) 268 flushBuffer(); 269 else 270 bufferOverflow(); 271 } 272 } 273 274 278 public final void write(char buf[]) throws IOException 279 { 280 write(buf, 0, buf.length); 281 } 282 283 291 public final void write(String s, int off, int len) throws IOException 292 { 293 if (bufferSize == 0) 294 { 295 writer.write(s, off, len); 296 return; 297 } 298 int b = off, t = off + len; 299 while (b < t) 300 { 301 int d = min(bufferSize - nextChar, t - b); 302 s.getChars(b, b + d, cb, nextChar); 303 b += d; 304 nextChar += d; 305 if (nextChar >= bufferSize) 306 if (autoFlush) 307 flushBuffer(); 308 else 309 bufferOverflow(); 310 } 311 } 312 313 317 public final void write(String s) throws IOException 318 { 319 write(s, 0, s.length()); 320 } 321 322 326 public final void recycle( Writer writer) 327 { 328 this.writer = writer; 329 flushed = false; 330 clear(); 331 } 332 } 333 | Popular Tags |