use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class BasicServlet method copy.
/**
* Write from in to out
*/
private void copy(InputStream in, long skip, OutputStream out, final long len) throws IOException {
ByteArray ba = _cache.acquire();
byte[] buf = ba.getData();
try {
if (skip > 0)
DataHelper.skip(in, skip);
int read = 0;
long tot = 0;
boolean done = false;
while ((read = in.read(buf)) != -1 && !done) {
if (len >= 0) {
tot += read;
if (tot >= len) {
read -= (int) (tot - len);
done = true;
}
}
out.write(buf, 0, read);
}
} finally {
_cache.release(ba, false);
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
if (out != null)
try {
out.close();
} catch (IOException ioe) {
}
}
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class ByteCache method acquire.
/**
* Get the next available structure, either from the cache or a brand new one.
* Returned ByteArray will have valid = 0 and offset = 0.
* Returned ByteArray may or may not be zero, depends on whether
* release(ba) or release(ba, false) was called.
* Which is a problem, you should really specify shouldZero on acquire, not release.
*/
public final ByteArray acquire() {
if (_cache) {
ByteArray rv = _available.poll();
if (rv != null)
return rv;
}
_lastOverflow = System.currentTimeMillis();
byte[] data = new byte[_entrySize];
ByteArray rv = new ByteArray(data);
rv.setValid(0);
// rv.setOffset(0);
return rv;
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class PartialPiece method read.
/**
* Blocking.
* If offset matches the previous downloaded amount
* (as set by a previous call to read() or setDownlaoded()),
* the downloaded amount will be incremented by len.
*
* @since 0.9.1
*/
public void read(DataInputStream din, int offset, int len) throws IOException {
if (bs != null) {
din.readFully(bs, offset, len);
synchronized (this) {
// only works for in-order chunks
if (this.off == offset)
this.off += len;
}
} else {
// read in fully before synching on raf
ByteArray ba;
byte[] tmp;
if (len == BUFSIZE) {
ba = _cache.acquire();
tmp = ba.getData();
} else {
ba = null;
tmp = new byte[len];
}
din.readFully(tmp);
synchronized (this) {
if (raf == null)
createTemp();
raf.seek(offset);
raf.write(tmp);
// only works for in-order chunks
if (this.off == offset)
this.off += len;
}
if (ba != null)
_cache.release(ba, false);
}
}
Aggregations