1 34 35 36 package com.micronova.util.servlet; 37 38 import javax.servlet.*; 39 import javax.servlet.http.*; 40 import java.io.*; 41 import java.util.*; 42 43 44 45 class BufferedHttpServletResponse extends HttpServletResponseWrapper 46 { 47 ByteArrayOutputStream _bOut; 48 ServletOutputStream _out; 49 StringWriter _writer; 50 boolean _isWriterUsed; 51 boolean _isOutputStreamUsed; 52 int _status; 53 54 public BufferedHttpServletResponse(HttpServletResponse response) 55 { 56 super(response); 57 58 _isWriterUsed = false; 59 _isOutputStreamUsed = false; 60 } 61 62 public ServletOutputStream getOutputStream() throws IOException 63 { 64 _isOutputStreamUsed = true; 65 _bOut = new ByteArrayOutputStream(); 66 _out = new ServletOutputStreamWrapper(_bOut); 67 68 return _out; 69 } 70 71 public PrintWriter getWriter() throws IOException 72 { 73 _isWriterUsed = true; 74 _writer = new StringWriter(); 75 76 return new PrintWriter(_writer); 77 } 78 79 public int getStatus() 80 { 81 return _status; 82 } 83 84 public void setStatus(int status) 85 { 86 _status = status; 87 } 88 89 90 91 public String getString(String encoding) throws Exception 92 { 93 if (_isWriterUsed) 94 { 95 return _writer.toString(); 96 } 97 else if (_isOutputStreamUsed) 98 { 99 return new String (_bOut.toByteArray(), encoding); 100 } 101 else 102 { 103 return null; 104 } 105 } 106 107 108 109 public String getString() throws Exception 110 { 111 return getString(getCharacterEncoding()); 112 } 113 } 114 | Popular Tags |