diff options
author | mikaelpeltier <mikaelpeltier@google.com> | 2015-06-24 14:31:11 +0200 |
---|---|---|
committer | Mikael Peltier <mikaelpeltier@google.com> | 2015-06-24 14:59:36 +0000 |
commit | 04563874ddaac702d6c715eaa89c29b253f4c54e (patch) | |
tree | c305fa98670c3e80be494cc054a8e31b51bfe7f2 /simple/simple-common/src/test/java/org/simpleframework/common/buffer/queue/ArrayByteQueue.java | |
parent | f1828481ebcfee3bddc323fca178a4502a60ceef (diff) | |
download | toolchain_jack-04563874ddaac702d6c715eaa89c29b253f4c54e.zip toolchain_jack-04563874ddaac702d6c715eaa89c29b253f4c54e.tar.gz toolchain_jack-04563874ddaac702d6c715eaa89c29b253f4c54e.tar.bz2 |
Add simpleframework source files
Change-Id: I18d01df16de2868ca5458f79a88e6070b75db2c3
(cherry picked from commit 3e9f84cf7b22f6970eb8041ca38d12d75c6bb270)
Diffstat (limited to 'simple/simple-common/src/test/java/org/simpleframework/common/buffer/queue/ArrayByteQueue.java')
-rw-r--r-- | simple/simple-common/src/test/java/org/simpleframework/common/buffer/queue/ArrayByteQueue.java | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/simple/simple-common/src/test/java/org/simpleframework/common/buffer/queue/ArrayByteQueue.java b/simple/simple-common/src/test/java/org/simpleframework/common/buffer/queue/ArrayByteQueue.java new file mode 100644 index 0000000..893ae80 --- /dev/null +++ b/simple/simple-common/src/test/java/org/simpleframework/common/buffer/queue/ArrayByteQueue.java @@ -0,0 +1,100 @@ +package org.simpleframework.common.buffer.queue; + +import java.io.IOException; + +import org.simpleframework.common.buffer.BufferException; + +public class ArrayByteQueue implements ByteQueue { + + private byte[] buffer; + private int limit; + private int count; + private int seek; + private boolean closed; + + public ArrayByteQueue(int limit) { + this.buffer = new byte[16]; + this.limit = limit; + } + + public synchronized void write(byte[] array) throws IOException { + write(array, 0, array.length); + } + + public synchronized void write(byte[] array, int off, int size) throws IOException { + if(closed) { + throw new BufferException("Queue has been closed"); + } + if (size + count > buffer.length) { + expand(count + size); + } + int fragment = buffer.length - seek; // from read pos to end + int space = fragment - count; // space at end + + if(space >= size) { + System.arraycopy(array, off, buffer, seek + count, size); + } else { + int chunk = Math.min(fragment, count); + + System.arraycopy(buffer, seek, buffer, 0, chunk); // adjust downward + System.arraycopy(array, off, buffer, chunk, size); + seek = 0; + } + notify(); + count += size; + } + + public synchronized int read(byte[] array) throws IOException { + return read(array, 0, array.length); + } + + public synchronized int read(byte[] array, int off, int size) throws IOException { + while(count == 0) { + try { + if(closed) { + return -1; + } + wait(); + } catch(Exception e) { + throw new BufferException("Thread interrupted", e); + } + } + int chunk = Math.min(size, count); + + if(chunk > 0) { + System.arraycopy(buffer, seek, array, off, chunk); + seek += chunk; + count -= chunk; + } + return chunk; + } + + private synchronized void expand(int capacity) throws IOException { + if (capacity > limit) { + throw new BufferException("Capacity limit %s exceeded", limit); + } + int resize = buffer.length * 2; + int size = Math.max(capacity, resize); + byte[] temp = new byte[size]; + + System.arraycopy(buffer, seek, temp, 0, count); + buffer = temp; + seek = 0; + } + + public synchronized void reset() throws IOException { + if(closed) { + throw new BufferException("Queue has been closed"); + } + seek = 0; + count = 0; + } + + public synchronized int available() { + return count; + } + + public synchronized void close() { + closed = true; + } +} |