1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.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 DirectProtocol extends AbstractByteProtocol |
32 | |
{ |
33 | |
|
34 | |
protected static final int UNLIMITED = -1; |
35 | |
|
36 | 2 | private static final Log logger = LogFactory.getLog(DirectProtocol.class); |
37 | |
private static final int DEFAULT_BUFFER_SIZE = 8192; |
38 | |
|
39 | |
protected int bufferSize; |
40 | |
|
41 | |
public DirectProtocol() |
42 | |
{ |
43 | 44 | this(STREAM_OK, DEFAULT_BUFFER_SIZE); |
44 | 44 | } |
45 | |
|
46 | |
public DirectProtocol(boolean streamOk, int bufferSize) |
47 | |
{ |
48 | 388 | super(streamOk); |
49 | 388 | this.bufferSize = bufferSize; |
50 | 388 | } |
51 | |
|
52 | |
public Object read(InputStream is) throws IOException |
53 | |
{ |
54 | 2721 | return read(is, UNLIMITED); |
55 | |
} |
56 | |
|
57 | |
public Object read(InputStream is, int limit) throws IOException |
58 | |
{ |
59 | |
|
60 | 3224 | ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize); |
61 | |
|
62 | |
try |
63 | |
{ |
64 | 3224 | byte[] buffer = new byte[bufferSize]; |
65 | |
int len; |
66 | 3224 | int remain = remaining(limit, limit, 0); |
67 | |
boolean repeat; |
68 | |
do |
69 | |
{ |
70 | |
|
71 | 4589 | len = copy(is, buffer, baos, remain); |
72 | 4589 | remain = remaining(limit, remain, len); |
73 | 4589 | repeat = EOF != len && remain > 0 && isRepeat(len, is.available()); |
74 | |
|
75 | 4589 | if (logger.isDebugEnabled()) |
76 | |
{ |
77 | 0 | logger.debug(MessageFormat.format( |
78 | |
"len/limit/repeat: {0}/{1}/{2}", |
79 | |
new Object[] {new Integer(len), new Integer(limit), Boolean.valueOf(repeat)})); |
80 | |
} |
81 | |
} |
82 | 4589 | while (repeat); |
83 | |
} |
84 | |
finally |
85 | |
{ |
86 | 3224 | baos.flush(); |
87 | 3224 | baos.close(); |
88 | 3223 | } |
89 | 3223 | return nullEmptyArray(baos.toByteArray()); |
90 | |
} |
91 | |
|
92 | |
protected int remaining(int limit, int remain, int len) |
93 | |
{ |
94 | 7813 | if (UNLIMITED == limit) |
95 | |
{ |
96 | 6789 | return bufferSize; |
97 | |
} |
98 | 1024 | else if (EOF != len) |
99 | |
{ |
100 | 958 | return remain - len; |
101 | |
} |
102 | |
else |
103 | |
{ |
104 | 65 | return remain; |
105 | |
} |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
protected boolean isRepeat(int len, int available) |
119 | |
{ |
120 | |
|
121 | |
|
122 | |
|
123 | 38 | return available > 0; |
124 | |
} |
125 | |
|
126 | |
} |