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.

1  
sclass ByteBuffer implements Iterable<Byte> {
2  
  byte[] data;
3  
  int size;
4  
  
5  
  *() {}
6  
  *(int size) { if (size != 0) data = new byte[size]; }
7  
  *(Iterable<Byte> l) {
8  
    if (l cast Cl) allocate(l.size());
9  
    addAll(l);
10  
  }
11  
  *(byte[] data) { this.data = data; size = l(data); }
12  
  // TODO *(ByteBuffer buf) { ... }
13  
  
14  
  void add(int idx, int i) {
15  
    add(0);
16  
    arraycopy(data, idx, data, idx+1, size-(idx+1));
17  
    data[idx] = (byte) i;
18  
  }
19  
  
20  
  void add(int i) { add((byte) i); }
21  
  
22  
  void add(byte i) {
23  
    if (size >= lByteArray(data)) {
24  
      allocate(Math.max(1, toInt(Math.min(maximumSafeArraySize(), lByteArray(data)*2L))));
25  
      if (size >= data.length) fail("ByteBuffer too large: " + size);
26  
    }
27  
    data[size++] = i;
28  
  }
29  
  
30  
  void allocate(int n) {
31  
    data = resizeByteArray(data, max(n, size()));
32  
  }
33  
  
34  
  void addAll(Iterable<Byte> l) {
35  
    if (l != null) for (byte i : l) add(i);
36  
  }
37  
  
38  
  byte[] toArray aka toByteArray() {
39  
    ret size == 0 ? null : resizeByteArray(data, size);
40  
  }
41  
  
42  
  L<Byte> toList() {
43  
    ret byteArrayToList(data, 0, size);
44  
  }
45  
46  
  L<Byte> asVirtualList() {
47  
    ret new RandomAccessAbstractList<Byte> {
48  
      public int size() { ret size; }
49  
      public Byte get(int i) { ret ByteBuffer.this.get(i); }
50  
      public Byte set(int i, Byte val) {
51  
        Byte a = get(i);
52  
        data[i] = val;
53  
        ret a;
54  
      }
55  
    };
56  
  }
57  
  
58  
  void reset { size = 0; }
59  
  void clear { reset(); }
60  
  
61  
  int size() { ret size; }
62  
  bool isEmpty() { ret size == 0; }
63  
  
64  
  byte get(int idx) {
65  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
66  
    ret data[idx];
67  
  }
68  
  
69  
  void set(int idx, byte value) {
70  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
71  
    data[idx] = value;
72  
  }
73  
  
74  
  byte popLast() {
75  
    if (size == 0) fail("empty buffer");
76  
    ret data[--size];
77  
  }
78  
  
79  
  // unefficient
80  
  byte popFirst() {
81  
    if (size == 0) fail("empty buffer");
82  
    byte b = data[0];
83  
    arraycopy(data, 1, 0, --size);
84  
    ret b;
85  
  }
86  
  
87  
  byte last() { ret data[size-1]; }
88  
  byte nextToLast() { ret data[size-2]; }
89  
  
90  
  toString { ret squareBracket(joinWithSpace(toList())); }
91  
  
92  
  public Iterator<Byte> iterator() {
93  
    ret new ItIt<Byte> {
94  
      int i = 0;
95  
      
96  
      public bool hasNext() { ret i < size; }
97  
      public Byte next() {
98  
        //if (!hasNext()) fail("Index out of bounds: " + i);
99  
        ret data[i++];
100  
      }
101  
    };
102  
  }
103  
  
104  
  /*public ByteIterator byteIterator() {
105  
    ret new ByteIterator {
106  
      int i = 0;
107  
      
108  
      public bool hasNext() { ret i < size; }
109  
      public int next() {
110  
        //if (!hasNext()) fail("Index out of bounds: " + i);
111  
        ret data[i++];
112  
      }
113  
      toString { ret "Iterator@" + i + " over " + ByteBuffer.this; }
114  
    };
115  
  }*/
116  
  
117  
  void trimToSize {
118  
    data = resizeByteArray(data, size);
119  
  }
120  
  
121  
  int indexOf(byte b) {
122  
    for i to size:
123  
      if (data[i] == b)
124  
        ret i;
125  
    ret -1;
126  
  }
127  
  
128  
  byte[] subArray(int start, int end) {
129  
    ret subByteArray(data, start, min(end, size));
130  
  }
131  
}

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: 264 / 610
Version history: 17 change(s)
Referenced in: [show references]