KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > servlet > BufferedHttpServletResponse


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

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 /** HttpServletResponse with buffered output */
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     /** returns buffered content as String using given encoding */
90
91     public String JavaDoc getString(String JavaDoc encoding) throws Exception JavaDoc
92     {
93         if (_isWriterUsed)
94         {
95             return _writer.toString();
96         }
97         else if (_isOutputStreamUsed)
98         {
99             return new String JavaDoc(_bOut.toByteArray(), encoding);
100         }
101         else
102         {
103             return null;
104         }
105     }
106
107     /** returns buffered content as String */
108
109     public String JavaDoc getString() throws Exception JavaDoc
110     {
111         return getString(getCharacterEncoding());
112     }
113 }
114
Popular Tags