KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > smartclient > message > AbsNameBytesMessage


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id:$
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component.smartclient.message;
27
28 import java.nio.ByteBuffer JavaDoc;
29
30 /**
31  * Abstract class used for exchanging a name and an array of bytes.
32  * @author Florent Benoit
33  */

34 public abstract class AbsNameBytesMessage extends AbsMessage {
35
36     /**
37      * Name contained in the message.
38      */

39     private String JavaDoc name = null;
40
41     /**
42      * Array of bytes stored in the message.
43      */

44     private byte[] bytes = null;
45
46     /**
47      * Builds a new message with the given name and the array of bytes.
48      * @param name the given name
49      * @param bytes the array of bytes to store.
50      */

51     public AbsNameBytesMessage(final String JavaDoc name, final byte[] bytes) {
52         super();
53         this.name = name;
54         this.bytes = bytes;
55     }
56
57     /**
58      * Builds an message with the given bytebuffer.
59      * @param dataBuffer buffer containing the data to extract.
60      */

61     public AbsNameBytesMessage(final ByteBuffer JavaDoc dataBuffer) {
62         super();
63         // Get length of the name
64
int lengthName = dataBuffer.getInt();
65
66
67         // allocate buffer with the size of the name
68
ByteBuffer JavaDoc nameBuffer = ByteBuffer.allocate(lengthName);
69         for (int l = 0; l < lengthName; l++) {
70             byte b = dataBuffer.get();
71             nameBuffer.put(b);
72         }
73         // decode the name
74
nameBuffer.position(0);
75         // set it
76
this.name = decode(nameBuffer);
77
78         // rest of bytes = bytecode
79
this.bytes = new byte[dataBuffer.limit() - dataBuffer.position()];
80         int k = 0;
81         for (int i = dataBuffer.position(); i < dataBuffer.limit(); i++) {
82             this.bytes[k++] = dataBuffer.get(i);
83         }
84
85     }
86
87     /**
88      * Gets the OpCode of this message.
89      * @return the operation code.
90      */

91     @Override JavaDoc
92     public abstract byte getOpCode();
93
94     /**
95      * Gets the content of this message (only this part, not the header).
96      * @return the content of this message.
97      */

98     @Override JavaDoc
99     public ByteBuffer JavaDoc getSubMessage() {
100         // Encode the classname
101
ByteBuffer JavaDoc nameBuffer = encode(name);
102         nameBuffer.position(0);
103
104         // create buffer : length's name(int) + name + bytecode
105
ByteBuffer JavaDoc messageBuffer = ByteBuffer.allocate(INT_BYTE_SIZE + nameBuffer.capacity() + bytes.length);
106
107         // appends length
108
messageBuffer.putInt(nameBuffer.capacity());
109
110         // Needs to append the name
111
messageBuffer.put(nameBuffer);
112
113         // Bytecode
114
messageBuffer.put(bytes);
115
116         return messageBuffer;
117     }
118
119     /**
120      * Gets the name of this message.
121      * @return the name of this message.
122      */

123     public String JavaDoc getName() {
124         return name;
125     }
126
127     /**
128      * Gets the bytes of this message.
129      * @return the bytes of this message.
130      */

131     public byte[] getBytes() {
132         return bytes;
133     }
134 }
135
Popular Tags