-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
core/src/main/java/com/turn/ttorrent/common/ByteBufferRentalService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |