Skip to content

Commit

Permalink
Added ByteBufferRentalService class
Browse files Browse the repository at this point in the history
  • Loading branch information
zanella committed Nov 16, 2016
1 parent 0acd62d commit 9cc01de
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.turn.ttorrent.common;

import java.nio.ByteBuffer;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
* A limited, exclusive storage, so that the workers are limited to it's amount.
*
* The ByteBuffers are array backed, so the APIs they get sent to have no need to instantiate one
*
* "rental service": exemplifies well the intent, shitty name :-)
*
*/
public class ByteBufferRentalService {
private BlockingQueue<ByteBuffer> byteBufferBlockingQueue;

/**
* Initializes a bounded storage
*
* @param amount the amount of byte buffers to create
* @param length the length of the created buffers
*/
public ByteBufferRentalService(int amount, int length) {
byteBufferBlockingQueue = new LinkedBlockingQueue<ByteBuffer>(amount);

for (int i = 0; i < amount; i++) {
byteBufferBlockingQueue.add( ByteBuffer.allocate(length) );
}
}

public ByteBuffer take() throws InterruptedException {
return byteBufferBlockingQueue.take();
}

public void put(ByteBuffer buffer) throws InterruptedException {
buffer.clear();

byteBufferBlockingQueue.put( buffer );
}
}

0 comments on commit 9cc01de

Please sign in to comment.