1 14 15 package com.sun.facelets; 16 17 import java.io.IOException ; 18 import java.io.Writer ; 19 20 import com.sun.facelets.util.FastWriter; 21 22 50 final class StateWriter extends Writer { 51 52 private int initialSize; 53 private Writer out; 54 private FastWriter fast; 55 private boolean writtenState; 56 57 static public StateWriter getCurrentInstance() { 58 return (StateWriter) CURRENT_WRITER.get(); 59 } 60 61 public StateWriter(Writer initialOut, int initialSize) { 62 if (initialSize < 0) { 63 throw new IllegalArgumentException ("Initial Size cannot be less than 0"); 64 } 65 66 this.initialSize = initialSize; 67 this.out = initialOut; 68 69 CURRENT_WRITER.set(this); 70 } 71 72 80 public void writingState() { 81 if (!this.writtenState) { 82 this.writtenState = true; 83 this.out = this.fast = new FastWriter(this.initialSize); 84 } 85 } 86 87 public boolean isStateWritten() { 88 return this.writtenState; 89 } 90 91 public void close() throws IOException { 92 } 94 95 public void flush() throws IOException { 96 } 98 99 public void write(char[] cbuf, int off, int len) throws IOException { 100 this.out.write(cbuf, off, len); 101 } 102 103 public void write(char[] cbuf) throws IOException { 104 this.out.write(cbuf); 105 } 106 107 public void write(int c) throws IOException { 108 this.out.write(c); 109 } 110 111 public void write(String str, int off, int len) throws IOException { 112 this.out.write(str, off, len); 113 } 114 115 public void write(String str) throws IOException { 116 this.out.write(str); 117 } 118 119 public String getAndResetBuffer() { 120 if (!this.writtenState) { 121 throw new IllegalStateException ( 122 "Did not write state; no buffer is available"); 123 } 124 125 String result = this.fast.toString(); 126 this.fast.reset(); 127 return result; 128 } 129 130 public void release() { 131 CURRENT_WRITER.set(null); 132 } 133 134 static private final ThreadLocal CURRENT_WRITER = new ThreadLocal (); 135 } | Popular Tags |