KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > ResponseOutputStream


1 /*
2  * $Id: ResponseOutputStream.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.impl;
12
13 import org.apache.commons.io.output.ByteArrayOutputStream;
14
15 import java.io.BufferedOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.net.Socket JavaDoc;
19
20 /**
21  * <code>ResponseOutputStream</code> is an output stream associated with the
22  * currently received event. Note that if the stream is from a socket the socket is
23  * also available on this stream so that the socket state can be validated before
24  * writing.
25  *
26  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
27  * @version $Revision: 3798 $
28  */

29
30 public class ResponseOutputStream extends BufferedOutputStream JavaDoc
31 {
32     private static ByteArrayOutputStream defaultStream = new ByteArrayOutputStream();
33
34     private boolean used = false;
35     private boolean isDefault = false;
36     private Socket JavaDoc socket = null;
37
38     public ResponseOutputStream()
39     {
40         super(defaultStream);
41         isDefault = true;
42     }
43
44     public ResponseOutputStream(OutputStream stream)
45     {
46         super(stream);
47     }
48
49     public ResponseOutputStream(OutputStream stream, Socket JavaDoc socket)
50     {
51         super(stream);
52         this.socket = socket;
53     }
54
55     public void write(int b) throws IOException JavaDoc
56     {
57         super.write(b);
58         used = true;
59     }
60
61     public byte[] getBytes() throws IOException JavaDoc
62     {
63         if (isDefault)
64         {
65             flush();
66             return defaultStream.toByteArray();
67         }
68         return null;
69     }
70
71     public boolean isUsed()
72     {
73         return used;
74     }
75
76     public Socket JavaDoc getSocket()
77     {
78         return socket;
79     }
80 }
81
Popular Tags