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

39
LINES

< > BotCompany Repo | #1026626 // CircularByteBuffer

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

Libraryless. Click here for Pure Java version (2163L/14K).

1  
sclass CircularByteBuffer {
2  
  byte[] buffer;
3  
  long base; // elements dropped due to overcapacity
4  
  int size;  // elements actually contained
5  
  
6  
  *(int capacity) {
7  
    buffer = new byte[capacity];
8  
  }
9  
  
10  
  synchronized void add(byte a) {
11  
    if (size == buffer.length) {
12  
      --size;
13  
      ++base;
14  
    }
15  
    buffer[(int) ((base+size) % buffer.length)] = a;
16  
    ++size;
17  
  }
18  
  
19  
  synchronized byte get(long pos) {
20  
    if (pos < base || pos >= base+size) ret 0;
21  
    ret buffer[(int) (pos % buffer.length)];
22  
  }
23  
  
24  
  synchronized int size() {
25  
    ret size;
26  
  }
27  
  
28  
  synchronized int capacity() {
29  
    ret buffer.length;
30  
  }
31  
  
32  
  synchronized bool isFull() {
33  
    ret size() == capacity();
34  
  }
35  
  
36  
  synchronized long getBase() {
37  
    ret base;
38  
  }
39  
}

Author comment

Began life as a copy of #1025789

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026626
Snippet name: CircularByteBuffer
Eternal ID of this version: #1026626/3
Text MD5: a669da170f312bb6e55c7824cfec4acf
Transpilation MD5: 9dc55d6b8f257e5513a88fe8c493972e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-16 19:42:45
Source code size: 781 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 196 / 539
Version history: 2 change(s)
Referenced in: [show references]