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 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 this(NO_MAX_LENGTH);
39 }
40
41 public LengthProtocol(int maxMessageLength)
42 {
43 super(NO_STREAM, SIZE_INT);
44 this.setMaxMessageLength(maxMessageLength);
45 }
46
47 public Object read(InputStream is) throws IOException
48 {
49
50
51
52
53 DataInputStream dis = new DataInputStream(is);
54 dis.mark(SIZE_INT);
55
56 if (null == super.read(dis, SIZE_INT))
57 {
58 return null;
59 }
60
61
62 dis.reset();
63 int length = dis.readInt();
64 if (logger.isDebugEnabled())
65 {
66 logger.debug("length: " + length);
67 }
68
69 if (length < 0 || (getMaxMessageLength() > 0 && length > getMaxMessageLength()))
70 {
71 throw new IOException("Length " + length + " exceeds limit: " + getMaxMessageLength());
72 }
73
74
75 byte[] buffer = new byte[length];
76 dis.readFully(buffer);
77 if (logger.isDebugEnabled())
78 {
79 logger.debug("length read: " + buffer.length);
80 }
81
82 return buffer;
83 }
84
85 @Override
86 protected void writeByteArray(OutputStream os, byte[] data) throws IOException
87 {
88
89 DataOutputStream dos = new DataOutputStream(os);
90 dos.writeInt(data.length);
91 dos.write(data);
92
93
94 if (dos.size() != data.length + SIZE_INT)
95 {
96
97 dos.flush();
98 }
99 }
100
101
102
103
104
105
106
107
108 @Override
109 protected boolean isRepeat(int len, int available)
110 {
111 return true;
112 }
113
114 public int getMaxMessageLength()
115 {
116 return maxMessageLength;
117 }
118
119 public void setMaxMessageLength(int maxMessageLength)
120 {
121 this.maxMessageLength = maxMessageLength;
122 }
123
124 }