sclass ByteBuffer implements Iterable { byte[] data; int size; *() {} *(int size) { if (size != 0) data = new byte[size]; } *(Iterable 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 l) { if (l != null) for (byte i : l) add(i); } byte[] toArray aka toByteArray() { ret size == 0 ? null : resizeByteArray(data, size); } L toList() { ret byteArrayToList(data, 0, size); } L asVirtualList() { ret new RandomAccessAbstractList { 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 iterator() { ret new ItIt { 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)); } }