1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.net.io;
17
18 import java.io.FilterInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.Socket;
22
23 /***
24 * This class wraps an input 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 SocketOutputStream
34 ***/
35
36 public class SocketInputStream extends FilterInputStream
37 {
38 private Socket __socket;
39
40 /***
41 * Creates a SocketInputStream instance wrapping an input 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 SocketInputStream(Socket socket, InputStream stream)
49 {
50 super(stream);
51 __socket = socket;
52 }
53
54 /***
55 * Closes the stream and immediately afterward closes the referenced
56 * socket.
57 * <p>
58 * @exception IOException If there is an error in closing the stream
59 * or socket.
60 ***/
61 public void close() throws IOException
62 {
63 super.close();
64 __socket.close();
65 }
66 }