Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.internal.message.field;
00018
00019 import com.google.common.base.Preconditions;
00020
00021 import org.jboss.netty.buffer.ChannelBuffer;
00022
00023 import java.util.Arrays;
00024
00028 public class ByteArrayField extends Field {
00029
00030 private final int size;
00031
00032 private byte[] value;
00033
00034 public static ByteArrayField newVariable(FieldType type, String name, int size) {
00035 return new ByteArrayField(type, name, size);
00036 }
00037
00038 private ByteArrayField(FieldType type, String name, int size) {
00039 super(type, name, false);
00040 this.size = size;
00041 setValue(new byte[Math.max(0, size)]);
00042 }
00043
00044 @SuppressWarnings("unchecked")
00045 @Override
00046 public byte[] getValue() {
00047 return value;
00048 }
00049
00050 @Override
00051 public void setValue(Object value) {
00052 Preconditions.checkArgument(size < 0 || ((byte[]) value).length == size);
00053 this.value = (byte[]) value;
00054 }
00055
00056 @Override
00057 public void serialize(ChannelBuffer buffer) {
00058 if (size < 0) {
00059 buffer.writeInt(value.length);
00060 }
00061 for (byte v : value) {
00062 type.serialize(v, buffer);
00063 }
00064 }
00065
00066 @Override
00067 public void deserialize(ChannelBuffer buffer) {
00068 int currentSize = size;
00069 if (currentSize < 0) {
00070 currentSize = buffer.readInt();
00071 }
00072 value = new byte[currentSize];
00073 for (int i = 0; i < currentSize; i++) {
00074 value[i] = buffer.readByte();
00075 }
00076 }
00077
00078 @Override
00079 public String getMd5String() {
00080 return String.format("%s %s\n", type, name);
00081 }
00082
00083 @Override
00084 public String getJavaTypeName() {
00085 return type.getJavaTypeName() + "[]";
00086 }
00087
00088 @Override
00089 public String toString() {
00090 return "ByteArrayField<" + type + ", " + name + ">";
00091 }
00092
00093 @Override
00094 public int hashCode() {
00095 final int prime = 31;
00096 int result = super.hashCode();
00097 result = prime * result + ((value == null) ? 0 : value.hashCode());
00098 return result;
00099 }
00100
00101 @Override
00102 public boolean equals(Object obj) {
00103 if (this == obj)
00104 return true;
00105 if (!super.equals(obj))
00106 return false;
00107 if (getClass() != obj.getClass())
00108 return false;
00109 ByteArrayField other = (ByteArrayField) obj;
00110 if (value == null) {
00111 if (other.value != null)
00112 return false;
00113 } else if (!Arrays.equals(value, other.value))
00114 return false;
00115 return true;
00116 }
00117 }