Search in sources :

Example 1 with VRandomReadable

use of nl.uva.vlet.vrs.io.VRandomReadable in project lobcder by skoulouzis.

the class VPDRI method doCopy.

private void doCopy(VFile file, Range range, OutputStream out, boolean decript) throws VlException, IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    long len = range.getFinish() - range.getStart() + 1;
    InputStream in = null;
    int buffSize;
    Long start = range.getStart();
    if (len <= Constants.BUF_SIZE) {
        buffSize = (int) len;
    } else {
        buffSize = Constants.BUF_SIZE;
    }
    DesEncrypter en = null;
    if (decript) {
        en = new DesEncrypter(getKeyInt());
    }
    int read = 0;
    try {
        if (file instanceof VRandomReadable) {
            VRandomReadable ra = (VRandomReadable) file;
            byte[] buff = new byte[buffSize];
            int totalBytesRead = 0;
            while (totalBytesRead < len || read != -1) {
                long startT = System.currentTimeMillis();
                read = ra.readBytes(start, buff, 0, buff.length);
                Logger.getLogger(VPDRI.class.getName()).log(Level.FINEST, "speed: {0} kb/s", (read / 1024.0) / ((System.currentTimeMillis() - startT) / 1000.0));
                if (read == -1 || totalBytesRead == len) {
                    break;
                }
                totalBytesRead += read;
                start += buff.length;
                if (decript) {
                    byte[] tmp = en.decrypt(buff);
                    buff = tmp;
                }
                out.write(buff, 0, read);
            }
        } else {
            if (start > 0) {
                throw new IOException("Backend at " + vrl.getScheme() + "://" + vrl.getHostname() + "does not support random reads");
            // long skiped = in.skip(start);
            // if (skiped != start) {
            // long n = start;
            // int buflen = (int) Math.min(Constants.BUF_SIZE, n);
            // byte data[] = new byte[buflen];
            // while (n > 0) {
            // int r = in.read(data, 0, (int) Math.min((long) buflen, n));
            // if (r < 0) {
            // break;
            // }
            // n -= r;
            // }
            // }
            }
            // int totalBytesRead = 0;
            // byte[] buff = new byte[buffSize];
            // while (totalBytesRead < len || read != -1) {
            // if (read == -1 || totalBytesRead == len) {
            // break;
            // }
            // read = in.read(buff, 0, buff.length);
            // totalBytesRead += read;
            // start += buff.length;
            // out.write(buff, 0, read);
            // }
            in = getData();
            if (decript) {
                InputStream tmp = en.wrapInputStream(in);
                in = tmp;
            }
            CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer(buffSize, in, out);
            cBuff.startTransfer(len);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
}
Also used : CircularStreamBufferTransferer(nl.uva.vlet.io.CircularStreamBufferTransferer) DesEncrypter(nl.uva.cs.lobcder.util.DesEncrypter) VRandomReadable(nl.uva.vlet.vrs.io.VRandomReadable)

Aggregations

DesEncrypter (nl.uva.cs.lobcder.util.DesEncrypter)1 CircularStreamBufferTransferer (nl.uva.vlet.io.CircularStreamBufferTransferer)1 VRandomReadable (nl.uva.vlet.vrs.io.VRandomReadable)1