KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > io > SocketOutputStream


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

16 package org.apache.commons.net.io;
17
18 import java.io.FilterOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.net.Socket JavaDoc;
22
23 /***
24  * This class wraps an output stream, storing a reference to its originating
25  * socket. When the stream is closed, it will also close the socket
26  * immediately afterward. This class is useful for situations where you
27  * are dealing with a stream originating from a socket, but do not have
28  * a reference to the socket, and want to make sure it closes when the
29  * stream closes.
30  * <p>
31  * <p>
32  * @author Daniel F. Savarese
33  * @see SocketInputStream
34  ***/

35
36 public class SocketOutputStream extends FilterOutputStream JavaDoc
37 {
38     private Socket JavaDoc __socket;
39
40     /***
41      * Creates a SocketOutputStream instance wrapping an output stream and
42      * storing a reference to a socket that should be closed on closing
43      * the stream.
44      * <p>
45      * @param socket The socket to close on closing the stream.
46      * @param stream The input stream to wrap.
47      ***/

48     public SocketOutputStream(Socket JavaDoc socket, OutputStream JavaDoc stream)
49     {
50         super(stream);
51         __socket = socket;
52     }
53
54
55     /***
56      * Writes a number of bytes from a byte array to the stream starting from
57      * a given offset. This method bypasses the equivalent method in
58      * FilterOutputStream because the FilterOutputStream implementation is
59      * very inefficient.
60      * <p>
61      * @param buffer The byte array to write.
62      * @param offset The offset into the array at which to start copying data.
63      * @param length The number of bytes to write.
64      * @exception IOException If an error occurs while writing to the underlying
65      * stream.
66      ***/

67     public void write(byte buffer[], int offset, int length) throws IOException JavaDoc
68     {
69         out.write(buffer, offset, length);
70     }
71
72
73     /***
74      * Closes the stream and immediately afterward closes the referenced
75      * socket.
76      * <p>
77      * @exception IOException If there is an error in closing the stream
78      * or socket.
79      ***/

80     public void close() throws IOException JavaDoc
81     {
82         super.close();
83         __socket.close();
84     }
85 }
86
Popular Tags