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();
}
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);
}
}
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");
}
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");
}
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 });
}
Aggregations