Source code: org/scoja/util/UNIXSocketOutputStream.java
1
2 package org.scoja.util;
3
4 import java.io.OutputStream;
5 import java.io.IOException;
6
7
8 /**
9 * A UNIXSocketOutputStream is a stream to write to a <code>UNIXSocket</code>
10 *
11 * <p><code>UNIXSocketOutputStream</code> is meant for writing streams of raw bytes
12 *
13 * @see java.io.OutputStream
14 * @see org.scoja.UNIXSocketInputStream
15 */
16 public final class UNIXSocketOutputStream extends OutputStream {
17
18 private UNIXSocket socket;
19
20 public UNIXSocketOutputStream(UNIXSocket us) {
21 this.socket = us;
22 }
23
24
25
26 /**
27 * Writes the specified byte to this UNIXSocket output stream. Implements
28 * the <code>write</code> method of <code>OutputStream</code>.
29 *
30 * @param b the byte to be written.
31 * @exception IOException if an I/O error occurs.
32 */
33 public void write(int b) throws IOException {
34 // im not sure what to do here.
35
36 byte bb = (byte) b;
37 byte[] arr = {bb};
38
39 socket.send(arr);
40 }
41
42
43 /**
44 * Writes <code>b.length</code> bytes from the specified byte array
45 * to this UNIXSocket output stream.
46 *
47 * @param b the data.
48 * @exception IOException if an I/O error occurs.
49 */
50 public void write(byte[] b) throws IOException {
51 socket.send(b);
52 }
53
54 /**
55 * Writes <code>len</code> bytes from the specified byte array
56 * starting at offset <code>off</code> to this UNIXSocket output stream.
57 *
58 * @param b the data.
59 * @param off the start offset in the data.
60 * @param len the number of bytes to write.
61 * @exception IOException if an I/O error occurs.
62 */
63 public void write(byte b[], int off, int len) throws IOException {
64
65 if (len <= 0 || off < 0 || off+len > b.length) {
66
67 if (len == 0) return;
68
69 throw new ArrayIndexOutOfBoundsException();
70 }
71
72 byte[] msg = new byte[len];
73 for (int k=off; k<len; k++)
74 msg[k] = b[k];
75
76 socket.send(msg);
77
78 }
79
80
81 /**
82 * Closes this UNIXSocket output stream and releases any system resources
83 * associated with this stream. This UNIXSocket output stream may no longer
84 * be used for writing bytes.
85 *
86 * <p> If this stream has an associated channel then the channel is closed
87 * as well.
88 *
89 * @exception IOException if an I/O error occurs.
90 *
91 * @revised 1.4
92 * @spec JSR-51
93 */
94 public void close() throws IOException {
95 this.flush();
96 this.socket.close();
97 }
98
99 }