1 5 6 package org.infohazard.maverick.util; 7 8 import java.io.PrintWriter ; 9 import java.io.Reader ; 10 import java.io.StringReader ; 11 import java.io.StringWriter ; 12 import java.io.UnsupportedEncodingException ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 17 19 public class PrintWriterBuffer extends PrintWriter implements Buffer 20 { 21 24 private static Log log = LogFactory.getLog(PrintWriterBuffer.class); 25 26 29 public PrintWriterBuffer() 30 { 31 super(new StringWriter ()); 32 } 33 34 38 public boolean prefersReader() 39 { 40 return false; 41 } 42 43 46 public Reader getAsReader() throws UnsupportedEncodingException 47 { 48 return new StringReader (this.getAsString()); 49 } 50 51 54 public String getAsString() throws UnsupportedEncodingException 55 { 56 if (this.size() == 0) 57 { 58 log.debug("size() was 0, returning empty string"); 59 return ""; 60 } 61 else 62 return this.out.toString(); 63 } 64 65 68 public int size() 69 { 70 return ((StringWriter )this.out).getBuffer().length(); 71 } 72 73 76 public void close() 77 { 78 } 80 } 81 | Popular Tags |