Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

131
LINES

< > BotCompany Repo | #1030688 // ByteBuffer

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (5659L) is out of date.

sclass ByteBuffer implements Iterable<Byte> {
  byte[] data;
  int size;
  
  *() {}
  *(int size) { if (size != 0) data = new byte[size]; }
  *(Iterable<Byte> l) {
    if (l cast Cl) allocate(l.size());
    addAll(l);
  }
  *(byte[] data) { this.data = data; size = l(data); }
  // TODO *(ByteBuffer buf) { ... }
  
  void add(int idx, int i) {
    add(0);
    arraycopy(data, idx, data, idx+1, size-(idx+1));
    data[idx] = (byte) i;
  }
  
  void add(int i) { add((byte) i); }
  
  void add(byte i) {
    if (size >= lByteArray(data)) {
      allocate(Math.max(1, toInt(Math.min(maximumSafeArraySize(), lByteArray(data)*2L))));
      if (size >= data.length) fail("ByteBuffer too large: " + size);
    }
    data[size++] = i;
  }
  
  void allocate(int n) {
    data = resizeByteArray(data, max(n, size()));
  }
  
  void addAll(Iterable<Byte> l) {
    if (l != null) for (byte i : l) add(i);
  }
  
  byte[] toArray aka toByteArray() {
    ret size == 0 ? null : resizeByteArray(data, size);
  }
  
  L<Byte> toList() {
    ret byteArrayToList(data, 0, size);
  }

  L<Byte> asVirtualList() {
    ret new RandomAccessAbstractList<Byte> {
      public int size() { ret size; }
      public Byte get(int i) { ret ByteBuffer.this.get(i); }
      public Byte set(int i, Byte val) {
        Byte a = get(i);
        data[i] = val;
        ret a;
      }
    };
  }
  
  void reset { size = 0; }
  void clear { reset(); }
  
  int size() { ret size; }
  bool isEmpty() { ret size == 0; }
  
  byte get(int idx) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    ret data[idx];
  }
  
  void set(int idx, byte value) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    data[idx] = value;
  }
  
  byte popLast() {
    if (size == 0) fail("empty buffer");
    ret data[--size];
  }
  
  // unefficient
  byte popFirst() {
    if (size == 0) fail("empty buffer");
    byte b = data[0];
    arraycopy(data, 1, 0, --size);
    ret b;
  }
  
  byte last() { ret data[size-1]; }
  byte nextToLast() { ret data[size-2]; }
  
  toString { ret squareBracket(joinWithSpace(toList())); }
  
  public Iterator<Byte> iterator() {
    ret new ItIt<Byte> {
      int i = 0;
      
      public bool hasNext() { ret i < size; }
      public Byte next() {
        //if (!hasNext()) fail("Index out of bounds: " + i);
        ret data[i++];
      }
    };
  }
  
  /*public ByteIterator byteIterator() {
    ret new ByteIterator {
      int i = 0;
      
      public bool hasNext() { ret i < size; }
      public int next() {
        //if (!hasNext()) fail("Index out of bounds: " + i);
        ret data[i++];
      }
      toString { ret "Iterator@" + i + " over " + ByteBuffer.this; }
    };
  }*/
  
  void trimToSize {
    data = resizeByteArray(data, size);
  }
  
  int indexOf(byte b) {
    for i to size:
      if (data[i] == b)
        ret i;
    ret -1;
  }
  
  byte[] subArray(int start, int end) {
    ret subByteArray(data, start, min(end, size));
  }
}

Author comment

Began life as a copy of #1028793

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, ekrmjmnbrukm, elmgxqgtpvxh, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt, wnsclhtenguj

No comments. add comment

Snippet ID: #1030688
Snippet name: ByteBuffer
Eternal ID of this version: #1030688/18
Text MD5: 00a27dde7e31f7a7b7696d1659ac8148
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-06 20:12:07
Source code size: 3127 bytes / 131 lines
Pitched / IR pitched: No / No
Views / Downloads: 254 / 592
Version history: 17 change(s)
Referenced in: [show references]