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).

sclass CircularByteBuffer {
  byte[] buffer;
  long base; // elements dropped due to overcapacity
  int size;  // elements actually contained
  
  *(int capacity) {
    buffer = new byte[capacity];
  }
  
  synchronized void add(byte a) {
    if (size == buffer.length) {
      --size;
      ++base;
    }
    buffer[(int) ((base+size) % buffer.length)] = a;
    ++size;
  }
  
  synchronized byte get(long pos) {
    if (pos < base || pos >= base+size) ret 0;
    ret buffer[(int) (pos % buffer.length)];
  }
  
  synchronized int size() {
    ret size;
  }
  
  synchronized int capacity() {
    ret buffer.length;
  }
  
  synchronized bool isFull() {
    ret size() == capacity();
  }
  
  synchronized long getBase() {
    ret base;
  }
}

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: 197 / 540
Version history: 2 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)
#1036465 - SynchronizedCircularBuffer - duplicate of SimpleCircularBuffer