RLPark 1.0.0
Reinforcement Learning Framework in Java

LiteByteBuffer.java

Go to the documentation of this file.
00001 package rlpark.plugin.robot.internal.sync;
00002 
00003 import java.nio.ByteOrder;
00004 import java.util.Arrays;
00005 
00006 public class LiteByteBuffer {
00007   private final byte[] array;
00008   private final ByteOrder order;
00009   private int offset;
00010 
00011   public LiteByteBuffer(int dataSize) {
00012     this(dataSize, ByteOrder.BIG_ENDIAN);
00013   }
00014 
00015   public LiteByteBuffer(int capacity, ByteOrder order) {
00016     assert capacity > 0;
00017     this.order = order;
00018     array = new byte[capacity];
00019   }
00020 
00021   public LiteByteBuffer(byte[] buffer, ByteOrder order) {
00022     this.order = order;
00023     array = buffer.clone();
00024   }
00025 
00026   public ByteOrder order() {
00027     return order;
00028   }
00029 
00030   public byte[] array() {
00031     return array;
00032   }
00033 
00034   public int capacity() {
00035     return array.length;
00036   }
00037 
00038   public void clear() {
00039     reset();
00040     Arrays.fill(array, (byte) 0);
00041   }
00042 
00043   public void reset() {
00044     offset = 0;
00045   }
00046 
00047   public byte get() {
00048     return get(getMoveOffset(1));
00049   }
00050 
00051   public byte get(int index) {
00052     return array[index];
00053   }
00054 
00055   private int getMoveOffset(int shift) {
00056     int result = offset;
00057     offset += shift;
00058     assert offset <= array.length;
00059     return result;
00060   }
00061 
00062   public float getFloat() {
00063     return getFloat(getMoveOffset(4));
00064   }
00065 
00066   public float getFloat(int i) {
00067     return Float.intBitsToFloat(getBits(i, 4));
00068   }
00069 
00070   public int getInt(int i) {
00071     return getBits(i, 4);
00072   }
00073 
00074   public short getShort() {
00075     return getShort(getMoveOffset(2));
00076   }
00077 
00078   public short getShort(int index) {
00079     return (short) getBits(index, 2);
00080   }
00081 
00082   private int getBits(int offset, int length) {
00083     assert length > 0 && length <= 8;
00084     if (order == ByteOrder.BIG_ENDIAN)
00085       return getBitsBigEndian(offset, length);
00086     return getBitsSmallEndian(offset, length);
00087   }
00088 
00089   public int getBitsBigEndian(int offset, int length) {
00090     int result = 0;
00091     for (int i = 0; i < length; i++) {
00092       result = result | (0x000000ff & array[offset + i]);
00093       if (length - i > 1)
00094         result = result << 8;
00095     }
00096     return result;
00097   }
00098 
00099   public int getBitsSmallEndian(int offset, int length) {
00100     int result = 0;
00101     for (int i = 0; i < length; i++) {
00102       result = result | (0x000000ff & array[offset + length - i - 1]);
00103       if (length - i > 1)
00104         result = result << 8;
00105     }
00106     return result;
00107   }
00108 
00109   public int getInt() {
00110     return getInt(getMoveOffset(4));
00111   }
00112 
00113   public void put(byte value) {
00114     array[getMoveOffset(1)] = value;
00115   }
00116 
00117   public void put(byte[] values) {
00118     System.arraycopy(values, 0, array, getMoveOffset(values.length), values.length);
00119   }
00120 
00121   public void putFloat(float value) {
00122     putBits(Float.floatToIntBits(value), 4);
00123   }
00124 
00125   public void putShort(short value) {
00126     putBits(value, 2);
00127   }
00128 
00129   public void putInt(int value) {
00130     putBits(value, 4);
00131   }
00132 
00133   private void putBits(int value, int length) {
00134     assert length > 0 && length <= 8;
00135     int index = getMoveOffset(length);
00136     if (order == ByteOrder.BIG_ENDIAN)
00137       putBitsBigEndian(index, value, length);
00138     else
00139       putBitsLittleEndian(index, value, length);
00140   }
00141 
00142   private void putBitsBigEndian(int index, int value, int length) {
00143     int buffer = value;
00144     for (int i = 0; i < length; i++) {
00145       array[index + length - i - 1] = (byte) (buffer & 0xFF);
00146       buffer = buffer >> 8;
00147     }
00148   }
00149 
00150   private void putBitsLittleEndian(int index, int value, int length) {
00151     int buffer = value;
00152     for (int i = 0; i < length; i++) {
00153       array[index + i] = (byte) (buffer & 0xFF);
00154       buffer = buffer >> 8;
00155     }
00156   }
00157 
00158   public int offset() {
00159     return offset;
00160   }
00161 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark