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 object models an output stream that sends data through a Connector instance.
26 * Everything is written to this output stream becomes a ZObject instance and gets sent through
27 * the Connector.
28 * @see java.io.ByteArrayOutputStream
29 * @author <a href="mailto:morgiaclaudio@yahoo.it">Claudio Morgia</a>
30 * @version 1.0
31 */
32 public class ZOutputStream extends ByteArrayOutputStream {
33 /**
34 * The connector responsible for real sending.
35 * @see java.net.Connector
36 */
37 protected Connector conn;
38
39 /**
40 * This constructor only stores the argument into an internal field.
41 */
42 public ZOutputStream(Connector conn) {
43 this.conn=conn;
44 }
45
46 /**
47 * This method writes the buffer to the underlying ByteArrayOutputStream and then flushes it
48 * using the connector through the {@link #flush flush} method.
49 */
50 public void write(byte[] b,int off, int len) {
51 super.write(b,off,len);
52 try {
53 flush();
54 } catch(IOException ioe) {}
55 }
56
57 /**
58 * This method really sends the stored data (if any) from the underlying ByteArrayOutputStream through
59 * the connector to the socket.
60 * After data sending, the underlying stream is reset to the empty state.
61 * @see java.net.Connector#send(byte[])
62 */
63 public void flush() throws IOException {
64 try {
65 if (count>0)
66 conn.send(toByteArray());
67 } catch(Exception e) {
68 throw new IOException(e.getMessage());
69 }
70 super.flush();
71 super.reset();
72 }
73 }