1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.tcp.protocols; |
8 | |
|
9 | |
import org.mule.transport.tcp.TcpInputStream; |
10 | |
import org.mule.transport.tcp.TcpProtocol; |
11 | |
|
12 | |
import java.io.IOException; |
13 | |
import java.io.InputStream; |
14 | |
import java.io.OutputStream; |
15 | |
|
16 | |
public class StreamingProtocol extends EOFProtocol implements TcpProtocol |
17 | |
{ |
18 | |
|
19 | |
public StreamingProtocol() |
20 | |
{ |
21 | 0 | super(); |
22 | 0 | } |
23 | |
|
24 | |
public Object read(InputStream is) throws IOException |
25 | |
{ |
26 | 0 | if (is instanceof TcpInputStream) |
27 | |
{ |
28 | 0 | ((TcpInputStream) is).setStreaming(true); |
29 | |
} |
30 | |
|
31 | 0 | return is; |
32 | |
} |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
protected void copyStream(InputStream is, OutputStream os) throws IOException |
41 | |
{ |
42 | |
try |
43 | |
{ |
44 | 0 | int limit = getLimit(); |
45 | 0 | byte[] buffer = new byte[bufferSize]; |
46 | |
int len; |
47 | 0 | int remain = remaining(limit, limit, 0); |
48 | 0 | int total = 0; |
49 | |
boolean repeat; |
50 | |
do |
51 | |
{ |
52 | 0 | len = copy(is, buffer, os, remain); |
53 | 0 | total += len; |
54 | 0 | remain = remaining(limit, remain, len); |
55 | 0 | repeat = EOF != len && remain > 0 && isRepeat(len, is.available()); |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 0 | if (len > 0 && len < buffer.length) |
61 | |
{ |
62 | 0 | os.flush(); |
63 | |
} |
64 | |
} |
65 | 0 | while (repeat); |
66 | |
} |
67 | |
finally |
68 | |
{ |
69 | 0 | is.close(); |
70 | 0 | } |
71 | 0 | } |
72 | |
|
73 | |
protected int getLimit() |
74 | |
{ |
75 | 0 | return UNLIMITED; |
76 | |
} |
77 | |
|
78 | |
} |
79 | |
|
80 | |
|