Search in sources :

Example 6 with CountingInputStream

use of org.apache.commons.io.input.CountingInputStream in project indy by Commonjava.

the class DelayedDownload method run.

@Override
public void run() {
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.info("Starting: {}", Thread.currentThread().getName());
    if (initialDelay > 0) {
        logger.info("Delaying: {}", initialDelay);
        try {
            Thread.sleep(initialDelay);
        } catch (final InterruptedException e) {
            return;
        }
    }
    startTime = System.nanoTime();
    content = new ByteArrayOutputStream();
    logger.info("Trying: {}", Thread.currentThread().getName());
    try (InputStream in = client.content().get(key, path)) {
        if (in == null) {
            missing = true;
        } else {
            CountingInputStream cin = new CountingInputStream(in);
            IOUtils.copy(cin, content);
            logger.debug("Read: {} bytes", cin.getByteCount());
        }
    } catch (IndyClientException | IOException e) {
        e.printStackTrace();
    }
    endTime = System.nanoTime();
    latch.countDown();
    logger.info("Stopping: {}", Thread.currentThread().getName());
}
Also used : CountingInputStream(org.apache.commons.io.input.CountingInputStream) InputStream(java.io.InputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IndyClientException(org.commonjava.indy.client.core.IndyClientException) IOException(java.io.IOException) Logger(org.slf4j.Logger)

Example 7 with CountingInputStream

use of org.apache.commons.io.input.CountingInputStream in project jackrabbit by apache.

the class DbDataStore method addRecord.

public DataRecord addRecord(InputStream stream) throws DataStoreException {
    InputStream fileInput = null;
    String tempId = null;
    ResultSet rs = null;
    try {
        long tempModified;
        while (true) {
            try {
                tempModified = System.currentTimeMillis();
                String id = UUID.randomUUID().toString();
                tempId = TEMP_PREFIX + id;
                temporaryInUse.add(tempId);
                // SELECT LENGTH, LAST_MODIFIED FROM DATASTORE WHERE ID=?
                rs = conHelper.query(selectMetaSQL, tempId);
                boolean hasNext = rs.next();
                DbUtility.close(rs);
                rs = null;
                if (hasNext) {
                    // re-try in the very, very unlikely event that the row already exists
                    continue;
                }
                // INSERT INTO DATASTORE VALUES(?, 0, ?, NULL)
                conHelper.exec(insertTempSQL, tempId, tempModified);
                break;
            } catch (Exception e) {
                throw convert("Can not insert new record", e);
            } finally {
                DbUtility.close(rs);
                // prevent that rs.close() is called again
                rs = null;
            }
        }
        MessageDigest digest = getDigest();
        DigestInputStream dIn = new DigestInputStream(stream, digest);
        CountingInputStream in = new CountingInputStream(dIn);
        StreamWrapper wrapper;
        if (STORE_SIZE_MINUS_ONE.equals(storeStream)) {
            wrapper = new StreamWrapper(in, -1);
        } else if (STORE_SIZE_MAX.equals(storeStream)) {
            wrapper = new StreamWrapper(in, Integer.MAX_VALUE);
        } else if (STORE_TEMP_FILE.equals(storeStream)) {
            File temp = moveToTempFile(in);
            long length = temp.length();
            wrapper = new StreamWrapper(new ResettableTempFileInputStream(temp), length);
        } else {
            throw new DataStoreException("Unsupported stream store algorithm: " + storeStream);
        }
        // UPDATE DATASTORE SET DATA=? WHERE ID=?
        conHelper.exec(updateDataSQL, wrapper, tempId);
        long length = in.getByteCount();
        DataIdentifier identifier = new DataIdentifier(encodeHexString(digest.digest()));
        usesIdentifier(identifier);
        String id = identifier.toString();
        long newModified;
        while (true) {
            newModified = System.currentTimeMillis();
            if (checkExisting(tempId, length, identifier)) {
                touch(identifier, newModified);
                conHelper.exec(deleteSQL, tempId);
                break;
            }
            try {
                // UPDATE DATASTORE SET ID=?, LENGTH=?, LAST_MODIFIED=?
                // WHERE ID=? AND LAST_MODIFIED=?
                int count = conHelper.update(updateSQL, id, length, newModified, tempId, tempModified);
                // collection could delete rows)
                if (count != 0) {
                    // update was successful
                    break;
                }
            } catch (SQLException e) {
            // duplicate key (the row already exists) - repeat
            // we use exception handling for flow control here, which is bad,
            // but the alternative is to use UPDATE ... WHERE ... (SELECT ...)
            // which could cause a deadlock in some databases - also,
            // duplicate key will only occur if somebody else concurrently
            // added the same record (which is very unlikely)
            }
            // SELECT LENGTH, LAST_MODIFIED FROM DATASTORE WHERE ID=?
            rs = conHelper.query(selectMetaSQL, tempId);
            if (!rs.next()) {
                // the row was deleted, which is unexpected / not allowed
                String msg = DIGEST + " temporary entry deleted: " + " id=" + tempId + " length=" + length;
                log.error(msg);
                throw new DataStoreException(msg);
            }
            tempModified = rs.getLong(2);
            DbUtility.close(rs);
            rs = null;
        }
        usesIdentifier(identifier);
        DbDataRecord record = new DbDataRecord(this, identifier, length, newModified);
        return record;
    } catch (Exception e) {
        throw convert("Can not insert new record", e);
    } finally {
        if (tempId != null) {
            temporaryInUse.remove(tempId);
        }
        DbUtility.close(rs);
        if (fileInput != null) {
            try {
                fileInput.close();
            } catch (IOException e) {
                throw convert("Can not close temporary file", e);
            }
        }
    }
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) DigestInputStream(java.security.DigestInputStream) SQLException(java.sql.SQLException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) IOException(java.io.IOException) SQLException(java.sql.SQLException) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ResultSet(java.sql.ResultSet) StreamWrapper(org.apache.jackrabbit.core.util.db.StreamWrapper) MessageDigest(java.security.MessageDigest) File(java.io.File)

Aggregations

CountingInputStream (org.apache.commons.io.input.CountingInputStream)7 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 ZipInputStream (java.util.zip.ZipInputStream)2 TarInputStream (hudson.org.apache.tools.tar.TarInputStream)1 RemoteInputStream (hudson.remoting.RemoteInputStream)1 IOException2 (hudson.util.IOException2)1 DataReader (info.ata4.io.DataReader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 BindException (java.net.BindException)1 URLConnection (java.net.URLConnection)1 DigestInputStream (java.security.DigestInputStream)1