1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.tcp.protocols; |
12 | |
|
13 | |
import java.io.IOException; |
14 | |
import java.io.InputStream; |
15 | |
import java.text.MessageFormat; |
16 | |
|
17 | |
import org.apache.commons.io.output.ByteArrayOutputStream; |
18 | |
import org.apache.commons.logging.Log; |
19 | |
import org.apache.commons.logging.LogFactory; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class DefaultProtocol extends ByteProtocol |
32 | |
{ |
33 | |
|
34 | 4 | private static final Log logger = LogFactory.getLog(DefaultProtocol.class); |
35 | |
private static final int DEFAULT_BUFFER_SIZE = 8192; |
36 | |
private static final int UNLIMITED = -1; |
37 | |
|
38 | |
private int bufferSize; |
39 | |
|
40 | |
public DefaultProtocol() |
41 | |
{ |
42 | 56 | this(STREAM_OK, DEFAULT_BUFFER_SIZE); |
43 | 56 | } |
44 | |
|
45 | |
public DefaultProtocol(boolean streamOk, int bufferSize) |
46 | |
{ |
47 | 74 | super(streamOk); |
48 | 74 | this.bufferSize = bufferSize; |
49 | 74 | } |
50 | |
|
51 | |
public Object read(InputStream is) throws IOException |
52 | |
{ |
53 | 5303 | return read(is, UNLIMITED); |
54 | |
} |
55 | |
|
56 | |
public Object read(InputStream is, int limit) throws IOException |
57 | |
{ |
58 | |
|
59 | 5360 | ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize); |
60 | |
|
61 | |
try |
62 | |
{ |
63 | 5360 | byte[] buffer = new byte[bufferSize]; |
64 | |
int len; |
65 | 5360 | int remain = remaining(limit, limit, 0); |
66 | |
boolean repeat; |
67 | |
do |
68 | |
{ |
69 | 7815 | len = copy(is, buffer, baos, remain); |
70 | 7815 | remain = remaining(limit, remain, len); |
71 | 7815 | repeat = EOF != len && remain > 0 && isRepeat(len, is.available()); |
72 | |
|
73 | 7815 | if (logger.isDebugEnabled()) |
74 | |
{ |
75 | 0 | logger.debug(MessageFormat.format( |
76 | |
"len/limit/repeat: {0}/{1}/{2}", |
77 | |
new Object[] {new Integer(len), new Integer(limit), Boolean.valueOf(repeat)})); |
78 | |
} |
79 | |
} |
80 | 7815 | while (repeat); |
81 | |
} |
82 | |
finally |
83 | |
{ |
84 | 5360 | baos.flush(); |
85 | 5360 | baos.close(); |
86 | 5360 | } |
87 | 5360 | return nullEmptyArray(baos.toByteArray()); |
88 | |
} |
89 | |
|
90 | |
private int remaining(int limit, int remain, int len) |
91 | |
{ |
92 | 13175 | if (UNLIMITED == limit) |
93 | |
{ |
94 | 13040 | return bufferSize; |
95 | |
} |
96 | 135 | else if (EOF != len) |
97 | |
{ |
98 | 116 | return remain - len; |
99 | |
} |
100 | |
else |
101 | |
{ |
102 | 19 | return remain; |
103 | |
} |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
protected boolean isRepeat(int len, int available) |
117 | |
{ |
118 | |
|
119 | |
|
120 | |
|
121 | 242 | return available > 0; |
122 | |
} |
123 | |
|
124 | |
} |