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

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

Introduction

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

Prototype

public int read(byte[] b) throws IOException 

Source Link

Document

Reads a number of bytes into the byte array, keeping count of the number read.

Usage

From source file:com.playonlinux.win32.pe.PEReader.java

private static SectionHeader[] readSectionHeaders(CountingInputStream executableInputStream,
        ImageNTHeaders imageNTHeaders) throws IOException {
    final int numberOfSectionHeaders = imageNTHeaders.fileHeader.numberOfSections.getUnsignedValue();
    final SectionHeader[] sectionHeaders = new SectionHeader[numberOfSectionHeaders];
    for (int i = 0; i < numberOfSectionHeaders; i++) {
        byte[] sectionHeaderBytes = new byte[SectionHeader.SECTION_HEADER_SIZE];
        executableInputStream.read(sectionHeaderBytes);
        sectionHeaders[i] = new SectionHeader(sectionHeaderBytes);
    }/*from w  w  w  .j a v a2  s .  co  m*/
    return sectionHeaders;
}

From source file:com.playonlinux.win32.pe.PEReader.java

private static RsrcSection readResourceSection(CountingInputStream executableInputStream,
        SectionHeader[] sectionHeaders) throws IOException {
    SectionHeader rsrcSectionHeader = null;
    for (SectionHeader sectionHeader : sectionHeaders) {
        if (".rsrc\u0000\u0000\u0000".equals(new String(sectionHeader.name))) {
            rsrcSectionHeader = sectionHeader;
        }/*from w  ww .j a v  a  2s  .c  o  m*/
    }

    if (rsrcSectionHeader == null) {
        return null;
    }

    long numberToSkip = rsrcSectionHeader.pointerToRawData.getUnsignedValue()
            - executableInputStream.getCount();
    executableInputStream.skip(numberToSkip);
    byte[] rsrcSection = new byte[(int) rsrcSectionHeader.sizeOfRawData.getUnsignedValue()];
    executableInputStream.read(rsrcSection);

    return new RsrcSection(rsrcSection);
}

From source file:com.constellio.app.services.appManagement.AppManagementService.java

public void getWarFromServer(ProgressInfo progressInfo)
        throws AppManagementServiceRuntimeException.CannotConnectToServer {
    String URL_WAR = getWarURLFromServer();

    System.out.println("URL FOR WAR => " + URL_WAR);

    try {//from   w ww  .j  a va2 s.  co  m
        progressInfo.reset();
        progressInfo.setTask("Getting WAR from server");
        progressInfo.setEnd(1);

        progressInfo.setProgressMessage("Downloading WAR");
        InputStream input = getInputForPost(URL_WAR, getLicenseInfo().getSignature());
        if (input == null) {
            throw new AppManagementServiceRuntimeException.CannotConnectToServer(URL_WAR);
        }
        CountingInputStream countingInputStream = new CountingInputStream(input);

        progressInfo.setProgressMessage("Creating WAR file");
        OutputStream warFileOutput = getWarFileDestination().create("war upload");

        try {
            progressInfo.setProgressMessage("Copying downloaded WAR");

            byte[] buffer = new byte[8 * 1024];
            int bytesRead;
            while ((bytesRead = countingInputStream.read(buffer)) != -1) {
                warFileOutput.write(buffer, 0, bytesRead);
                long totalBytesRead = countingInputStream.getByteCount();
                String progressMessage = FileUtils.byteCountToDisplaySize(totalBytesRead);
                progressInfo.setProgressMessage(progressMessage);
            }
        } finally {
            IOUtils.closeQuietly(countingInputStream);
            IOUtils.closeQuietly(warFileOutput);
        }
        progressInfo.setCurrentState(1);

    } catch (IOException ioe) {
        throw new AppManagementServiceRuntimeException.CannotConnectToServer(URL_WAR, ioe);
    }
}

From source file:org.eclipse.che.api.vfs.server.util.ZipContent.java

public static ZipContent newInstance(InputStream in) throws IOException {
    java.io.File file = null;/*ww w . j a v  a2  s .co m*/
    byte[] inMemory = null;

    int count = 0;
    ByteArrayOutputStream inMemorySpool = new ByteArrayOutputStream(BUFFER);

    int bytes;
    final byte[] buff = new byte[BUFFER_SIZE];
    while (count <= BUFFER && (bytes = in.read(buff)) != -1) {
        inMemorySpool.write(buff, 0, bytes);
        count += bytes;
    }

    InputStream spool;
    if (count > BUFFER) {
        file = java.io.File.createTempFile("import", ".zip");
        try (FileOutputStream fileSpool = new FileOutputStream(file)) {
            inMemorySpool.writeTo(fileSpool);
            while ((bytes = in.read(buff)) != -1) {
                fileSpool.write(buff, 0, bytes);
            }
        }
        spool = new FileInputStream(file);
    } else {
        inMemory = inMemorySpool.toByteArray();
        spool = new ByteArrayInputStream(inMemory);
    }

    // Counts numbers of compressed data.
    try (CountingInputStream compressedCounter = new CountingInputStream(spool);
            ZipInputStream zip = new ZipInputStream(compressedCounter)) {

        // Counts number of uncompressed data.
        CountingInputStream uncompressedCounter = new CountingInputStream(zip) {
            @Override
            public int read() throws IOException {
                int i = super.read();
                checkCompressionRatio();
                return i;
            }

            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                int i = super.read(b, off, len);
                checkCompressionRatio();
                return i;
            }

            @Override
            public int read(byte[] b) throws IOException {
                int i = super.read(b);
                checkCompressionRatio();
                return i;
            }

            @Override
            public long skip(long length) throws IOException {
                long i = super.skip(length);
                checkCompressionRatio();
                return i;
            }

            private void checkCompressionRatio() {
                long uncompressedBytes = getByteCount(); // number of uncompressed bytes
                if (uncompressedBytes > ZIP_THRESHOLD) {
                    long compressedBytes = compressedCounter.getByteCount(); // number of compressed bytes
                    if (uncompressedBytes > (ZIP_RATIO * compressedBytes)) {
                        throw new RuntimeException("Zip bomb detected. ");
                    }
                }
            }
        };

        ZipEntry zipEntry;
        while ((zipEntry = zip.getNextEntry()) != null) {
            if (!zipEntry.isDirectory()) {
                while (uncompressedCounter.read(buff) != -1) {
                    // Read full data from stream to be able detect zip-bomb.
                }
            }
        }

        return new ZipContent(
                inMemory != null ? new ByteArrayInputStream(inMemory) : new DeleteOnCloseFileInputStream(file),
                file == null);
    }
}

From source file:org.eclipse.hudson.plugins.PluginInstallationJob.java

private File download(URL src) throws IOException {
    URLConnection con;//from  w w  w .j  a  v a2  s. c  om
    if ((proxyConfig != null) && (proxyConfig.name != null)) {
        con = proxyConfig.openUrl(src);
    } else {
        con = src.openConnection();
    }
    int total = con.getContentLength();
    CountingInputStream in = new CountingInputStream(con.getInputStream());
    byte[] buf = new byte[8192];
    int len;

    File dst = getDestination();
    File tmp = new File(dst.getPath() + ".tmp");
    OutputStream out = new FileOutputStream(tmp);

    try {
        while ((len = in.read(buf)) >= 0) {
            out.write(buf, 0, len);
            //job.status = job.new Installing(total == -1 ? -1 : in.getCount() * 100 / total);
        }
    } catch (IOException e) {
        throw new IOException("Failed to load " + src + " to " + tmp, e);
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }

    if (total != -1 && total != tmp.length()) {
        throw new IOException("Inconsistent file length: expected " + total + " but only got " + tmp.length());
    }

    return tmp;
}

From source file:org.phoenicis.win32.pe.PEReader.java

private RsrcSection readResourceSection(CountingInputStream executableInputStream,
        SectionHeader[] sectionHeaders) throws IOException {
    SectionHeader rsrcSectionHeader = null;
    for (SectionHeader sectionHeader : sectionHeaders) {
        if (".rsrc\u0000\u0000\u0000".equals(new String(sectionHeader.name))) {
            rsrcSectionHeader = sectionHeader;
        }/*w w  w.  j  a v a  2 s . c om*/
    }

    if (rsrcSectionHeader == null) {
        return null;
    }

    long numberToSkip = rsrcSectionHeader.pointerToRawData.getUnsignedValue()
            - executableInputStream.getCount();
    executableInputStream.skip(numberToSkip);
    byte[] rsrcSection = new byte[(int) rsrcSectionHeader.sizeOfRawData.getUnsignedValue()];
    executableInputStream.read(rsrcSection);

    return new RsrcSection(rsrcSection);
}

From source file:org.phoenicis.win32.pe.PEReader.java

private SectionHeader[] readSectionHeaders(CountingInputStream executableInputStream,
        ImageNTHeaders imageNTHeaders) throws IOException {
    final int numberOfSectionHeaders = imageNTHeaders.fileHeader.numberOfSections.getUnsignedValue();
    final SectionHeader[] sectionHeaders = new SectionHeader[numberOfSectionHeaders];
    for (int i = 0; i < numberOfSectionHeaders; i++) {
        byte[] sectionHeaderBytes = new byte[SectionHeader.SECTION_HEADER_SIZE];
        executableInputStream.read(sectionHeaderBytes);
        sectionHeaders[i] = new SectionHeader(sectionHeaderBytes);
    }/*from www.j  a  v a2s . c  om*/
    return sectionHeaders;
}

From source file:org.sead.repositories.reference.RefRepository.java

private JsonNode getItem(String item, File indexFile, CountingInputStream cis, boolean withChildren,
        Long oreFileSize, long curOffset, ArrayList<String> entries, ArrayList<Long> offsets)
        throws JsonParseException, JsonMappingException, IOException {
    log.trace("Getting: " + item + " with starting offset: " + curOffset);

    long curPos = curOffset;

    if ((entries == null) || (offsets == null)) {
        entries = new ArrayList<String>();
        offsets = new ArrayList<Long>();

        FileInputStream fis = new FileInputStream(indexFile);
        JsonFactory f = new MappingJsonFactory();
        JsonParser jp = f.createParser(fis);

        JsonToken current;/*w w w  .j  a  va 2s.c  o m*/
        log.trace("Reading Index file");
        current = jp.nextToken(); // Start object

        while ((current = jp.nextToken()) != null) {
            if (current.equals(JsonToken.FIELD_NAME)) {
                String fName = jp.getText();
                current = jp.nextToken(); // Get to start of
                // value
                long offset = jp.getLongValue();
                log.trace("Adding: " + fName + " : " + offset);
                entries.add(fName);
                offsets.add(offset);
            }
        }
        try {
            fis.close();
        } catch (Exception e) {
            log.debug(e.getMessage());
        }

    }

    byte[] b = null;
    int bytesRead = 0;

    int index = entries.indexOf(item);
    if (index == -1) {
        log.warn(item + " not in index");
    }
    // getSizeEstimateFor(index)
    int estSize;
    if (index < offsets.size() - 1) {
        estSize = (int) (offsets.get(index + 1) - offsets.get(index));
    } else {
        estSize = (int) (oreFileSize - offsets.get(index));
    }
    curPos += skipTo(cis, curPos, offsets.get(index));
    log.trace("Current Pos updated to : " + curPos);
    b = new byte[estSize];
    bytesRead = cis.read(b);
    log.trace("Read " + bytesRead + " bytes");
    if (bytesRead == estSize) {
        log.trace("Read: " + new String(b));
        InputStream is = new ByteArrayInputStream(b);
        // mapper seems to be OK ignoring a last char such as a comma after
        // the object/tree
        ObjectNode resultNode = (ObjectNode) mapper.readTree(is);
        try {
            is.close();
        } catch (Exception e) {
            log.debug(e.getMessage());
        }

        curPos += bytesRead;
        log.trace("curPos: " + curPos + " : count: " + cis.getByteCount());

        log.trace(resultNode.toString());
        if ((resultNode.has("Has Part")) && withChildren) {
            resultNode = getChildren(resultNode, indexFile, cis, oreFileSize, curPos, entries, offsets);
        } else {
            resultNode.remove("aggregates");
        }
        /*
         * if (args[2] != null) { long offset2 = Long.parseLong(args[2]);
         * sbc.position(offset2); b.clear(); sbc.read(b);
         * 
         * InputStream is2 = new ByteArrayInputStream(b.array());
         * 
         * JsonNode node2 = mapper.readTree(is2);
         * System.out.println(node2.toString()); is2.close(); }
         */
        return resultNode;
    } else {
        return null;
    }

}