Example usage for org.apache.commons.io.input CountingInputStream getByteCount

List of usage examples for org.apache.commons.io.input CountingInputStream getByteCount

Introduction

In this page you can find the example usage for org.apache.commons.io.input CountingInputStream getByteCount.

Prototype

public synchronized long getByteCount() 

Source Link

Document

The number of bytes that have passed through this stream.

Usage

From source file:com.guye.baffle.decoder.ArscData.java

private static ArscData readTable(File file, CountingInputStream countIn, ExtDataInput in) throws IOException {
    ArscData arscData = new ArscData();
    arscData.mFile = file;//  w  w  w.  j a v  a2 s. c o m
    arscData.mHeader = Header.read(in);
    int packageCount = in.readInt();
    if (packageCount != 1) {
        throw new UnsupportedOperationException("not support more then 1 package");
    }
    arscData.mTableStrings = StringBlock.read(in);
    arscData.mPkgHeaderIndex = (int) countIn.getByteCount();
    arscData.mPkgHeader = PackageHeader.read(in);
    arscData.mTypeStrStart = (int) countIn.getByteCount();
    arscData.mTypeNames = StringBlock.read(in);
    arscData.mTypeStrEnd = (int) countIn.getByteCount();
    arscData.mSpecNames = StringBlock.read(in);
    arscData.mResIndex = (int) countIn.getByteCount();
    return arscData;
}

From source file:com.intel.cosbench.driver.operator.FileWriter.java

public static Sample doWrite(InputStream in, long length, String conName, String objName, Config config,
        Session session) {//from  ww  w . j  a  v  a2s.  c o  m
    if (Thread.interrupted())
        throw new AbortedException();

    CountingInputStream cin = new CountingInputStream(in);

    long start = System.currentTimeMillis();

    try {
        session.getApi().createObject(conName, objName, cin, length, config);
    } catch (StorageInterruptedException sie) {
        throw new AbortedException();
    } catch (Exception e) {
        session.getLogger().error("fail to perform write operation", e);
        return new Sample(new Date(), OP_TYPE, false);
    } finally {
        IOUtils.closeQuietly(cin);
    }

    long end = System.currentTimeMillis();

    Date now = new Date(end);
    return new Sample(now, OP_TYPE, true, end - start, cin.getByteCount());
}

From source file:ch.cyberduck.core.dav.DAVReadFeatureTest.java

@Test
public void testReadCloseReleaseEntity() throws Exception {
    final Host host = new Host(new DAVSSLProtocol(), "svn.cyberduck.ch",
            new Credentials(PreferencesFactory.get().getProperty("connection.login.anon.name"), null));
    final DAVSession session = new DAVSession(host);
    session.open(new DisabledHostKeyCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final TransferStatus status = new TransferStatus();
    final Path test = new Path("/trunk/LICENSE.txt", EnumSet.of(Path.Type.file));
    final CountingInputStream in = new CountingInputStream(
            new DAVReadFeature(session).read(test, status, new DisabledConnectionCallback()));
    in.close();/*w ww. j a v a  2 s.  com*/
    assertEquals(0L, in.getByteCount(), 0L);
    session.close();
}

From source file:ch.cyberduck.core.sds.SDSReadFeatureTest.java

@Test
public void testReadCloseReleaseEntity() throws Exception {
    final Host host = new Host(new SDSProtocol(), "duck.ssp-europe.eu", new Credentials(
            System.getProperties().getProperty("sds.user"), System.getProperties().getProperty("sds.key")));
    final SDSSession session = new SDSSession(host, new DisabledX509TrustManager(),
            new DefaultX509KeyManager());
    session.open(new DisabledHostKeyCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final TransferStatus status = new TransferStatus();
    final byte[] content = RandomUtils.nextBytes(32769);
    final TransferStatus writeStatus = new TransferStatus();
    writeStatus.setLength(content.length);
    final Path room = new SDSDirectoryFeature(session)
            .mkdir(new Path(new AlphanumericRandomStringService().random(),
                    EnumSet.of(Path.Type.directory, Path.Type.volume)), null, new TransferStatus());
    final Path test = new Path(room, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final SDSWriteFeature writer = new SDSWriteFeature(session);
    final HttpResponseOutputStream<VersionId> out = writer.write(test, writeStatus,
            new DisabledConnectionCallback());
    assertNotNull(out);//  w w  w  . jav a  2 s  .co m
    new StreamCopier(writeStatus, writeStatus).transfer(new ByteArrayInputStream(content), out);
    final CountingInputStream in = new CountingInputStream(
            new SDSReadFeature(session).read(test, status, new DisabledConnectionCallback()));
    in.close();
    assertEquals(0L, in.getByteCount(), 0L);
    new SDSDeleteFeature(session).delete(Collections.singletonList(room), new DisabledLoginCallback(),
            new Delete.DisabledCallback());
    session.close();
}

From source file:net.sf.jsignpdf.crl.CRLInfo.java

/**
 * Initialize CRLs (load URLs from certificates and download the CRLs).
 *///ww w.  j  a  va  2 s . co  m
private void initCrls() {
    if (!options.isCrlEnabledX() || crls != null) {
        return;
    }
    LOGGER.info(RES.get("console.readingCRLs"));
    final Set<String> urls = new HashSet<String>();
    for (Certificate cert : certChain) {
        if (cert instanceof X509Certificate) {
            urls.addAll(getCrlUrls((X509Certificate) cert));
        }
    }
    final Set<CRL> crlSet = new HashSet<CRL>();
    for (final String urlStr : urls) {
        try {
            LOGGER.info(RES.get("console.crlinfo.loadCrl", urlStr));
            final URL tmpUrl = new URL(urlStr);
            final CountingInputStream inStream = new CountingInputStream(
                    tmpUrl.openConnection(options.createProxy()).getInputStream());
            final CertificateFactory cf = CertificateFactory.getInstance(Constants.CERT_TYPE_X509);
            final CRL crl = cf.generateCRL(inStream);
            final long tmpBytesRead = inStream.getByteCount();
            LOGGER.info(RES.get("console.crlinfo.crlSize", String.valueOf(tmpBytesRead)));
            if (!crlSet.contains(crl)) {
                byteCount += tmpBytesRead;
                crlSet.add(crl);
            } else {
                LOGGER.info(RES.get("console.crlinfo.alreadyLoaded"));
            }
            inStream.close();
        } catch (MalformedURLException e) {
            LOGGER.warn("", e);
        } catch (IOException e) {
            LOGGER.warn("", e);
        } catch (CertificateException e) {
            LOGGER.warn("", e);
        } catch (CRLException e) {
            LOGGER.warn("", e);
        }
    }
    crls = crlSet.toArray(new CRL[crlSet.size()]);
}

From source file:eu.annocultor.reports.parts.ReportCounter.java

@SuppressWarnings("unchecked")
@Override/*from  w w w. j a v  a 2  s.c om*/
public void load() throws IOException {
    XStream xStream = new XStream();
    //BufferedReader reader = new BufferedReader(new FileReader(getFile())); 
    CountingInputStream cis = new CountingInputStream(new FileInputStream(getFile()));
    ObjectInputStream in = xStream.createObjectInputStream(cis);
    try {
        int mb = 1;
        // how ugly but it should throw exception at the end
        while (true) {
            ObjectCountPair<T> ocp = (ObjectCountPair<T>) in.readObject();
            inc(ocp.getObject(), ocp.getCount());
            if (cis.getByteCount() > FileUtils.ONE_MB * 100 * mb) {
                log.info("Passed " + (cis.getByteCount() / FileUtils.ONE_MB) + " MB");
                mb++;
            }
        }
    } catch (EOFException e) {
        // normal end of file
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    } finally {
        in.close();
        cis.close();
    }
}

From source file:com.moesol.geoserver.sync.client.AbstractGeoserverClientSynchronizer.java

void processGmlResponse(Response response) throws IOException, SAXException, ParserConfigurationException {
    FeatureCollection<?, ?> features;
    if (response instanceof ResponseFeatureCollection) {
        ResponseFeatureCollection responseFeatures = (ResponseFeatureCollection) response;
        features = responseFeatures.getFeatureCollection();
    } else {/*  w w  w . j  av  a 2  s.com*/
        CountingInputStream counter = new CountingInputStream(response.getResultStream());
        features = (FeatureCollection<?, ?>) parseWfs(counter);
        m_rxGml += counter.getByteCount();
    }

    FeatureIterator<?> it = features.features();
    try {
        while (it.hasNext()) {
            Feature feature = it.next();
            FeatureId fid = feature.getIdentifier();
            m_potentialDeletes.remove(fid);
            if (!m_features.containsKey(fid)) {
                m_listener.featureCreate(fid, feature);
                m_numCreates++;
            } else {
                m_listener.featureUpdate(fid, feature);
                m_numUpdates++;
            }
        }
    } finally {
        it.close();
    }
}

From source file:com.blackducksoftware.integration.hub.cli.CLIDownloadService.java

public void customInstall(HubProxyInfo hubProxyInfo, CLILocation cliLocation,
        CIEnvironmentVariables ciEnvironmentVariables, final URL archive, String hubVersion,
        final String localHostName) throws IOException, InterruptedException, HubIntegrationException,
        IllegalArgumentException, EncryptionException {
    boolean cliMismatch = true;
    try {//from  w ww .jav a2s  . c om
        final File hubVersionFile = cliLocation.createHubVersionFile();
        if (hubVersionFile.exists()) {
            final String storedHubVersion = IOUtils.toString(new FileReader(hubVersionFile));
            if (hubVersion.equals(storedHubVersion)) {
                cliMismatch = false;
            } else {
                hubVersionFile.delete();
                hubVersionFile.createNewFile();
            }
        }
        final File cliInstallDirectory = cliLocation.getCLIInstallDir();
        if (!cliInstallDirectory.exists()) {
            cliMismatch = true;
        }

        if (cliMismatch) {
            logger.debug("Attempting to download the Hub CLI.");
            final FileWriter writer = new FileWriter(hubVersionFile);
            writer.write(hubVersion);
            writer.close();
            hubVersionFile.setLastModified(0L);
        }
        final long cliTimestamp = hubVersionFile.lastModified();

        URLConnection connection = null;
        try {
            Proxy proxy = null;
            if (hubProxyInfo != null) {
                String proxyHost = hubProxyInfo.getHost();
                int proxyPort = hubProxyInfo.getPort();
                String proxyUsername = hubProxyInfo.getUsername();
                String proxyPassword = hubProxyInfo.getDecryptedPassword();

                if (StringUtils.isNotBlank(proxyHost) && proxyPort > 0) {
                    proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
                }
                if (proxy != null) {
                    if (StringUtils.isNotBlank(proxyUsername) && StringUtils.isNotBlank(proxyPassword)) {
                        AuthenticatorUtil.setAuthenticator(proxyUsername, proxyPassword);
                    } else {
                        AuthenticatorUtil.resetAuthenticator();
                    }
                }
            }
            if (proxy != null) {
                connection = archive.openConnection(proxy);
            } else {
                connection = archive.openConnection();
            }
            connection.setIfModifiedSince(cliTimestamp);
            connection.connect();
        } catch (final IOException ioe) {
            logger.error("Skipping installation of " + archive + " to " + cliLocation.getCanonicalPath() + ": "
                    + ioe.toString());
            return;
        }

        if (connection instanceof HttpURLConnection
                && ((HttpURLConnection) connection).getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
            // CLI has not been modified
            return;
        }

        final long sourceTimestamp = connection.getLastModified();

        if (cliInstallDirectory.exists() && cliInstallDirectory.listFiles().length > 0) {
            if (!cliMismatch && sourceTimestamp == cliTimestamp) {
                logger.debug("The current Hub CLI is up to date.");
                return;
            }
            for (final File file : cliInstallDirectory.listFiles()) {
                FileUtils.deleteDirectory(file);
            }
        } else {
            cliInstallDirectory.mkdir();
        }
        logger.debug("Updating the Hub CLI.");
        hubVersionFile.setLastModified(sourceTimestamp);

        logger.info("Unpacking " + archive.toString() + " to " + cliInstallDirectory.getCanonicalPath() + " on "
                + localHostName);

        final CountingInputStream cis = new CountingInputStream(connection.getInputStream());
        try {
            unzip(cliInstallDirectory, cis, logger);
            updateJreSecurity(logger, cliLocation, ciEnvironmentVariables);
        } catch (final IOException e) {
            throw new IOException(String.format("Failed to unpack %s (%d bytes read of total %d)", archive,
                    cis.getByteCount(), connection.getContentLength()), e);
        }
    } catch (final IOException e) {
        throw new IOException("Failed to install " + archive + " to " + cliLocation.getCanonicalPath(), e);
    }
}

From source file:info.mikaelsvensson.devtools.analysis.db2eventlog.Db2EventLogReport.java

private void load(File logFile, String charsetName) throws IOException, XMLStreamException {

    CountingInputStream is = new CountingInputStream(new FileInputStream(logFile));
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, charsetName));

    String line = null;//ww w  .  j  ava 2s.c  om
    Db2EventLogSample currentSample = null;
    long fileSize = logFile.length();
    int percentDone = 0;

    _records = new HashMap<String, QueryStatistics>();
    while ((line = reader.readLine()) != null) {
        if (line.endsWith("Statement Event ...")) {
            int percentTemp = (int) (100.0 * is.getByteCount() / fileSize);
            if (percentDone != percentTemp) {
                percentDone = percentTemp;
                System.out.format("Progress: %d%%\r", percentDone);
            }
            handleSample(currentSample, _records);
            currentSample = new Db2EventLogSample(new Date(), 0);
        } else if (line.startsWith(LINE_PREFIX_OPERATION)) {
            currentSample.setOperation(line.substring(LINE_PREFIX_OPERATION.length()));
        } else if (line.startsWith(LINE_PREFIX_TEXT)) {
            currentSample.setText(line.substring(LINE_PREFIX_TEXT.length()));
        } else if (line.startsWith(LINE_PREFIX_START_TIME)) {
            try {
                String timeString = line.substring(LINE_PREFIX_START_TIME.length(),
                        LINE_PREFIX_START_TIME.length() + TIME_FORMAT.toPattern().length());
                Date timeStamp = TIME_FORMAT.parse(timeString);
                currentSample.setTimeStamp(timeStamp);
            } catch (ParseException e) {
                e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }
        } else if (line.startsWith(LINE_PREFIX_ELAPSED_EXECUTION_TIME)) {
            String timeStr = line.substring(LINE_PREFIX_ELAPSED_EXECUTION_TIME.length());
            double time = Double.parseDouble(timeStr.substring(0, timeStr.indexOf(' ')));
            currentSample.setResponseTime((int) (time * MILLION));
        } else if (line.startsWith(LINE_PREFIX_FETCH_COUNT)) {
            long count = getLongAfterPrefix(line, LINE_PREFIX_FETCH_COUNT);
            currentSample.increaseFetchCount(count);
        } else if (line.startsWith(LINE_PREFIX_SORTS)) {
            long count = getLongAfterPrefix(line, LINE_PREFIX_SORTS);
            currentSample.increaseSorts(count);
        } else if (line.startsWith(LINE_PREFIX_TOTAL_SORT_TIME)) {
            long count = getLongAfterPrefix(line, LINE_PREFIX_TOTAL_SORT_TIME);
            currentSample.increaseTotalSortTime(count);
        } else if (line.startsWith(LINE_PREFIX_SORT_OVERFLOWS)) {
            long count = getLongAfterPrefix(line, LINE_PREFIX_SORT_OVERFLOWS);
            currentSample.increaseSortOverflows(count);
        } else if (line.startsWith(LINE_PREFIX_ROWS_READ)) {
            long count = getLongAfterPrefix(line, LINE_PREFIX_ROWS_READ);
            currentSample.increaseRowsRead(count);
        } else if (line.startsWith(LINE_PREFIX_ROWS_WRITTEN)) {
            long count = getLongAfterPrefix(line, LINE_PREFIX_ROWS_WRITTEN);
            currentSample.increaseRowsWritten(count);
        }
        /*
                    else if (line.startsWith(LINE_PREFIX_INTERNAL_ROWS_DELETED))
                    {
        long count = getLongAfterPrefix(line, LINE_PREFIX_INTERNAL_ROWS_DELETED);
        currentSample.increaseInternalRowsDeleted(count);
                    }
                    else if (line.startsWith(LINE_PREFIX_INTERNAL_ROWS_UPDATED))
                    {
        long count = getLongAfterPrefix(line, LINE_PREFIX_INTERNAL_ROWS_UPDATED);
        currentSample.increaseInternalRowsUpdated(count);
                    }
                    else if (line.startsWith(LINE_PREFIX_INTERNAL_ROWS_INSERTED))
                    {
        long count = getLongAfterPrefix(line, LINE_PREFIX_INTERNAL_ROWS_INSERTED);
        currentSample.increaseInternalRowsInserted(count);
                    }
        */
    }
    handleSample(currentSample, _records);
}

From source file:de.huxhorn.lilith.log4j.xml.Log4jImportCallable.java

public Long call() throws Exception {
    if (!inputFile.isFile()) {
        throw new IllegalArgumentException("'" + inputFile.getAbsolutePath() + "' is not a file!");
    }//  w w w .  j  ava  2 s  .com
    if (!inputFile.canRead()) {
        throw new IllegalArgumentException("'" + inputFile.getAbsolutePath() + "' is not a readable!");
    }
    long fileSize = inputFile.length();
    setNumberOfSteps(fileSize);
    FileInputStream fis = new FileInputStream(inputFile);
    CountingInputStream cis = new CountingInputStream(fis);

    String fileName = inputFile.getName().toLowerCase(Locale.US);
    BufferedReader br;
    if (fileName.endsWith(".gz")) {
        br = new BufferedReader(new InputStreamReader(new GZIPInputStream(cis), StandardCharsets.UTF_8));
    } else {
        br = new BufferedReader(new InputStreamReader(cis, StandardCharsets.UTF_8));
    }

    StringBuilder builder = new StringBuilder();

    result = 0;
    for (;;) {
        String line = br.readLine();
        setCurrentStep(cis.getByteCount());
        if (line == null) {
            evaluate(builder.toString());
            break;
        }
        for (;;) {
            int closeIndex = line.indexOf(CLOSING_LOG4J_EVENT_TAG);
            if (closeIndex >= 0) {
                int endIndex = closeIndex + CLOSING_LOG4J_EVENT_TAG.length();
                builder.append(line.subSequence(0, endIndex));
                evaluate(builder.toString());
                builder.setLength(0);
                line = line.substring(endIndex);
            } else {
                builder.append(line);
                builder.append("\n");
                break;
            }
        }
    }
    return result;
}