001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.memorydatastore.message;
005:
006: import com.tc.io.TCByteBufferInput;
007: import com.tc.io.TCByteBufferOutput;
008: import com.tc.io.TCSerializable;
009:
010: import java.io.IOException;
011: import java.util.ArrayList;
012: import java.util.Collection;
013: import java.util.Iterator;
014:
015: public class TCMemoryDataStoreMessageData implements TCSerializable {
016: private final int type;
017: private byte[] key;
018: private byte[] value;
019: private Collection values;
020:
021: public TCMemoryDataStoreMessageData(int type) {
022: this .type = type;
023: }
024:
025: public TCMemoryDataStoreMessageData(int type, byte[] key) {
026: this .type = type;
027: this .key = key;
028: this .value = null;
029: this .values = null;
030: }
031:
032: public TCMemoryDataStoreMessageData(int type, byte[] key,
033: Collection values) {
034: this .type = type;
035: this .key = key;
036: this .values = values;
037: }
038:
039: public TCMemoryDataStoreMessageData(int type, byte[] key,
040: byte[] value) {
041: this .type = type;
042: this .key = key;
043: this .value = value;
044: }
045:
046: public byte[] getKey() {
047: return key;
048: }
049:
050: public byte[] getValue() {
051: return value;
052: }
053:
054: public Collection getValues() {
055: return values;
056: }
057:
058: public void serializeTo(TCByteBufferOutput serialOutput) {
059: if (key != null) {
060: // serialOutput.writeByte(KEY);
061: serialOutput.writeInt(key.length);
062: serialOutput.write(key);
063: }
064: if (value != null) {
065: // serialOutput.writeByte(VALUE);
066: serialOutput.writeInt(value.length);
067: serialOutput.write(value);
068: } else {
069: if (type == MemoryDataStoreResponseMessage.GET_RESPONSE
070: || type == MemoryDataStoreResponseMessage.REMOVE_RESPONSE
071: || type == MemoryDataStoreResponseMessage.REMOVE_ALL_RESPONSE) {
072: serialOutput.writeInt(0);
073: } else if (type == MemoryDataStoreResponseMessage.GET_ALL_RESPONSE) {
074: serialOutput.writeInt(values.size());
075: for (Iterator i = values.iterator(); i.hasNext();) {
076: TCByteArrayKeyValuePair keyValuePair = (TCByteArrayKeyValuePair) i
077: .next();
078: keyValuePair.serializeTo(serialOutput);
079: }
080: }
081: }
082: }
083:
084: public Object deserializeFrom(TCByteBufferInput serialInput)
085: throws IOException {
086: int length = 0;
087: switch (type) {
088: case MemoryDataStoreRequestMessage.PUT:
089: length = serialInput.readInt();
090: this .key = new byte[length];
091: serialInput.read(this .key);
092:
093: length = serialInput.readInt();
094: this .value = new byte[length];
095: serialInput.read(this .value);
096: break;
097: case MemoryDataStoreRequestMessage.GET:
098: length = serialInput.readInt();
099: this .key = new byte[length];
100: serialInput.read(this .key);
101: break;
102: case MemoryDataStoreRequestMessage.REMOVE:
103: length = serialInput.readInt();
104: this .key = new byte[length];
105: serialInput.read(this .key);
106: break;
107: case MemoryDataStoreResponseMessage.GET_RESPONSE:
108: length = serialInput.readInt();
109: if (length > 0) {
110: this .value = new byte[length];
111: serialInput.read(this .value);
112: }
113: break;
114: case MemoryDataStoreResponseMessage.GET_ALL_RESPONSE:
115: int size = serialInput.readInt();
116: this .values = new ArrayList(size);
117: for (int i = 0; i < size; i++) {
118: TCByteArrayKeyValuePair keyValuePair = new TCByteArrayKeyValuePair();
119: keyValuePair.deserializeFrom(serialInput);
120: values.add(keyValuePair);
121: }
122: break;
123: case MemoryDataStoreResponseMessage.REMOVE_RESPONSE:
124: length = serialInput.readInt();
125: if (length > 0) {
126: this .value = new byte[length];
127: serialInput.read(this.value);
128: }
129: break;
130: }
131: return this;
132: }
133: }
|