34 * @author Jan Lahoda
35 */
36 class MultiplexingOutputStream extends OutputStream {
37
38 private static final int PACKET_SIZE = 127;
39 private final byte[] name;
40 private final OutputStream delegate;
41
42 MultiplexingOutputStream(String name, OutputStream delegate) {
43 try {
44 this.name = name.getBytes("UTF-8");
45 this.delegate = delegate;
46 } catch (UnsupportedEncodingException ex) {
47 throw new IllegalStateException(ex); //should not happen
48 }
49 }
50
51 @Override
52 public void write(int b) throws IOException {
53 synchronized (delegate) {
54 delegate.write(name.length); //assuming the len is small enough to fit into byte
55 delegate.write(name);
56 delegate.write(1);
57 delegate.write(b);
58 delegate.flush();
59 }
60 }
61
62 @Override
63 public void write(byte[] b, int off, int len) throws IOException {
64 synchronized (delegate) {
65 int i = 0;
66 while (len > 0) {
67 int size = Math.min(PACKET_SIZE, len);
68 delegate.write(name.length); //assuming the len is small enough to fit into byte
69 delegate.write(name);
70 delegate.write(size);
71 delegate.write(b, off + i, size);
72 i += size;
73 len -= size;
74 }
75 delegate.flush();
76 }
77 }
78
79 @Override
80 public void flush() throws IOException {
81 super.flush();
82 delegate.flush();
83 }
84
85 @Override
86 public void close() throws IOException {
87 super.close();
88 delegate.close();
89 }
90
91 }
|
34 * @author Jan Lahoda
35 */
36 class MultiplexingOutputStream extends OutputStream {
37
38 private static final int PACKET_SIZE = 127;
39 private final byte[] name;
40 private final OutputStream delegate;
41
42 MultiplexingOutputStream(String name, OutputStream delegate) {
43 try {
44 this.name = name.getBytes("UTF-8");
45 this.delegate = delegate;
46 } catch (UnsupportedEncodingException ex) {
47 throw new IllegalStateException(ex); //should not happen
48 }
49 }
50
51 @Override
52 public void write(int b) throws IOException {
53 synchronized (delegate) {
54 byte[] data = new byte[name.length + 1 + 1 + 1];
55 data[0] = (byte) name.length; //assuming the len is small enough to fit into byte
56 System.arraycopy(name, 0, data, 1, name.length);
57 data[name.length + 1] = 1;
58 data[name.length + 2] = (byte) b;
59 delegate.write(data);
60 delegate.flush();
61 }
62 }
63
64 @Override
65 public void write(byte[] b, int off, int len) throws IOException {
66 synchronized (delegate) {
67 int i = 0;
68 while (len > 0) {
69 int size = Math.min(PACKET_SIZE, len);
70 byte[] data = new byte[name.length + 1 + size + 1];
71 data[0] = (byte) name.length; //assuming the len is small enough to fit into byte
72 System.arraycopy(name, 0, data, 1, name.length);
73 data[name.length + 1] = (byte) size;
74 System.arraycopy(b, off + i, data, name.length + 2, size);
75 delegate.write(data);
76 i += size;
77 len -= size;
78 }
79 delegate.flush();
80 }
81 }
82
83 @Override
84 public void flush() throws IOException {
85 super.flush();
86 delegate.flush();
87 }
88
89 @Override
90 public void close() throws IOException {
91 super.close();
92 delegate.close();
93 }
94
95 }
|