comparison src/org/dancres/blitz/remote/transport/MessageDecoder.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.remote.transport;
2
3 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
4 import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
5 import org.apache.mina.common.IoSession;
6 import org.apache.mina.common.ByteBuffer;
7
8 /**
9 */
10 public class MessageDecoder extends CumulativeProtocolDecoder {
11 private int maxObjectSize = MessageCodecFactory.DEFAULT_MAX_OBJECT_SIZE;
12
13 /**
14 * Creates a new instance with the {@link ClassLoader} of
15 * the current thread.
16 */
17 public MessageDecoder() {
18 }
19
20 /**
21 * Returns the allowed maximum size of the object to be decoded.
22 * If the size of the object to be decoded exceeds this value, this
23 * decoder will throw a {@link org.apache.mina.common.BufferDataException}. The default
24 * value is <tt>1048576</tt> (1MB).
25 */
26 public int getMaxObjectSize() {
27 return maxObjectSize;
28 }
29
30 /**
31 * Sets the allowed maximum size of the object to be decoded.
32 * If the size of the object to be decoded exceeds this value, this
33 * decoder will throw a {@link org.apache.mina.common.BufferDataException}. The default
34 * value is <tt>1048576</tt> (1MB).
35 */
36 public void setMaxObjectSize(int maxObjectSize) {
37 if (maxObjectSize <= 0) {
38 throw new IllegalArgumentException(
39 "maxObjectSize: " + maxObjectSize);
40 }
41
42 this.maxObjectSize = maxObjectSize;
43 }
44
45 protected boolean doDecode(IoSession session, ByteBuffer in,
46 ProtocolDecoderOutput out) throws Exception {
47 if (!in.prefixedDataAvailable(4, maxObjectSize)) {
48 return false;
49 }
50
51 /*
52 Read length field
53 Read conversation id
54 Extract payload array
55 Construct Message object and post to "out"
56 */
57 int myLength = in.getInt();
58 int myConversationId = in.getInt();
59 byte[] myPayload = new byte[myLength - 4];
60 in.get(myPayload);
61
62 out.write(new Message(myConversationId, myPayload));
63
64 return true;
65 }
66 }