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 super();
26 }
27
28 public Object read(InputStream is) throws IOException
29 {
30 if (is instanceof TcpInputStream)
31 {
32 ((TcpInputStream) is).setStreaming(true);
33 }
34
35 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 int limit = getLimit();
49 byte[] buffer = new byte[bufferSize];
50 int len;
51 int remain = remaining(limit, limit, 0);
52 int total = 0;
53 boolean repeat;
54 do
55 {
56 len = copy(is, buffer, os, remain);
57 total += len;
58 remain = remaining(limit, remain, len);
59 repeat = EOF != len && remain > 0 && isRepeat(len, is.available());
60
61
62
63
64 if (len > 0 && len < buffer.length)
65 {
66 os.flush();
67 }
68 }
69 while (repeat);
70 }
71 finally
72 {
73 is.close();
74 }
75 }
76
77 protected int getLimit()
78 {
79 return UNLIMITED;
80 }
81
82 }
83
84