Example usage for org.apache.commons.compress.archivers.tar TarArchiveInputStream skip

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream skip

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveInputStream skip.

Prototype

public long skip(long numToSkip) throws IOException 

Source Link

Document

Skip bytes in the input buffer.

Usage

From source file:in.neoandroid.neoupdate.neoUpdate.java

private boolean downloadFile(NewAsset asset, String toPath, TarArchiveInputStream tin, TarArchiveEntry ae) {
    if (enableDebug)
        Log.d(TAG, "Start download: " + asset.path + ":NPK: " + (tin != null));
    boolean resume = (db.updateAndGetStatus(asset.path, asset.md5) == neoUpdateDB.UPDATE_STATUS.UPDATE_RESUME);
    String newPath;/* w w w  .j av  a2 s .  com*/
    if (toPath != null)
        newPath = toPath;
    else
        newPath = mapPath(asset.path);
    createSubDirectories(newPath);
    File newFile = new File(newPath);
    long fromBytes = 0;
    if (resume)
        fromBytes = newFile.length();

    try {
        FileOutputStream os = new FileOutputStream(newFile, resume);
        db.setMd5(asset.path, asset.md5);

        if (tin != null && ae != null) {
            // Via NPK
            final int BUFF_SIZE = (8 * 1024); // Buffer size of 8KB
            byte[] buffer = new byte[BUFF_SIZE];
            int n = 0;
            long size = ae.getSize();
            if (resume && fromBytes > 0 && fromBytes < size) {
                tin.skip(fromBytes);
                size -= fromBytes;
            }
            while (size > 0) {
                n = BUFF_SIZE;
                if (n > size)
                    n = (int) size;
                n = tin.read(buffer, 0, n);
                if (n < 0)
                    break;
                if (n > 0)
                    os.write(buffer, 0, n);
            }
        } else if (nConnections <= 0) {
            // Via Local File System
            FileInputStream is = new FileInputStream(baseUrl + asset.path);
            is.getChannel().transferTo(fromBytes, is.getChannel().size() - fromBytes, os.getChannel());
            is.close();
        } else {
            // Via Internet
            HttpResponse resp = HttpWithPostData(asset.path, fromBytes);
            resp.getEntity().writeTo(os);
        }
        db.setDownloaded(asset.path, true);
        os.close();
    } catch (Exception e) {
        if (enableDebug)
            e.printStackTrace();
        return false;
    }
    return true;
}