1 package jodd.servlet.filters; 2 3 import java.io.CharArrayWriter; 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 7 import javax.servlet.ServletOutputStream; 8 import javax.servlet.http.HttpServletResponse; 9 import javax.servlet.http.HttpServletResponseWrapper; 10 11 import jodd.util.CharUtil; 12 13 17 public class CharArrayResponseWrapper extends HttpServletResponseWrapper { 18 19 private CharArrayWriter writer; 20 21 33 public CharArrayResponseWrapper(HttpServletResponse response) { 34 super(response); 35 writer = new CharArrayWriter(); 36 } 37 38 44 public PrintWriter getWriter() { 45 return new PrintWriter(writer); 46 } 47 48 55 public String toString() { 56 return writer.toString(); 57 } 58 59 64 public char[] toCharArray() { 65 return writer.toCharArray(); 66 } 67 68 73 public byte[] toByteArray() { 74 return CharUtil.toByteArray(writer.toCharArray()); 75 } 76 77 78 83 public void setContentLength(int len) { 84 } 85 86 91 public int getSize() { 92 return writer.size(); 93 } 94 95 96 private String contentType = ""; 97 98 103 public void setContentType(String type) { 104 super.setContentType(type); 105 contentType = type; 106 } 107 108 113 public String getContentType() { 114 return contentType; 115 } 116 117 public void close() { 118 writer.close(); 119 } 120 121 127 public ServletOutputStream getOutputStream() throws java.io.IOException { 128 129 return new ServletOutputStream() { 130 public void write(int b) throws IOException { 131 writer.write(b); 132 } 133 134 public void write(byte b[]) throws IOException { 135 writer.write(CharUtil.toCharArray(b), 0, b.length); 136 } 137 138 public void write(byte b[], int off, int len) throws IOException { 139 writer.write(CharUtil.toCharArray(b), off, len); 140 } 141 }; 142 } 143 144 } 145 | Popular Tags |