001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: AbsNameBytesMessage.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.smartclient.message;
025:
026: import java.nio.ByteBuffer;
027:
028: /**
029: * Abstract class used for exchanging a name and an array of bytes.
030: * @author Florent Benoit
031: */
032: public abstract class AbsNameBytesMessage extends AbsMessage {
033:
034: /**
035: * Name contained in the message.
036: */
037: private String name = null;
038:
039: /**
040: * Array of bytes stored in the message.
041: */
042: private byte[] bytes = null;
043:
044: /**
045: * Builds a new message with the given name and the array of bytes.
046: * @param name the given name
047: * @param bytes the array of bytes to store.
048: */
049: public AbsNameBytesMessage(final String name, final byte[] bytes) {
050: super ();
051: this .name = name;
052: this .bytes = bytes;
053: }
054:
055: /**
056: * Builds an message with the given bytebuffer.
057: * @param dataBuffer buffer containing the data to extract.
058: */
059: public AbsNameBytesMessage(final ByteBuffer dataBuffer) {
060: super ();
061: // Get length of the name
062: int lengthName = dataBuffer.getInt();
063:
064: // allocate buffer with the size of the name
065: ByteBuffer nameBuffer = ByteBuffer.allocate(lengthName);
066: for (int l = 0; l < lengthName; l++) {
067: byte b = dataBuffer.get();
068: nameBuffer.put(b);
069: }
070: // decode the name
071: nameBuffer.position(0);
072: // set it
073: this .name = decode(nameBuffer);
074:
075: // rest of bytes = bytecode
076: this .bytes = new byte[dataBuffer.limit()
077: - dataBuffer.position()];
078: int k = 0;
079: for (int i = dataBuffer.position(); i < dataBuffer.limit(); i++) {
080: this .bytes[k++] = dataBuffer.get(i);
081: }
082:
083: }
084:
085: /**
086: * Gets the OpCode of this message.
087: * @return the operation code.
088: */
089: @Override
090: public abstract byte getOpCode();
091:
092: /**
093: * Gets the content of this message (only this part, not the header).
094: * @return the content of this message.
095: */
096: @Override
097: public ByteBuffer getSubMessage() {
098: // Encode the classname
099: ByteBuffer nameBuffer = encode(name);
100: nameBuffer.position(0);
101:
102: // create buffer : length's name(int) + name + bytecode
103: ByteBuffer messageBuffer = ByteBuffer.allocate(INT_BYTE_SIZE
104: + nameBuffer.capacity() + bytes.length);
105:
106: // appends length
107: messageBuffer.putInt(nameBuffer.capacity());
108:
109: // Needs to append the name
110: messageBuffer.put(nameBuffer);
111:
112: // Bytecode
113: messageBuffer.put(bytes);
114:
115: return messageBuffer;
116: }
117:
118: /**
119: * Gets the name of this message.
120: * @return the name of this message.
121: */
122: public String getName() {
123: return name;
124: }
125:
126: /**
127: * Gets the bytes of this message.
128: * @return the bytes of this message.
129: */
130: public byte[] getBytes() {
131: return bytes;
132: }
133: }
|