15 package com.neuronrobotics.sdk.common;
17 import java.nio.ByteBuffer;
18 import java.nio.LongBuffer;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.ListIterator;
24 import java.util.Vector;
26 import com.neuronrobotics.sdk.config.SDKBuildInfo;
50 private List<Byte>
store =
new ArrayList<Byte>();
110 for(
int i=0;i<data.length;i++){
121 for(
int i=0;i<arguments.length;i++){
122 add(arguments[i].getValue());
149 public synchronized boolean add(
byte data) {
156 Log.
info(
"Bytelist static buffer overflow, resizing to "+newSize);
161 for(
int i=0;i<tmpBuff.length;i++){
174 return store.add(data);
183 public boolean add(
int data) {
184 return add((
byte) data);
197 public boolean add(Byte b) {
198 return add(b.byteValue());
217 public boolean add(String data) {
218 return add(data.getBytes());
227 public boolean add(
byte [] data) {
233 for(
int i = 0; i < index; i++) {
249 public boolean add(
int [] data) {
255 for(
int i = 0; i < index; i++) {
271 public boolean add(Byte [] bl) {
277 for(
int i = 0; i < index; i++) {
310 public boolean add(
byte[] data,
int len,
int offset) {
311 byte[] out =
new byte[len];
312 System.arraycopy(data, offset, out, 0, len);
346 public boolean addAll(Collection<? extends Byte> c) {
347 Byte b[] =
new Byte[c.size()];
348 return add(c.toArray(b));
362 public boolean addAll(
int index, Collection<? extends Byte> c) {
376 return store.isEmpty();
394 public synchronized byte[]
getBytes(
int start,
int len) {
395 int sizeLocal =
size();
396 int requestedLen=len;
408 if(start > sizeLocal) {
413 if(start+len > sizeLocal) {
414 len = sizeLocal- start - 1;
417 throw new RuntimeException(
"Requesting more bytes then in the list, size="+sizeLocal+
" start="+start+
" len="+len+
" requested="+ requestedLen);
420 byte out[] =
new byte[len];
426 for(
int i=0;i<start;i++){
433 for(
int i=0;i<len;i++) {
439 List<Byte> iter =
store.subList(start, start+len);
440 for(
int i=0;i<len;i++) {
441 out[i] = iter.get(i);
465 if(index < 0 || index >
size-1) {
466 throw new IndexOutOfBoundsException(
"Requested : "+index+
" have : "+
size());
492 public Byte
pop(
int index) {
494 throw new RuntimeException(
"Can not pop a list of length "+index);
501 for(
int i=0;i<index;i++)
516 }
catch (Exception e) {
534 for(
int i=0;i<index;i++){
554 public static byte[]
wrap(
int value) {
555 byte [] b = {(byte) value};
588 public <T> T[]
toArray(T[] a) {
590 throw new UnsupportedOperationException();
592 return store.toArray(a);
607 throw new UnsupportedOperationException();
609 return store.contains(o);
624 throw new UnsupportedOperationException();
626 return store.containsAll(c);
639 public Byte
get(
int index) {
642 Log.
error(
"Requesting data out of an empty ByteList");
643 throw new RuntimeException(
"Requesting data out of an empty ByteList");
662 Log.
error(
"Requesting data out of an empty ByteList");
663 throw new RuntimeException(
"Requesting data out of an empty ByteList");
677 throw new UnsupportedOperationException();
679 return store.indexOf(o);
688 return new Iterator<Byte>() {
693 public boolean hasNext() {
694 return readIndex !=
size;
700 return data[readIndex++];
704 public void remove() {
709 return store.iterator();
724 throw new UnsupportedOperationException();
726 return store.lastIndexOf(o);
740 throw new UnsupportedOperationException();
742 return store.listIterator();
757 throw new UnsupportedOperationException();
759 return store.listIterator(index);
772 public boolean remove(Object o) {
774 throw new UnsupportedOperationException();
776 return store.remove(o);
789 public synchronized Byte
remove(
int index) {
798 return store.remove(index);
812 public List<Byte>
subList(
int fromIndex,
int toIndex) {
814 byte [] content =
getBytes(fromIndex, toIndex-fromIndex);
815 ArrayList<Byte> back =
new ArrayList<Byte>();
816 for(
int i=0;i<content.length;i++){
817 back.add(content[i]);
821 return store.subList(fromIndex, toIndex);
835 throw new UnsupportedOperationException();
837 return store.toArray();
849 rtn += String.format(
"%02x ", x);
851 rtn = rtn.substring(0, rtn.length()-1);
852 return rtn.toUpperCase();
862 if(data.length == 0) {
868 while(i<data.length ){
894 byte[] bArray =
new byte[8];
895 byte[] rtn =
new byte[
split];
896 ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
897 LongBuffer lBuffer = bBuffer.asLongBuffer();
898 lBuffer.put(0, data);
900 System.arraycopy(bArray, 8-
split, rtn, 0,
split);
930 long bytes = b.length;
932 throw new RuntimeException(
"Ints can only have 2 or 4 bytes");
935 for (
int i=0;i<bytes;i++){
937 int power =(int) ((bytes-i-1)*8);
938 long val = (long) (tmp * Math.pow(2, power ));
943 long power = bytes*8;
944 long max_pos_val =(long) Math.pow(2,power-1);
945 long sub_val = (long) Math.pow(2,power);
947 if (out>max_pos_val){
948 abs_val=(sub_val-out);
949 out=(int) (-1*abs_val);
963 byte b [] =
new byte[2];
965 b[0] = (byte)(value >> 8);
966 b[1] = (byte)(value);
978 byte b [] =
new byte[4];
980 b[0] = (byte)(value >> 24);
981 b[1] = (byte)(value >> 16);
982 b[2] = (byte)(value >> 8);
983 b[3] = (byte)(value);
998 public void add(
int arg0, Byte arg1) {
999 throw new UnsupportedOperationException();
1013 throw new UnsupportedOperationException();
1027 throw new UnsupportedOperationException();
1041 public Byte
set(
int arg0, Byte arg1) {
1042 throw new UnsupportedOperationException();
1055 for(
int i=0;i<current.length;i++){
1062 store.add(index, val);
1089 if (
size() >= off+len) {
1090 byte [] ret =
new byte[len];
1091 for(
int i=0;i<len;i++) {
1096 throw new IndexOutOfBoundsException();
static final int MAX_PACKET_SIZE
boolean add(ISendable sendable)
byte[] popList(int index)
ListIterator< Byte > listIterator(int index)
Iterator< Byte > iterator()
void setStaticBufferSize(int staticBufferSize)
boolean add(byte[] data, int len, int offset)
int staticBufferWritePointer
void add(int arg0, Byte arg1)
synchronized boolean add(byte data)
static byte[] split(long data, int split)
static void setUseStaticBuffer(boolean useStaticBuffer)
byte[] getBytes(int index)
void insert(int index, byte val)
ByteList(BowlerDataType[] arguments)
static int convertToInt(byte[] b, boolean Signed)
static int rawByteToInt(byte b)
static boolean isUseStaticBuffer()
byte[] popList(int off, int len)
boolean contains(Object o)
boolean removeAll(Collection<?> c)
static final int convertToInt(byte[] b)
int getUnsigned(int index)
boolean containsAll(Collection<?> c)
boolean addAs16(int value)
int getStaticBufferSize()
boolean addAll(Collection<? extends Byte > c)
boolean add(long data, int split)
boolean addAs32(int value)
int lastIndexOf(Object o)
ListIterator< Byte > listIterator()
boolean addAll(int index, Collection<? extends Byte > c)
int staticBufferReadPointer
int getStaticBufferByteCount()
static byte[] wrap(int value)
List< Byte > subList(int fromIndex, int toIndex)
static final boolean useStaticBuffer
boolean retainAll(Collection<?> c)
static byte[] convertTo16(int value)
static byte[] convertTo32(int value)
synchronized byte[] getBytes(int start, int len)
static void info(String message)
static void error(String message)