BowlerKernel
ByteList.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15  package com.neuronrobotics.sdk.common;
16 
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;
25 
26 import com.neuronrobotics.sdk.config.SDKBuildInfo;
27 
28 // TODO: Auto-generated Javadoc
32 public class ByteList implements ISendable,Iterable<Byte> {
33 
35  private static final boolean useStaticBuffer = true;
36 
39 
41  private byte [] staticBuffer = new byte[staticBufferSize];
42 
44  private int staticBufferReadPointer = 0;
45 
47  private int staticBufferWritePointer = 0;
48 
50  private List<Byte> store = new ArrayList<Byte>();
51 
56  public ByteList() {
57 // if (SDKBuildInfo.isLinux() && SDKBuildInfo.isARM())
58 // useStaticBuffer = true;
59  if(isUseStaticBuffer()){
60  //Log.debug("Starting Static ByteList");
62  }else{
63  staticBuffer=null;
64  }
65  }
66 
72  public ByteList(Byte data) {
73  add(data);
74  }
75 
81  public ByteList(byte[] data) {
82 
83  add(data);
84  }
85 
91  public ByteList(String data) {
92  add(data);
93  }
94 
100  public ByteList(int data) {
101  add(data);
102  }
103 
109  public ByteList(int[] data) {
110  for(int i=0;i<data.length;i++){
111  add(data[i]);
112  }
113  }
114 
120  public ByteList(BowlerDataType[] arguments) {
121  for(int i=0;i<arguments.length;i++){
122  add(arguments[i].getValue());
123  }
124  }
125 
133  int r = staticBufferReadPointer;
134  if(w>r){
135  return w-r;
136  }else if(w==r){
137  return 0;
138  }else{
139  return (w+staticBuffer.length)-r;
140  }
141  }
142 
149  public synchronized boolean add(byte data) {
150  if(isUseStaticBuffer()){
151  if(staticBuffer == null){
153  }
154  if(getStaticBufferByteCount()>=(staticBuffer.length-1)){
155  int newSize = staticBufferSize*2;
156  Log.info("Bytelist static buffer overflow, resizing to "+newSize);
157  byte tmpBuff[] = getBytes();
158  // Double the buffer size
159  setStaticBufferSize(newSize);
160  //load the old data into newly resized buffer
161  for(int i=0;i<tmpBuff.length;i++){
162  staticBuffer[staticBufferWritePointer++] = tmpBuff[i];
163  if(staticBufferWritePointer == (staticBuffer.length)){
165  }
166  }
167  }
169  if(staticBufferWritePointer == (staticBuffer.length)){
171  }
172  return true;
173  }
174  return store.add(data);
175  }
176 
183  public boolean add(int data) {
184  return add((byte) data);
185  }
186 
187  /* (non-Javadoc)
188  * @see java.util.List#add(java.lang.Object)
189  */
190 
197  public boolean add(Byte b) {
198  return add(b.byteValue());
199  }
200 
207  public boolean add(ISendable sendable) {
208  return add(sendable.getBytes());
209  }
210 
217  public boolean add(String data) {
218  return add(data.getBytes());
219  }
220 
227  public boolean add(byte [] data) {
228  int index = 0;
229  for(byte b : data) {
230  if(!add(b)) {
231 
232  // remove all the added bytes if there was an error adding a byte
233  for(int i = 0; i < index; i++) {
234  remove(size());
235  }
236  }
237  index++;
238  }
239 
240  return true;
241  }
242 
249  public boolean add(int [] data) {
250  int index = 0;
251  for(int d : data) {
252  if(!add(d)) {
253 
254  // remove all the added bytes if there was an error adding a byte
255  for(int i = 0; i < index; i++) {
256  remove(size());
257  }
258  }
259  index++;
260  }
261 
262  return true;
263  }
264 
271  public boolean add(Byte [] bl) {
272  int index = 0;
273  for(Byte b : bl) {
274  if(!add(b)) {
275 
276  // remove all the added bytes if there was an error adding a byte
277  for(int i = 0; i < index; i++) {
278  remove(size());
279  }
280  }
281  index++;
282  }
283 
284  return true;
285  }
286 
295  @Deprecated
296  public boolean add(long data, int split) {
297  return add(split(data, split));
298  }
299 
300 
301 
310  public boolean add(byte[] data, int len, int offset) {
311  byte[] out = new byte[len];
312  System.arraycopy(data, offset, out, 0, len);
313  return add(out);
314  }
315 
322  public boolean addAs16(int value) {
323  return add(convertTo16(value));
324  }
325 
332  public boolean addAs32(int value) {
333  return add(convertTo32(value));
334  }
335 
336  /* (non-Javadoc)
337  * @see java.util.List#addAll(java.util.Collection)
338  */
339 
346  public boolean addAll(Collection<? extends Byte> c) {
347  Byte b[] = new Byte[c.size()];
348  return add(c.toArray(b));
349  }
350 
351  /* (non-Javadoc)
352  * @see java.util.List#addAll(int, java.util.Collection)
353  */
354 
362  public boolean addAll(int index, Collection<? extends Byte> c) {
363  // TODO Auto-generated method stub
364  return false;
365  }
366 
372  public boolean isEmpty() {
373  if(isUseStaticBuffer()){
374  return getStaticBufferByteCount() == 0;
375  }
376  return store.isEmpty();
377  }
378 
379  /* (non-Javadoc)
380  * @see com.neuronrobotics.sdk.common.ISendable#getBytes()
381  */
382 
383  public byte[] getBytes() {
384  return getBytes(0, size());
385  }
386 
394  public synchronized byte[] getBytes(int start, int len) {
395  int sizeLocal =size();
396  int requestedLen=len;
397  // starting offset that is less than 0
398  if(start < 0) {
399  start = 0;
400  }
401 
402  // length that is less than 0
403  if(len < 0) {
404  len = 0;
405  }
406 
407  // starting offset is further then the last element
408  if(start > sizeLocal) {
409  return new byte [0];
410  }
411 
412  // the ending position is
413  if(start+len > sizeLocal) {
414  len = sizeLocal- start - 1;
415  }
416  if(len < 0) {
417  throw new RuntimeException("Requesting more bytes then in the list, size="+sizeLocal+" start="+start+" len="+len+" requested="+ requestedLen);
418  }
419 
420  byte out[] = new byte[len];
421 
422  if(isUseStaticBuffer()){
423  int tmpRead = staticBufferReadPointer;
424  //Allign the start pointer
425  if(start != 0){
426  for(int i=0;i<start;i++){
427  tmpRead++;
428  if(tmpRead== staticBuffer.length)
429  tmpRead=0;
430  }
431  }
432  //do the peek read
433  for(int i=0;i<len;i++) {
434  out[i] = staticBuffer[tmpRead++];
435  if(tmpRead== staticBuffer.length)
436  tmpRead=0;
437  }
438  }else{
439  List<Byte> iter = store.subList(start, start+len);
440  for(int i=0;i<len;i++) {
441  out[i] = iter.get(i);
442  }
443  }
444  return out;
445  }
446 
453  public byte[] getBytes(int index) {
454  return getBytes(index, size()-index);
455  }
456 
463  public byte getByte(int index) {
464  int size = size();
465  if(index < 0 || index > size-1) {
466  throw new IndexOutOfBoundsException("Requested : "+index+" have : "+size());
467  }
468 
469  return getBytes(index,1)[0];
470 
471  }
472 
478  public int size() {
479  if(isUseStaticBuffer()){
480  return getStaticBufferByteCount();
481  }
482  return store.size();
483  }
484 
492  public Byte pop(int index) {
493  if(index < 0) {
494  throw new RuntimeException("Can not pop a list of length "+index);
495  }
496 
497  if(index > size()) {
498  clear();
499  return null;
500  }
501  for(int i=0;i<index;i++)
502  pop();
503 
504  return pop();
505  }
506 
512  public Byte pop() {
513  Byte b=null;;
514  try {
515  b = remove(0);
516  } catch (Exception e) {
517  b = null;
518  }
519  return b;
520  }
521 
527  public byte[] popList(int index) {
528  byte[] rtn;
529 
530  if(index < size()) {
531  //The first param is inclusive, the second is exclusive
532  rtn = getBytes(0, index);
533  if(isUseStaticBuffer()){
534  for(int i=0;i<index;i++){
535  remove(0);
536  }
537  }else{
538  //The first param is inclusive, the second is exclusive
539  store = store.subList(index, store.size());
540  }
541  } else {
542  rtn = getBytes();
543  clear();
544  }
545  return rtn;
546  }
547 
554  public static byte[] wrap(int value) {
555  byte [] b = {(byte) value};
556 
557  return b;
558  }
559 
560 
561 
562  /* (non-Javadoc)
563  * @see java.util.List#clear()
564  */
565 
569  public void clear() {
570  if(isUseStaticBuffer()){
572  return;
573  }
574  store.clear();
575  }
576 
577  /* (non-Javadoc)
578  * @see java.util.List#toArray(T[])
579  */
580 
588  public <T> T[] toArray(T[] a) {
589  if(isUseStaticBuffer()){
590  throw new UnsupportedOperationException();
591  }
592  return store.toArray(a);
593  }
594 
595  /* (non-Javadoc)
596  * @see java.util.List#contains(java.lang.Object)
597  */
598 
605  public boolean contains(Object o) {
606  if(isUseStaticBuffer()){
607  throw new UnsupportedOperationException();
608  }
609  return store.contains(o);
610  }
611 
612  /* (non-Javadoc)
613  * @see java.util.List#containsAll(java.util.Collection)
614  */
615 
622  public boolean containsAll(Collection<?> c) {
623  if(isUseStaticBuffer()){
624  throw new UnsupportedOperationException();
625  }
626  return store.containsAll(c);
627  }
628 
629  /* (non-Javadoc)
630  * @see java.util.List#get(int)
631  */
632 
639  public Byte get(int index) {
640  if(size()>0)
641  return getByte(index);
642  Log.error("Requesting data out of an empty ByteList");
643  throw new RuntimeException("Requesting data out of an empty ByteList");
644  }
645 
652  /* (non-Javadoc)
653  * @see java.util.List#get(int)
654  */
655  public int getUnsigned(int index) {
656  if(size()>0){
657  int val =get(index);
658  if(val<0)
659  val+=256;
660  return val;
661  }
662  Log.error("Requesting data out of an empty ByteList");
663  throw new RuntimeException("Requesting data out of an empty ByteList");
664  }
665  /* (non-Javadoc)
666  * @see java.util.List#indexOf(java.lang.Object)
667  */
668 
675  public int indexOf(Object o) {
676  if(isUseStaticBuffer()){
677  throw new UnsupportedOperationException();
678  }
679  return store.indexOf(o);
680  }
681 
682  /* (non-Javadoc)
683  * @see java.util.List#iterator()
684  */
685 
686  public Iterator<Byte> iterator() {
687  if(isUseStaticBuffer()){
688  return new Iterator<Byte>() {
690  int readIndex=0;
691  byte [] data = getBytes();
692  @Override
693  public boolean hasNext() {
694  return readIndex != size;
695  }
696 
697  @Override
698  public Byte next() {
699  // TODO Auto-generated method stub
700  return data[readIndex++];
701  }
702 
703  @Override
704  public void remove() {
705  readIndex++;
706  }
707  };
708  }
709  return store.iterator();
710  }
711 
712  /* (non-Javadoc)
713  * @see java.util.List#lastIndexOf(java.lang.Object)
714  */
715 
722  public int lastIndexOf(Object o) {
723  if(isUseStaticBuffer()){
724  throw new UnsupportedOperationException();
725  }
726  return store.lastIndexOf(o);
727  }
728 
729  /* (non-Javadoc)
730  * @see java.util.List#listIterator()
731  */
732 
738  public ListIterator<Byte> listIterator() {
739  if(isUseStaticBuffer()){
740  throw new UnsupportedOperationException();
741  }
742  return store.listIterator();
743  }
744 
745  /* (non-Javadoc)
746  * @see java.util.List#listIterator(int)
747  */
748 
755  public ListIterator<Byte> listIterator(int index) {
756  if(isUseStaticBuffer()){
757  throw new UnsupportedOperationException();
758  }
759  return store.listIterator(index);
760  }
761 
762  /* (non-Javadoc)
763  * @see java.util.List#remove(java.lang.Object)
764  */
765 
772  public boolean remove(Object o) {
773  if(isUseStaticBuffer()){
774  throw new UnsupportedOperationException();
775  }
776  return store.remove(o);
777  }
778 
779  /* (non-Javadoc)
780  * @see java.util.List#remove(int)
781  */
782 
789  public synchronized Byte remove(int index) {
790  if(isUseStaticBuffer()){
794  }
795  return b;
796  }
797 
798  return store.remove(index);
799  }
800 
801  /* (non-Javadoc)
802  * @see java.util.List#subList(int, int)
803  */
804 
812  public List<Byte> subList(int fromIndex, int toIndex) {
813  if(isUseStaticBuffer()){
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]);
818  }
819  return back;
820  }
821  return store.subList(fromIndex, toIndex);
822  }
823 
824  /* (non-Javadoc)
825  * @see java.util.List#toArray()
826  */
827 
833  public Object[] toArray() {
834  if(isUseStaticBuffer()){
835  throw new UnsupportedOperationException();
836  }
837  return store.toArray();
838  }
839 
840  /* (non-Javadoc)
841  * @see java.lang.Object#toString()
842  */
843  public String toString() {
844  if(size() == 0) {
845  return "";
846  }
847  String rtn = "";
848  for(byte x : getBytes()){
849  rtn += String.format("%02x ", x);
850  }
851  rtn = rtn.substring(0, rtn.length()-1);
852  return rtn.toUpperCase();
853  }
854 
860  public String asString() {
861  byte [] data = getBytes();
862  if(data.length == 0) {
863  return "";
864  }
865  String s="";
866 
867  int i=0;
868  while(i<data.length ){
869  if(data[i] != 0)
870  s+=(char)data[i++];
871  else
872  return s;
873  }
874  return s;
875  }
876 
884  public static byte[] split(long data, int split) {
885  if(split < 1) {
886  byte[] b = {};
887  return b;
888  }
889 
890  if(split > 8) {
891  split = 8;
892  }
893 
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);
899 
900  System.arraycopy(bArray, 8-split, rtn, 0, split);
901 
902  return rtn;
903  }
904 
911  public static final int convertToInt(byte[] b)
912  {
913  int i=0;
914  i=(int) convertToInt(b,false);
915  return i;
916  }
917 
928  public static int convertToInt(byte[] b,boolean Signed){
929 
930  long bytes = b.length;
931  if(bytes>4)
932  throw new RuntimeException("Ints can only have 2 or 4 bytes");
933  long out = 0;
934  long tmp = 0;
935  for (int i=0;i<bytes;i++){
936  tmp = rawByteToInt(b[i]);
937  int power =(int) ((bytes-i-1)*8);
938  long val = (long) (tmp * Math.pow(2, power ));
939  out+=val;
940  }
941  if (Signed){
942  // This converts to a signed long
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);
946  long abs_val=0;
947  if (out>max_pos_val){
948  abs_val=(sub_val-out);
949  out=(int) (-1*abs_val);
950  }
951  }
952  int ret = (int) out;
953  return ret;
954  }
955 
962  public static byte[] convertTo16(int value) {
963  byte b [] = new byte[2];
964 
965  b[0] = (byte)(value >> 8);
966  b[1] = (byte)(value);
967 
968  return b;
969  }
970 
977  public static byte[] convertTo32(int value) {
978  byte b [] = new byte[4];
979 
980  b[0] = (byte)(value >> 24);
981  b[1] = (byte)(value >> 16);
982  b[2] = (byte)(value >> 8);
983  b[3] = (byte)(value);
984 
985  return b;
986  }
987 
988  /* (non-Javadoc)
989  * @see java.util.List#add(int, java.lang.Object)
990  */
991 
998  public void add(int arg0, Byte arg1) {
999  throw new UnsupportedOperationException();
1000  }
1001 
1002  /* (non-Javadoc)
1003  * @see java.util.List#removeAll(java.util.Collection)
1004  */
1005 
1012  public boolean removeAll(Collection<?> c) {
1013  throw new UnsupportedOperationException();
1014  }
1015 
1016  /* (non-Javadoc)
1017  * @see java.util.List#retainAll(java.util.Collection)
1018  */
1019 
1026  public boolean retainAll(Collection<?> c) {
1027  throw new UnsupportedOperationException();
1028  }
1029 
1030  /* (non-Javadoc)
1031  * @see java.util.List#set(int, java.lang.Object)
1032  */
1033 
1041  public Byte set(int arg0, Byte arg1) {
1042  throw new UnsupportedOperationException();
1043  }
1044 
1051  public void insert(int index,byte val){
1052  if(isUseStaticBuffer()){
1053  byte [] current = getBytes();
1054  clear();
1055  for(int i=0;i<current.length;i++){
1056  if(i==index){
1057  add(val);
1058  }
1059  add(current[i]);
1060  }
1061  }else{
1062  store.add(index, val);
1063  }
1064  }
1065 
1072  public static int rawByteToInt(byte b){
1073  int tmp =(int)b;
1074  if (tmp < 0){
1075  // This solves the Java signedness problem of "bytes"
1076  tmp +=256;
1077  }
1078  return tmp;
1079  }
1080 
1088  public byte[] popList(int off, int len) {
1089  if (size() >= off+len) {
1090  byte [] ret = new byte[len];
1091  for(int i=0;i<len;i++) {
1092  ret[i]=remove(off);
1093  }
1094  return ret;
1095  }
1096  throw new IndexOutOfBoundsException();
1097  }
1098 
1104  public static boolean isUseStaticBuffer() {
1105  return useStaticBuffer;
1106  }
1107 
1113  public static void setUseStaticBuffer(boolean useStaticBuffer) {
1114  //ByteList.useStaticBuffer =true;
1115  }
1116 
1122  public int getStaticBufferSize() {
1123  return staticBufferSize;
1124  }
1125 
1132  this.staticBufferSize = staticBufferSize;
1133  staticBuffer = new byte[getStaticBufferSize()];
1136  store = null;
1137  }
1138 }
boolean add(ISendable sendable)
Definition: ByteList.java:207
ListIterator< Byte > listIterator(int index)
Definition: ByteList.java:755
void setStaticBufferSize(int staticBufferSize)
Definition: ByteList.java:1131
boolean add(byte[] data, int len, int offset)
Definition: ByteList.java:310
void add(int arg0, Byte arg1)
Definition: ByteList.java:998
synchronized boolean add(byte data)
Definition: ByteList.java:149
static byte[] split(long data, int split)
Definition: ByteList.java:884
static void setUseStaticBuffer(boolean useStaticBuffer)
Definition: ByteList.java:1113
void insert(int index, byte val)
Definition: ByteList.java:1051
ByteList(BowlerDataType[] arguments)
Definition: ByteList.java:120
static int convertToInt(byte[] b, boolean Signed)
Definition: ByteList.java:928
byte[] popList(int off, int len)
Definition: ByteList.java:1088
boolean removeAll(Collection<?> c)
Definition: ByteList.java:1012
static final int convertToInt(byte[] b)
Definition: ByteList.java:911
boolean containsAll(Collection<?> c)
Definition: ByteList.java:622
boolean addAll(Collection<? extends Byte > c)
Definition: ByteList.java:346
boolean add(long data, int split)
Definition: ByteList.java:296
ListIterator< Byte > listIterator()
Definition: ByteList.java:738
boolean addAll(int index, Collection<? extends Byte > c)
Definition: ByteList.java:362
static byte[] wrap(int value)
Definition: ByteList.java:554
List< Byte > subList(int fromIndex, int toIndex)
Definition: ByteList.java:812
static final boolean useStaticBuffer
Definition: ByteList.java:35
boolean retainAll(Collection<?> c)
Definition: ByteList.java:1026
static byte[] convertTo16(int value)
Definition: ByteList.java:962
static byte[] convertTo32(int value)
Definition: ByteList.java:977
synchronized byte[] getBytes(int start, int len)
Definition: ByteList.java:394
static void info(String message)
Definition: Log.java:110
static void error(String message)
Definition: Log.java:92