1 16 package org.mortbay.jetty.servlet; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.io.OutputStreamWriter ; 21 import java.io.PrintWriter ; 22 import java.io.UnsupportedEncodingException ; 23 24 import org.apache.commons.logging.Log; 25 import org.mortbay.log.LogFactory; 26 import org.mortbay.http.HttpOutputStream; 27 import org.mortbay.util.IO; 28 import org.mortbay.util.LogSupport; 29 30 31 32 39 class ServletWriter extends PrintWriter 40 { 41 private static Log log = LogFactory.getLog(ServletWriter.class); 42 43 String encoding=null; 44 OutputStream os=null; 45 boolean written=false; 46 47 48 ServletWriter(OutputStream os) 49 throws IOException 50 { 51 super((os instanceof HttpOutputStream) 52 ?((HttpOutputStream)os).getWriter(null) 53 :new OutputStreamWriter (os)); 54 this.os=os; 55 } 56 57 58 ServletWriter(OutputStream os, String encoding) 59 throws IOException 60 { 61 super((os instanceof HttpOutputStream) 62 ?((HttpOutputStream)os).getWriter(encoding) 63 :new OutputStreamWriter (os,encoding)); 64 this.os=os; 65 this.encoding=encoding; 66 } 67 68 69 public void disable() 70 { 71 out=IO.getNullWriter(); 72 } 73 74 75 public void reset() 76 { 77 try{ 78 out=IO.getNullWriter(); 79 super.flush(); 80 out=new OutputStreamWriter (os,encoding); 81 written=false; 82 } 83 catch(UnsupportedEncodingException e) 84 { 85 log.fatal(e); System.exit(1); 86 } 87 } 88 89 90 91 public boolean isWritten() 92 { 93 return written; 94 } 95 96 97 98 public void print(boolean p) {written=true;super.print(p);} 99 public void print(char p) {written=true;super.print(p);} 100 public void print(char[] p) {written=true;super.print(p);} 101 public void print(double p) {written=true;super.print(p);} 102 public void print(float p) {written=true;super.print(p);} 103 public void print(int p) {written=true;super.print(p);} 104 public void print(long p) {written=true;super.print(p);} 105 public void print(Object p) {written=true;super.print(p);} 106 public void print(String p) {written=true;super.print(p);} 107 public void println() {written=true;super.println();} 108 public void println(boolean p){written=true;super.println(p);} 109 public void println(char p) {written=true;super.println(p);} 110 public void println(char[] p) {written=true;super.println(p);} 111 public void println(double p) {written=true;super.println(p);} 112 public void println(float p) {written=true;super.println(p);} 113 public void println(int p) {written=true;super.println(p);} 114 public void println(long p) {written=true;super.println(p);} 115 public void println(Object p) {written=true;super.println(p);} 116 public void println(String p) {written=true;super.println(p);} 117 118 119 public void write(int c) 120 { 121 try 122 { 123 if (out==null) 124 throw new IOException ("closed"); 125 written=true; 126 out.write(c); 127 } 128 catch (IOException e){LogSupport.ignore(log,e);setError();} 129 } 130 131 public void write(char[] cbuf, int off, int len) 132 { 133 try 134 { 135 if (out==null) 136 throw new IOException ("closed"); 137 written=true; 138 out.write(cbuf,off,len); 139 } 140 catch (IOException e){LogSupport.ignore(log,e);setError();} 141 } 142 143 public void write(char[] cbuf) 144 { 145 try 146 { 147 if (out==null) 148 throw new IOException ("closed"); 149 written=true; 150 out.write(cbuf,0,cbuf.length); 151 } 152 catch (IOException e){LogSupport.ignore(log,e);setError();} 153 } 154 155 public void write(String s, int off, int len) 156 { 157 try 158 { 159 if (out==null) 160 throw new IOException ("closed"); 161 written=true; 162 out.write(s,off,len); 163 } 164 catch (IOException e){LogSupport.ignore(log,e);setError();} 165 } 166 167 public void write(String s) 168 { 169 try 170 { 171 if (out==null) 172 throw new IOException ("closed"); 173 written=true; 174 out.write(s,0,s.length()); 175 } 176 catch (IOException e){LogSupport.ignore(log,e);setError();} 177 } 178 } 179 | Popular Tags |