summaryrefslogtreecommitdiffstats
path: root/simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileByteQueue.java
diff options
context:
space:
mode:
authormikaelpeltier <mikaelpeltier@google.com>2015-06-24 14:31:11 +0200
committerMikael Peltier <mikaelpeltier@google.com>2015-06-24 14:59:36 +0000
commit04563874ddaac702d6c715eaa89c29b253f4c54e (patch)
treec305fa98670c3e80be494cc054a8e31b51bfe7f2 /simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileByteQueue.java
parentf1828481ebcfee3bddc323fca178a4502a60ceef (diff)
downloadtoolchain_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/FileByteQueue.java')
-rw-r--r--simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileByteQueue.java109
1 files changed, 109 insertions, 0 deletions
diff --git a/simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileByteQueue.java b/simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileByteQueue.java
new file mode 100644
index 0000000..b404562
--- /dev/null
+++ b/simple/simple-common/src/test/java/org/simpleframework/common/buffer/FileByteQueue.java
@@ -0,0 +1,109 @@
+package org.simpleframework.common.buffer;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import org.simpleframework.common.buffer.Allocator;
+import org.simpleframework.common.buffer.Buffer;
+import org.simpleframework.common.buffer.BufferAllocator;
+
+public class FileByteQueue {
+
+ private BlockingQueue<Block> blocks;
+ private BlockAllocator allocator;
+ private Block source;
+
+ public FileByteQueue(Allocator allocator) throws IOException {
+ this.blocks = new LinkedBlockingQueue<Block>();
+ this.allocator = new BlockAllocator(allocator);
+ }
+
+ public int read(byte[] array, int off, int size) throws Exception {
+ int left = blocks.size();
+ int mark = size;
+
+ for(int i = 0; source != null || i < left; i++) {
+ if(source == null) {
+ source = blocks.take();
+ }
+ int remain = source.remaining();
+ int read = Math.min(remain, size);
+
+ if(read > 0) {
+ source.read(array, off, size);
+ size -= read;
+ off += read;
+ }
+ if(remain == 0) {
+ source.close(); // clear up file handles
+ source = null;
+ }
+ if(size <= 0) {
+ return mark;
+ }
+ }
+ return mark - size;
+ }
+
+ public void write(byte[] array, int off, int size) throws Exception {
+ Block buffer = allocator.allocate(array, off, size);
+
+ if(size > 0) {
+ blocks.offer(buffer);
+ }
+ }
+
+ private class BlockAllocator {
+
+ private Allocator allocator;
+
+ public BlockAllocator(Allocator allocator) {
+ this.allocator = new BufferAllocator(allocator);
+ }
+
+ public Block allocate(byte[] array, int off, int size) throws IOException {
+ Buffer buffer = allocator.allocate();
+
+ if(size > 0) {
+ buffer.append(array, off, size);
+ }
+ return new Block(buffer, size);
+ }
+ }
+
+ private class Block {
+
+ private InputStream source;
+ private int remaining;
+ private int size;
+
+ public Block(Buffer buffer, int size) throws IOException {
+ this.source = buffer.open();
+ this.remaining = size;
+ this.size = size;
+ }
+
+ public int read(byte[] array, int off, int size) throws IOException {
+ int count = source.read(array, off, size);
+
+ if(count > 0) {
+ remaining -= size;
+ }
+ return count;
+ }
+
+ public void close() throws IOException {
+ source.close();
+ }
+
+ public int remaining() {
+ return remaining;
+ }
+
+ public int size() {
+ return size;
+ }
+ }
+} \ No newline at end of file