use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class MessageInputStreamTest method testGetHighestBlockId.
@Test
public void testGetHighestBlockId() {
assertThat(in.getHighestBlockId(), is((long) -1));
in.messageReceived(0, new ByteArray());
assertThat(in.getHighestBlockId(), is((long) 0));
in.messageReceived(2, new ByteArray());
assertThat(in.getHighestBlockId(), is((long) 2));
in.messageReceived(1, new ByteArray());
assertThat(in.getHighestBlockId(), is((long) 2));
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class MessageInputStreamTest method testCanAccept_smallerMsgsWithMaxSizeCount.
@Test
public void testCanAccept_smallerMsgsWithMaxSizeCount() {
// Fill the buffer to one message under count that would reach limit with max-size msgs
int numMsgs = _options.getInboundBufferSize() / _options.getMaxMessageSize();
byte[] orig = new byte[numMsgs * 1024];
_context.random().nextBytes(orig);
for (int i = 0; i < numMsgs - 1; i++) {
byte[] msg = new byte[1024];
System.arraycopy(orig, i * 1024, msg, 0, 1024);
in.messageReceived(i, new ByteArray(msg));
}
assertTrue(in.canAccept(numMsgs, 1));
byte[] msg = new byte[1024];
System.arraycopy(orig, (numMsgs - 1) * 1024, msg, 0, 1024);
in.messageReceived(numMsgs - 1, new ByteArray(msg));
assertTrue(in.canAccept(numMsgs, 1));
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class MessageInputStreamTest method testCanAccept_msgIdExceedsBuffer.
@Test
public void testCanAccept_msgIdExceedsBuffer() {
// Fill the buffer to one message under limit with max-size msgs
int numMsgs = _options.getInboundBufferSize() / _options.getMaxMessageSize();
byte[] orig = new byte[_options.getInboundBufferSize()];
_context.random().nextBytes(orig);
for (int i = 0; i < numMsgs - 2; i++) {
byte[] msg = new byte[_options.getMaxMessageSize()];
System.arraycopy(orig, i * _options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize());
in.messageReceived(i, new ByteArray(msg));
}
// Add two half-size messages (to get past shortcut)
byte[] msg = new byte[_options.getMaxMessageSize() / 2];
System.arraycopy(orig, (numMsgs - 1) * _options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize() / 2);
in.messageReceived(numMsgs - 2, new ByteArray(msg));
in.messageReceived(numMsgs - 1, new ByteArray(msg));
// Check that it predicts only one more msgId will be accepted
assertTrue(in.canAccept(numMsgs, 1));
assertFalse(in.canAccept(numMsgs + 1, 1));
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class MessageInputStreamTest method testCanAccept_readyDup.
@Test
public void testCanAccept_readyDup() {
// Fill the buffer
int numMsgs = _options.getInboundBufferSize() / _options.getMaxMessageSize();
byte[] orig = new byte[_options.getInboundBufferSize()];
_context.random().nextBytes(orig);
for (int i = 0; i < numMsgs; i++) {
byte[] msg = new byte[_options.getMaxMessageSize()];
System.arraycopy(orig, i * _options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize());
in.messageReceived(i, new ByteArray(msg));
}
// Check that new messages won't be accepted
assertFalse(in.canAccept(numMsgs, 1));
// Check that duplicate messages will be accepted
assertTrue(in.canAccept(numMsgs - 1, 1));
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class Storage method getPiece.
/**
* Returns a byte array containing a portion of the requested piece or null if
* the storage doesn't contain the piece yet.
*/
public ByteArray getPiece(int piece, int off, int len) throws IOException {
if (!bitfield.get(piece))
return null;
// Catch a common place for OOMs esp. on 1MB pieces
ByteArray rv;
byte[] bs;
try {
// Will be restored to cache in Message.sendMessage()
if (len == BUFSIZE)
rv = _cache.acquire();
else
rv = new ByteArray(new byte[len]);
} catch (OutOfMemoryError oom) {
if (_log.shouldLog(Log.WARN))
_log.warn("Out of memory, can't honor request for piece " + piece, oom);
return null;
}
bs = rv.getData();
getUncheckedPiece(piece, bs, off, len);
return rv;
}
Aggregations