Search in sources :

Example 46 with ByteArray

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));
}
Also used : ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Example 47 with ByteArray

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));
}
Also used : ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Example 48 with ByteArray

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));
}
Also used : ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Example 49 with ByteArray

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));
}
Also used : ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Example 50 with ByteArray

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;
}
Also used : ByteArray(net.i2p.data.ByteArray)

Aggregations

ByteArray (net.i2p.data.ByteArray)53 Test (org.junit.Test)14 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)3 Destination (net.i2p.data.Destination)3 InterruptedIOException (java.io.InterruptedIOException)2 SessionKey (net.i2p.data.SessionKey)2 I2NPMessage (net.i2p.data.i2np.I2NPMessage)2 I2NPMessageException (net.i2p.data.i2np.I2NPMessageException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InetAddress (java.net.InetAddress)1 ByteBuffer (java.nio.ByteBuffer)1 CancelledKeyException (java.nio.channels.CancelledKeyException)1 NotYetConnectedException (java.nio.channels.NotYetConnectedException)1 SelectionKey (java.nio.channels.SelectionKey)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 MessageDigest (java.security.MessageDigest)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 I2PException (net.i2p.I2PException)1