Search in sources :

Example 11 with ByteArray

use of net.i2p.data.ByteArray in project i2p.i2p by i2p.

the class PartialPiece method getHash.

/**
 **
 *    public long getCreated() {
 *         return this.createdTime;
 *    }
 ***
 */
/**
 *  Piece must be complete.
 *  The SHA1 hash of the completely read data.
 *  @since 0.9.1
 */
public byte[] getHash() throws IOException {
    MessageDigest sha1 = SHA1.getInstance();
    if (bs != null) {
        sha1.update(bs);
    } else {
        int read = 0;
        int buflen = Math.min(pclen, BUFSIZE);
        ByteArray ba;
        byte[] buf;
        if (buflen == BUFSIZE) {
            ba = _cache.acquire();
            buf = ba.getData();
        } else {
            ba = null;
            buf = new byte[buflen];
        }
        synchronized (this) {
            if (raf == null)
                throw new IOException();
            raf.seek(0);
            while (read < pclen) {
                int rd = raf.read(buf, 0, Math.min(buf.length, pclen - read));
                if (rd < 0)
                    break;
                read += rd;
                sha1.update(buf, 0, rd);
            }
        }
        if (ba != null)
            _cache.release(ba, false);
        if (read < pclen)
            throw new IOException();
    }
    return sha1.digest();
}
Also used : ByteArray(net.i2p.data.ByteArray) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 12 with ByteArray

use of net.i2p.data.ByteArray in project i2p.i2p by i2p.

the class PartialPiece method write.

/**
 *  Piece must be complete.
 *  Caller must synchronize on out and seek to starting point.
 *  Caller must call release() when done with the whole piece.
 *
 *  @param out stream to write to
 *  @param offset offset in the piece
 *  @param len length to write
 *  @since 0.9.1
 */
public void write(DataOutput out, int offset, int len) throws IOException {
    if (bs != null) {
        out.write(bs, offset, len);
    } else {
        int read = 0;
        int buflen = Math.min(len, BUFSIZE);
        ByteArray ba;
        byte[] buf;
        if (buflen == BUFSIZE) {
            ba = _cache.acquire();
            buf = ba.getData();
        } else {
            ba = null;
            buf = new byte[buflen];
        }
        synchronized (this) {
            if (raf == null)
                throw new IOException();
            raf.seek(offset);
            while (read < len) {
                int rd = Math.min(buf.length, len - read);
                raf.readFully(buf, 0, rd);
                read += rd;
                out.write(buf, 0, rd);
            }
        }
        if (ba != null)
            _cache.release(ba, false);
    }
}
Also used : ByteArray(net.i2p.data.ByteArray) IOException(java.io.IOException)

Example 13 with ByteArray

use of net.i2p.data.ByteArray in project i2p.i2p by i2p.

the class MessageInputStreamTest method testRandomDups.

@Test
public void testRandomDups() throws IOException {
    byte[] orig = new byte[256 * 1024];
    _context.random().nextBytes(orig);
    for (int n = 0; n < 3; n++) {
        ArrayList<Integer> order = new ArrayList<Integer>(32);
        for (int i = 0; i < orig.length / 1024; i++) order.add(new Integer(i));
        Collections.shuffle(order);
        for (int i = 0; i < orig.length / 1024; i++) {
            byte[] msg = new byte[1024];
            Integer cur = (Integer) order.get(i);
            System.arraycopy(orig, cur.intValue() * 1024, msg, 0, 1024);
            in.messageReceived(cur.intValue(), new ByteArray(msg));
            _log.debug("Injecting " + cur);
        }
    }
    byte[] read = new byte[orig.length];
    int howMany = DataHelper.read(in, read);
    if (howMany != orig.length)
        fail("not enough bytes read [" + howMany + "]");
    if (!DataHelper.eq(orig, read))
        fail("data read is not equal");
    _log.info("Passed test: random dups");
}
Also used : ArrayList(java.util.ArrayList) ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Example 14 with ByteArray

use of net.i2p.data.ByteArray in project i2p.i2p by i2p.

the class MessageInputStreamTest method testRandomOrder.

@Test
public void testRandomOrder() throws IOException {
    byte[] orig = new byte[256 * 1024];
    _context.random().nextBytes(orig);
    ArrayList<Integer> order = new ArrayList<Integer>(32);
    for (int i = 0; i < orig.length / 1024; i++) order.add(new Integer(i));
    Collections.shuffle(order);
    for (int i = 0; i < orig.length / 1024; i++) {
        byte[] msg = new byte[1024];
        Integer cur = (Integer) order.get(i);
        System.arraycopy(orig, cur.intValue() * 1024, msg, 0, 1024);
        in.messageReceived(cur.intValue(), new ByteArray(msg));
        _log.debug("Injecting " + cur);
    }
    byte[] read = new byte[orig.length];
    int howMany = DataHelper.read(in, read);
    if (howMany != orig.length)
        fail("not enough bytes read [" + howMany + "]");
    if (!DataHelper.eq(orig, read))
        fail("data read is not equal");
    _log.info("Passed test: random order");
}
Also used : ArrayList(java.util.ArrayList) ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Example 15 with ByteArray

use of net.i2p.data.ByteArray in project i2p.i2p by i2p.

the class MessageInputStreamTest method testUpdateAcks_missingMsgs.

@Test
public void testUpdateAcks_missingMsgs() {
    in.messageReceived(0, new ByteArray());
    in.messageReceived(2, new ByteArray());
    in.updateAcks(packetLocal);
    verify(packetLocal).setAckThrough(2);
    verify(packetLocal).setNacks(new long[] { 1 });
}
Also used : ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

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