mirror of
https://github.com/faye/websocket-driver-ruby.git
synced 2025-11-01 13:59:38 +00:00
56 lines
1.3 KiB
Java
56 lines
1.3 KiB
Java
package com.jcoglan.websocket;
|
|
|
|
import java.util.LinkedList;
|
|
import java.util.Queue;
|
|
|
|
class StreamReader {
|
|
static int MAX_CAPACITY = 0xfffffff;
|
|
|
|
private Queue<byte[]> queue;
|
|
private int capacity;
|
|
private int cursor;
|
|
|
|
StreamReader() {
|
|
queue = new LinkedList<byte[]>();
|
|
capacity = 0;
|
|
cursor = 0;
|
|
}
|
|
|
|
boolean push(byte[] chunk) {
|
|
if (chunk.length > MAX_CAPACITY - capacity) return false;
|
|
|
|
queue.add(chunk);
|
|
capacity += chunk.length;
|
|
|
|
return true;
|
|
}
|
|
|
|
byte[] read(int length) {
|
|
if (capacity < length) return null;
|
|
|
|
byte[] target = new byte[length];
|
|
int offset = 0;
|
|
|
|
while (offset < length) {
|
|
byte[] chunk = queue.peek();
|
|
|
|
int available = chunk.length - cursor;
|
|
int required = length - offset;
|
|
int takeBytes = (available < required) ? available : required;
|
|
|
|
System.arraycopy(chunk, cursor, target, offset, takeBytes);
|
|
offset += takeBytes;
|
|
|
|
if (takeBytes == available ) {
|
|
cursor = 0;
|
|
queue.remove();
|
|
} else {
|
|
cursor += takeBytes;
|
|
}
|
|
}
|
|
|
|
capacity -= length;
|
|
return target;
|
|
}
|
|
}
|