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

107
LINES

< > BotCompany Repo | #1030688 - ByteBuffer

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

Libraryless. Click here for Pure Java version (4661L/26K).

sclass ByteBuffer implements Iterable<Byte> {
  byte[] data;
  int size;
  
  *() {}
  *(int size) { if (size != 0) data = new byte[size]; }
  *(Iterable<Byte> l) { this(l(l)); addAll(l); }
  *(byte[] data) { this.data = data; size = l(data); }
  
  void add(byte i) {
    if (size >= lByteArray(data)) {
      data = resizeByteArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lByteArray(data)*2L))));
      if (size >= data.length) fail("ByteBuffer too large: " + size);
    }
    data[size++] = i;
  }
  
  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];
  }
  
  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 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030688
Snippet name: ByteBuffer
Eternal ID of this version: #1030688/12
Text MD5: 3e6d7673f68401418925bc659c2b89d4
Transpilation MD5: a676a1a2f92141c134133d1582ed3fe8
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-14 04:17:24
Source code size: 2652 bytes / 107 lines
Pitched / IR pitched: No / No
Views / Downloads: 82 / 231
Version history: 11 change(s)
Referenced in: [show references]