KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > tcp > TcpBufferedOutputStream


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.transport.tcp;
20
21 import java.io.EOFException JavaDoc;
22 import java.io.FilterOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 /**
27  * An optimized buffered outputstream for Tcp
28  *
29  * @version $Revision: 1.1.1.1 $
30  */

31
32 public class TcpBufferedOutputStream extends FilterOutputStream JavaDoc {
33     private final static int BUFFER_SIZE = 8192;
34     private byte[] buffer;
35     private int bufferlen;
36     private int count;
37     private boolean closed;
38
39     /**
40      * Constructor
41      *
42      * @param out
43      */

44     public TcpBufferedOutputStream(OutputStream JavaDoc out) {
45         this(out, BUFFER_SIZE);
46     }
47
48     /**
49      * Creates a new buffered output stream to write data to the specified underlying output stream with the specified
50      * buffer size.
51      *
52      * @param out the underlying output stream.
53      * @param size the buffer size.
54      * @throws IllegalArgumentException if size <= 0.
55      */

56     public TcpBufferedOutputStream(OutputStream JavaDoc out, int size) {
57         super(out);
58         if (size <= 0) {
59             throw new IllegalArgumentException JavaDoc("Buffer size <= 0");
60         }
61         buffer = new byte[size];
62         bufferlen=size;
63     }
64
65     /**
66      * write a byte on to the stream
67      *
68      * @param b - byte to write
69      * @throws IOException
70      */

71     public void write(int b) throws IOException JavaDoc {
72         if ((bufferlen-count) < 1) {
73             flush();
74         }
75         buffer[count++] = (byte) b;
76     }
77
78
79     /**
80      * write a byte array to the stream
81      *
82      * @param b the byte buffer
83      * @param off the offset into the buffer
84      * @param len the length of data to write
85      * @throws IOException
86      */

87     public void write(byte b[], int off, int len) throws IOException JavaDoc {
88         if ((bufferlen-count) < len) {
89             flush();
90         }
91         if (buffer.length >= len) {
92             System.arraycopy(b, off, buffer, count, len);
93             count += len;
94         }
95         else {
96             out.write(b, off, len);
97         }
98     }
99
100     /**
101      * flush the data to the output stream
102      * This doesn't call flush on the underlying outputstream, because
103      * Tcp is particularly efficent at doing this itself ....
104      *
105      * @throws IOException
106      */

107     public void flush() throws IOException JavaDoc {
108         if (count > 0 && out != null) {
109             out.write(buffer, 0, count);
110             count = 0;
111         }
112     }
113
114     /**
115      * close this stream
116      *
117      * @throws IOException
118      */

119     public void close() throws IOException JavaDoc {
120         super.close();
121         closed = true;
122     }
123
124
125     /**
126      * Checks that the stream has not been closed
127      *
128      * @throws IOException
129      */

130     private final void checkClosed() throws IOException JavaDoc {
131         if (closed) {
132             throw new EOFException JavaDoc("Cannot write to the stream any more it has already been closed");
133         }
134     }
135
136 }
137
Popular Tags