1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.tcp.protocols; |
8 | |
|
9 | |
import java.io.DataInputStream; |
10 | |
import java.io.DataOutputStream; |
11 | |
import java.io.IOException; |
12 | |
import java.io.InputStream; |
13 | |
import java.io.OutputStream; |
14 | |
|
15 | |
import org.apache.commons.logging.Log; |
16 | |
import org.apache.commons.logging.LogFactory; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class LengthProtocol extends DirectProtocol |
28 | |
{ |
29 | |
|
30 | 0 | private static final Log logger = LogFactory.getLog(LengthProtocol.class); |
31 | |
|
32 | |
private static final int SIZE_INT = 4; |
33 | |
public static final int NO_MAX_LENGTH = -1; |
34 | |
private int maxMessageLength; |
35 | |
|
36 | |
public LengthProtocol() |
37 | |
{ |
38 | 0 | this(NO_MAX_LENGTH); |
39 | 0 | } |
40 | |
|
41 | |
public LengthProtocol(int maxMessageLength) |
42 | |
{ |
43 | 0 | super(NO_STREAM, SIZE_INT); |
44 | 0 | this.setMaxMessageLength(maxMessageLength); |
45 | 0 | } |
46 | |
|
47 | |
public Object read(InputStream is) throws IOException |
48 | |
{ |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | DataInputStream dis = new DataInputStream(is); |
54 | 0 | dis.mark(SIZE_INT); |
55 | |
|
56 | 0 | if (null == super.read(dis, SIZE_INT)) |
57 | |
{ |
58 | 0 | return null; |
59 | |
} |
60 | |
|
61 | |
|
62 | 0 | dis.reset(); |
63 | 0 | int length = dis.readInt(); |
64 | 0 | if (logger.isDebugEnabled()) |
65 | |
{ |
66 | 0 | logger.debug("length: " + length); |
67 | |
} |
68 | |
|
69 | 0 | if (length < 0 || (getMaxMessageLength() > 0 && length > getMaxMessageLength())) |
70 | |
{ |
71 | 0 | throw new IOException("Length " + length + " exceeds limit: " + getMaxMessageLength()); |
72 | |
} |
73 | |
|
74 | |
|
75 | 0 | byte[] buffer = new byte[length]; |
76 | 0 | dis.readFully(buffer); |
77 | 0 | if (logger.isDebugEnabled()) |
78 | |
{ |
79 | 0 | logger.debug("length read: " + buffer.length); |
80 | |
} |
81 | |
|
82 | 0 | return buffer; |
83 | |
} |
84 | |
|
85 | |
@Override |
86 | |
protected void writeByteArray(OutputStream os, byte[] data) throws IOException |
87 | |
{ |
88 | |
|
89 | 0 | DataOutputStream dos = new DataOutputStream(os); |
90 | 0 | dos.writeInt(data.length); |
91 | 0 | dos.write(data); |
92 | |
|
93 | |
|
94 | 0 | if (dos.size() != data.length + SIZE_INT) |
95 | |
{ |
96 | |
|
97 | 0 | dos.flush(); |
98 | |
} |
99 | 0 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
@Override |
109 | |
protected boolean isRepeat(int len, int available) |
110 | |
{ |
111 | 0 | return true; |
112 | |
} |
113 | |
|
114 | |
public int getMaxMessageLength() |
115 | |
{ |
116 | 0 | return maxMessageLength; |
117 | |
} |
118 | |
|
119 | |
public void setMaxMessageLength(int maxMessageLength) |
120 | |
{ |
121 | 0 | this.maxMessageLength = maxMessageLength; |
122 | 0 | } |
123 | |
|
124 | |
} |