1 4 package com.tc.memorydatastore.message; 5 6 import com.tc.io.TCByteBufferInputStream; 7 import com.tc.io.TCByteBufferOutput; 8 import com.tc.io.TCSerializable; 9 10 import java.io.IOException ; 11 import java.util.ArrayList ; 12 import java.util.Collection ; 13 import java.util.Iterator ; 14 15 public class TCMemoryDataStoreMessageData implements TCSerializable { 16 private final int type; 17 private byte[] key; 18 private byte[] value; 19 private Collection values; 20 21 public TCMemoryDataStoreMessageData(int type) { 22 this.type = type; 23 } 24 25 public TCMemoryDataStoreMessageData(int type, byte[] key) { 26 this.type = type; 27 this.key = key; 28 this.value = null; 29 this.values = null; 30 } 31 32 public TCMemoryDataStoreMessageData(int type, byte[] key, Collection values) { 33 this.type = type; 34 this.key = key; 35 this.values = values; 36 } 37 38 public TCMemoryDataStoreMessageData(int type, byte[] key, byte[] value) { 39 this.type = type; 40 this.key = key; 41 this.value = value; 42 } 43 44 public byte[] getKey() { 45 return key; 46 } 47 48 public byte[] getValue() { 49 return value; 50 } 51 52 public Collection getValues() { 53 return values; 54 } 55 56 public void serializeTo(TCByteBufferOutput serialOutput) { 57 if (key != null) { 58 serialOutput.writeInt(key.length); 60 serialOutput.write(key); 61 } 62 if (value != null) { 63 serialOutput.writeInt(value.length); 65 serialOutput.write(value); 66 } else { 67 if (type == MemoryDataStoreResponseMessage.GET_RESPONSE || type == MemoryDataStoreResponseMessage.REMOVE_RESPONSE 68 || type == MemoryDataStoreResponseMessage.REMOVE_ALL_RESPONSE) { 69 serialOutput.writeInt(0); 70 } else if (type == MemoryDataStoreResponseMessage.GET_ALL_RESPONSE) { 71 serialOutput.writeInt(values.size()); 72 for (Iterator i = values.iterator(); i.hasNext();) { 73 TCByteArrayKeyValuePair keyValuePair = (TCByteArrayKeyValuePair)i.next(); 74 keyValuePair.serializeTo(serialOutput); 75 } 76 } 77 } 78 } 79 80 public Object deserializeFrom(TCByteBufferInputStream serialInput) throws IOException { 81 int length = 0; 82 switch (type) { 83 case MemoryDataStoreRequestMessage.PUT: 84 length = serialInput.readInt(); 85 this.key = new byte[length]; 86 serialInput.read(this.key); 87 88 length = serialInput.readInt(); 89 this.value = new byte[length]; 90 serialInput.read(this.value); 91 break; 92 case MemoryDataStoreRequestMessage.GET: 93 length = serialInput.readInt(); 94 this.key = new byte[length]; 95 serialInput.read(this.key); 96 break; 97 case MemoryDataStoreRequestMessage.REMOVE: 98 length = serialInput.readInt(); 99 this.key = new byte[length]; 100 serialInput.read(this.key); 101 break; 102 case MemoryDataStoreResponseMessage.GET_RESPONSE: 103 length = serialInput.readInt(); 104 if (length > 0) { 105 this.value = new byte[length]; 106 serialInput.read(this.value); 107 } 108 break; 109 case MemoryDataStoreResponseMessage.GET_ALL_RESPONSE: 110 int size = serialInput.readInt(); 111 this.values = new ArrayList (size); 112 for (int i = 0; i < size; i++) { 113 TCByteArrayKeyValuePair keyValuePair = new TCByteArrayKeyValuePair(); 114 keyValuePair.deserializeFrom(serialInput); 115 values.add(keyValuePair); 116 } 117 break; 118 case MemoryDataStoreResponseMessage.REMOVE_RESPONSE: 119 length = serialInput.readInt(); 120 if (length > 0) { 121 this.value = new byte[length]; 122 serialInput.read(this.value); 123 } 124 break; 125 } 126 return this; 127 } 128 } | Popular Tags |