Advertisement
T99

ByteList.java

T99
Apr 9th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.34 KB | None | 0 0
  1. package io.t99.caffeinesocket.util;
  2.  
  3. /*
  4.  *  Copyright 2017, Trevor Sears <trevorsears.main@gmail.com>
  5.  *
  6.  *  Licensed under the Apache License, Version 2.0 (the "License");
  7.  *  you may not use this file except in compliance with the License.
  8.  *  You may obtain a copy of the License at
  9.  *
  10.  *      http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  *  Unless required by applicable law or agreed to in writing, software
  13.  *  distributed under the License is distributed on an "AS IS" BASIS,
  14.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  *  See the License for the specific language governing permissions and
  16.  *  limitations under the License.
  17.  */
  18.  
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21.  
  22. public class ByteList implements Iterable<Byte> {
  23.  
  24.     private byte[] array;
  25.    
  26.     /**
  27.      * Acts as the 'cursor' in the array, indicating where the next value should be placed. This therefore means that
  28.      * `index` also always points to the first non-real value in the array, as while all indices are initialized to
  29.      * zero, we know that any zero values that have an index equal to or greater than this value are non-real.
  30.      */
  31.     private int index = 0;
  32.     private int growthStepSize;
  33.    
  34.     public ByteList() {
  35.        
  36.         this(10, 1);
  37.        
  38.     }
  39.    
  40.     public ByteList(int size) {
  41.        
  42.         this(size, 1);
  43.        
  44.     }
  45.    
  46.     public ByteList(int size, int growthStepSize) {
  47.        
  48.         if (size <= 0) throw new IllegalArgumentException("Illegal ByteList size: " + size);
  49.         array = new byte[size];
  50.         this.growthStepSize = growthStepSize;
  51.        
  52.     }
  53.    
  54.     public ByteList(ByteList byteList, int size, int growthStepSize) throws IndexOutOfBoundsException {
  55.        
  56.         this(size, growthStepSize);
  57.        
  58.         if (size < byteList.size()) throw new IndexOutOfBoundsException("Attempted to insert a ByteList of size " + byteList.size() + " into a new ByteList of size " + size);
  59.        
  60.         this.add(byteList);
  61.        
  62.     }
  63.    
  64.     public ByteList(ByteList byteList, int i1, int i2, int growthStepSize) {
  65.        
  66.         this(i2-i1, growthStepSize);
  67.        
  68.         for (int i = i1; i < i2; i++) {
  69.            
  70.             this.add(byteList.get(i));
  71.            
  72.         }
  73.        
  74.     }
  75.    
  76.     public byte get(int index) {
  77.        
  78.         return array[index];
  79.        
  80.     }
  81.    
  82.     public boolean getBit(int bit) {
  83.        
  84.         return NumberBaseConverter.signedByteToBinary(get(bit/8))[bit%8];
  85.        
  86.     }
  87.    
  88.     public boolean getBit(int b, int bit) {
  89.  
  90.         return NumberBaseConverter.signedByteToBinary(get(b))[bit];
  91.  
  92.     }
  93.    
  94.     public boolean[] getBits(int i1, int i2) {
  95.        
  96.         boolean[] bits = new boolean[i2 - i1];
  97.        
  98.         for (int i = i1; i < i2; i++) {
  99.            
  100.             bits[i - i1] = getBit(i);
  101.            
  102.         }
  103.        
  104.         return bits;
  105.        
  106.     }
  107.    
  108.     public synchronized void resizeToLength() {
  109.    
  110.         // TODO - Write `resizeToLength()` method.
  111.    
  112.     }
  113.    
  114.     private synchronized void resizeUp() {
  115.        
  116.         // Store a copy of the array in `copy`.
  117.         byte[] copy = array;
  118.        
  119.         // Reset `array` to an array of it's previous size, plus the growth step size.
  120.         array = new byte[copy.length + growthStepSize];
  121.        
  122.         // Copy the previous elements of `array` (currently stored in `copy`) to the new `array` array.
  123.         System.arraycopy(copy, 0, array, 0, copy.length);
  124.        
  125.     }
  126.    
  127.     private synchronized void resizeDown() {
  128.        
  129.         // Store a copy of the array in `copy`.
  130.         byte[] copy = array;
  131.        
  132.         // Reset `array` to an array of it's previous size, minus the growth step size.
  133.         array = new byte[copy.length - growthStepSize];
  134.        
  135.         // Copy the previous elements of `array` (currently stored in `copy`) to the new `array` array.
  136.         System.arraycopy(copy, 0, array, 0, copy.length - growthStepSize);
  137.        
  138.     }
  139.    
  140.     public synchronized void add(byte b) {
  141.        
  142.         // If the current array is not large enough for the new element, call `resizeUp()`.
  143.         if ((index + 1) > array.length) resizeUp();
  144.        
  145.         // Store the new element at the active index of the array.
  146.         array[index] = b;
  147.        
  148.         // Move the index to the new appropriate position.
  149.         index++;
  150.    
  151.     }
  152.    
  153.     public synchronized void add(ByteList b) {
  154.        
  155.         // If the current array is not large enough for the new elements, call `resizeUp()`.
  156.         if ((index + b.size()) > array.length) resizeUp();
  157.        
  158.         // Store the new elements at the active index of the array.
  159.         for (int index = 0; index < b.size(); index++) {
  160.            
  161.             // Store the nth element of b at the this.nth index of the array.
  162.             array[this.index] = b.get(index);
  163.            
  164.             // Move the index to the new appropriate position.
  165.             this.index++;
  166.        
  167.         }
  168.        
  169.     }
  170.    
  171.     public synchronized byte remove(int index) throws IndexOutOfBoundsException {
  172.        
  173.         if (index >= this.index) throw new IndexOutOfBoundsException("Attempted to remove index " + index + ", which is beyond the greatest index of " + (this.index - 1) + ".");
  174.        
  175.         byte returned = array[index];
  176.         byte[] copy = new byte[this.index - (index + 1)];
  177.         System.arraycopy(array, index + 1, copy, 0, copy.length);
  178.         this.index--;
  179.         System.arraycopy(copy, 0, array, index, copy.length);
  180.        
  181.         if (this.index <= (array.length - growthStepSize)) resizeDown();
  182.        
  183.         return returned;
  184.        
  185.     }
  186.    
  187.     public synchronized void insert(int index, byte b) {
  188.    
  189.    
  190.    
  191.     }
  192.    
  193.     public int size() {
  194.        
  195.         return index;
  196.        
  197.     }
  198.    
  199.     public String getDebugInfo() {
  200.        
  201.         String info = "";
  202.        
  203.         info += "array:\t\t\t\t"        + hrArray(array)            + System.lineSeparator();
  204.         info += "reported size:\t\t"    + size()                    + System.lineSeparator();
  205.         info += "array.length:\t\t"     + array.length              + System.lineSeparator();
  206.         info += "unused indicies:\t"    + (array.length - size())   + System.lineSeparator();
  207.         info += "growth step:\t\t"      + growthStepSize;
  208.        
  209.         return info;
  210.        
  211.     }
  212.    
  213.     public String getArray() {
  214.        
  215.         return hrArray(this.array);
  216.        
  217.     }
  218.    
  219.     public static String hrArray(byte[] bytes) {
  220.        
  221.         String s = "[";
  222.        
  223.         for (int i = 0; i < bytes.length - 1; i++) {
  224.            
  225.             s += hrByte(bytes[i]) + ", ";
  226.            
  227.         }
  228.        
  229.         s += hrByte(bytes[bytes.length - 1]) + "]";
  230.        
  231.         return s;
  232.        
  233.     }
  234.    
  235.     public static int hrByte(byte b) {
  236.        
  237.         if (b < 0) {
  238.            
  239.             return -(-128 - (b + 128));
  240.            
  241.         } else {
  242.            
  243.             return b;
  244.            
  245.         }
  246.        
  247.     }
  248.    
  249.     @Override
  250.     public Iterator<Byte> iterator() { // This is nasty and probably grossly inefficient. If only Java supported primitively typed generics.
  251.        
  252.         ArrayList<Byte> arrayList = new ArrayList<>();
  253.        
  254.         byte[] copy = new byte[index];
  255.         System.arraycopy(array, 0, copy, 0, index);
  256.        
  257.         for (byte b: copy) {
  258.            
  259.             arrayList.add(b);
  260.            
  261.         }
  262.        
  263.         return arrayList.iterator();
  264.        
  265.     }
  266.    
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement