Search in sources :

Example 51 with ByteArray

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) {
            }
    }
}
Also used : ByteArray(net.i2p.data.ByteArray) IOException(java.io.IOException)

Example 52 with ByteArray

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

Example 53 with ByteArray

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);
    }
}
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