1 /* ZNet - Java Compression Layer for a new Socket Factory
2 Copyright (C) 1999, Free Software Rulez
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 The author of this program may be contacted at morgiaclaudio@yahoo.it. */
19
20 package java.net;
21
22 import java.io;
23
24 /**
25 * This class is the main entry point to the compression layer.
26 * It provides method to deal with compressed objects through the incapsulation into streams.
27 * @see java.net.PlainSocketImpl the plain-old socket implementation
28 * @see java.net.Connector the real packet handler
29 * @author <a href="mailto:morgiaclaudio@yahoo.it">Claudio Morgia</a>
30 * @version 1.0
31 */
32 class ZSocketImpl extends PlainSocketImpl {
33 /**
34 * The Connector is responsible for packet handling.
35 * @see java.net.Connector
36 */
37 protected Connector conn;
38 protected InputStream input;
39 protected OutputStream output;
40
41 /**
42 * When this method is first invoked, a connector is created with a SocketInputStream and a
43 * SocketOutputStream bound to it.
44 * Then a new (compressed) OutputStream is created and returned.
45 * @return the output stream that can be used to write compressed data
46 */
47 protected synchronized OutputStream getOutputStream() throws IOException {
48 try {
49 if (conn==null) {
50 OutputStream out=new SocketOutputStream(this);
51 InputStream in=new SocketInputStream(this);
52 conn=new Connector(out,in);
53 }
54 output=conn.getOutputStream();
55 return output;
56 } catch(Exception e) {
57 throw new IOException(e.getMessage());
58 }
59 }
60
61 /**
62 * When this method is first invoked, a connector is created with a SocketInputStream and a
63 * SocketOutputStream bound to it.
64 * Then a new (compressed) InputStream is created and returned.
65 * @return the input stream that can be used to read compressed data
66 */
67 public InputStream getInputStream() throws IOException {
68 try {
69 if (conn==null) {
70 InputStream in=new SocketInputStream(this);
71 OutputStream out=new SocketOutputStream(this);
72 conn=new Connector(in,out);
73 }
74 input=conn.getInputStream();
75 return input;
76 } catch(Exception e) {
77 throw new IOException(e.getMessage());
78 }
79 }
80
81 /**
82 * This method stops the connector before object deallocation.
83 */
84 public void close() throws IOException {
85 if (conn!=null) {
86 conn.stop();
87 conn=null;
88 }
89 super.close();
90 }
91 }