1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.tcp.protocols; |
12 | |
|
13 | |
import org.mule.transport.tcp.TcpProtocol; |
14 | |
import org.mule.util.ClassUtils; |
15 | |
|
16 | |
import java.io.IOException; |
17 | |
import java.io.OutputStream; |
18 | |
|
19 | |
public class ProtocolStream extends OutputStream |
20 | |
{ |
21 | |
|
22 | |
private boolean streamOk; |
23 | |
private TcpProtocol protocol; |
24 | |
private OutputStream os; |
25 | |
|
26 | |
public ProtocolStream(TcpProtocol protocol, boolean streamOk, OutputStream os) |
27 | 0 | { |
28 | 0 | this.protocol = protocol; |
29 | 0 | this.streamOk = streamOk; |
30 | 0 | this.os = os; |
31 | 0 | } |
32 | |
|
33 | |
private void assertStreamOk() |
34 | |
{ |
35 | 0 | if (!streamOk) |
36 | |
{ |
37 | 0 | throw new IllegalArgumentException("TCP protocol " + ClassUtils.getSimpleName(protocol.getClass()) + |
38 | |
" does not support streaming output"); |
39 | |
} |
40 | 0 | } |
41 | |
|
42 | |
public void write(byte b[]) throws IOException |
43 | |
{ |
44 | 0 | assertStreamOk(); |
45 | 0 | protocol.write(os, b); |
46 | 0 | } |
47 | |
|
48 | |
public void write(byte b[], int off, int len) throws IOException |
49 | |
{ |
50 | 0 | assertStreamOk(); |
51 | 0 | byte[] buffer = new byte[len]; |
52 | 0 | System.arraycopy(b, off, buffer, 0, len); |
53 | 0 | protocol.write(os, buffer); |
54 | 0 | } |
55 | |
|
56 | |
public void flush() throws IOException |
57 | |
{ |
58 | 0 | assertStreamOk(); |
59 | 0 | os.flush(); |
60 | 0 | } |
61 | |
|
62 | |
public void write(int b) throws IOException |
63 | |
{ |
64 | 0 | write(new byte[]{(byte) b}); |
65 | 0 | } |
66 | |
|
67 | |
public void close() throws IOException |
68 | |
{ |
69 | 0 | assertStreamOk(); |
70 | 0 | os.close(); |
71 | 0 | } |
72 | |
|
73 | |
} |