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