ChannelBufferField.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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 import org.ros.internal.message.MessageBuffers;
00023 
00024 import java.nio.ByteOrder;
00025 
00029 public class ChannelBufferField extends Field {
00030 
00031   private final int size;
00032 
00033   private ChannelBuffer value;
00034 
00035   public static ChannelBufferField newVariable(FieldType type, String name, int size) {
00036     return new ChannelBufferField(type, name, size);
00037   }
00038 
00039   private ChannelBufferField(FieldType type, String name, int size) {
00040     super(type, name, false);
00041     this.size = size;
00042     value = MessageBuffers.dynamicBuffer();
00043   }
00044 
00045   @SuppressWarnings("unchecked")
00046   @Override
00047   public ChannelBuffer getValue() {
00048     // Return a defensive duplicate. Unlike with copy(), duplicated
00049     // ChannelBuffers share the same backing array, so this is relatively cheap.
00050     return value.duplicate();
00051   }
00052 
00053   @Override
00054   public void setValue(Object value) {
00055     Preconditions.checkArgument(((ChannelBuffer) value).order() == ByteOrder.LITTLE_ENDIAN);
00056     Preconditions.checkArgument(size < 0 || ((ChannelBuffer) value).readableBytes() == size);
00057     this.value = (ChannelBuffer) value;
00058   }
00059 
00060   @Override
00061   public void serialize(ChannelBuffer buffer) {
00062     if (size < 0) {
00063       buffer.writeInt(value.readableBytes());
00064     }
00065     // By specifying the start index and length we avoid modifying value's
00066     // indices and marks.
00067     buffer.writeBytes(value, 0, value.readableBytes());
00068   }
00069 
00070   @Override
00071   public void deserialize(ChannelBuffer buffer) {
00072     int currentSize = size;
00073     if (currentSize < 0) {
00074       currentSize = buffer.readInt();
00075     }
00076     value = buffer.readSlice(currentSize);
00077   }
00078 
00079   @Override
00080   public String getMd5String() {
00081     return String.format("%s %s\n", type, name);
00082   }
00083 
00084   @Override
00085   public String getJavaTypeName() {
00086     return "org.jboss.netty.buffer.ChannelBuffer";
00087   }
00088 
00089   @Override
00090   public String toString() {
00091     return "ChannelBufferField<" + type + ", " + name + ">";
00092   }
00093 
00094   @Override
00095   public int hashCode() {
00096     final int prime = 31;
00097     int result = super.hashCode();
00098     result = prime * result + ((value == null) ? 0 : value.hashCode());
00099     return result;
00100   }
00101 
00102   @Override
00103   public boolean equals(Object obj) {
00104     if (this == obj)
00105       return true;
00106     if (!super.equals(obj))
00107       return false;
00108     if (getClass() != obj.getClass())
00109       return false;
00110     ChannelBufferField other = (ChannelBufferField) obj;
00111     if (value == null) {
00112       if (other.value != null)
00113         return false;
00114     } else if (!value.equals(other.value))
00115       return false;
00116     return true;
00117   }
00118 }


rosjava_bootstrap
Author(s): Daniel Stonier , Damon Kohler
autogenerated on Fri Aug 28 2015 12:41:44