Search in sources :

Example 11 with CountingInputStream

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

the class HTTPJavaImpl method readResponse.

/**
 * Reads the response from the URL connection.
 *
 * @param conn
 *            URL from which to read response
 * @param res
 *            {@link SampleResult} to read response into
 * @return response content
 * @exception IOException
 *                if an I/O exception occurs
 */
protected byte[] readResponse(HttpURLConnection conn, SampleResult res) throws IOException {
    BufferedInputStream in;
    final long contentLength = conn.getContentLength();
    if ((contentLength == 0) && OBEY_CONTENT_LENGTH) {
        log.info("Content-Length: 0, not reading http-body");
        res.setResponseHeaders(getResponseHeaders(conn));
        res.latencyEnd();
        return NULL_BA;
    }
    // works OK even if ContentEncoding is null
    boolean gzipped = HTTPConstants.ENCODING_GZIP.equals(conn.getContentEncoding());
    CountingInputStream instream = null;
    try {
        instream = new CountingInputStream(conn.getInputStream());
        if (gzipped) {
            in = new BufferedInputStream(new GZIPInputStream(instream));
        } else {
            in = new BufferedInputStream(instream);
        }
    } catch (IOException e) {
        if (!(e.getCause() instanceof FileNotFoundException)) {
            log.error("readResponse: {}", e.toString());
            Throwable cause = e.getCause();
            if (cause != null) {
                log.error("Cause: {}", cause.toString());
                if (cause instanceof Error) {
                    throw (Error) cause;
                }
            }
        }
        // Normal InputStream is not available
        InputStream errorStream = conn.getErrorStream();
        if (errorStream == null) {
            if (log.isInfoEnabled()) {
                log.info("Error Response Code: {}, Server sent no Errorpage", conn.getResponseCode());
            }
            res.setResponseHeaders(getResponseHeaders(conn));
            res.latencyEnd();
            return NULL_BA;
        }
        if (log.isInfoEnabled()) {
            log.info("Error Response Code: {}", conn.getResponseCode());
        }
        if (gzipped) {
            in = new BufferedInputStream(new GZIPInputStream(errorStream));
        } else {
            in = new BufferedInputStream(errorStream);
        }
    } catch (Exception e) {
        log.error("readResponse: {}", e.toString());
        Throwable cause = e.getCause();
        if (cause != null) {
            log.error("Cause: {}", cause.toString());
            if (cause instanceof Error) {
                throw (Error) cause;
            }
        }
        in = new BufferedInputStream(conn.getErrorStream());
    }
    // N.B. this closes 'in'
    byte[] responseData = readResponse(res, in, contentLength);
    if (instream != null) {
        res.setBodySize(instream.getByteCount());
        instream.close();
    }
    return responseData;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) InputStream(java.io.InputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BindException(java.net.BindException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 12 with CountingInputStream

use of org.apache.commons.io.input.CountingInputStream in project POL-POM-5 by PlayOnLinux.

the class PEReader method parseExecutable.

public PEFile parseExecutable(InputStream inputStream) throws IOException {
    try (CountingInputStream executableInputStream = new CountingInputStream(inputStream)) {
        final ImageDOSHeader imageDOSHeader = readDosHeader(executableInputStream);
        final byte[] realModeStubProgram = readRealModeStubProgram(executableInputStream, imageDOSHeader);
        final ImageNTHeaders imageNTHeaders = readImageNTHeaders(executableInputStream);
        final SectionHeader[] sectionHeaders = readSectionHeaders(executableInputStream, imageNTHeaders);
        final RsrcSection resourceSection = readResourceSection(executableInputStream, sectionHeaders);
        return new PEFile(imageDOSHeader, realModeStubProgram, imageNTHeaders, sectionHeaders, resourceSection);
    }
}
Also used : RsrcSection(org.phoenicis.win32.pe.rsrc.RsrcSection) CountingInputStream(org.apache.commons.io.input.CountingInputStream)

Example 13 with CountingInputStream

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

the class RemoteNodeProcessor method handleResponseBody.

private boolean handleResponseBody(InputStream inputStream, TickBuilder tickBuilder) {
    CountingInputStream countingStream = new CountingInputStream(inputStream);
    DataInputStream input = new DataInputStream(countingStream);
    RemoteMessage message;
    try {
        while ((message = mapper.decode(input)) != null) {
            if (message instanceof TrackStartResponseMessage) {
                handleTrackStartResponse((TrackStartResponseMessage) message);
            } else if (message instanceof TrackFrameDataMessage) {
                handleTrackFrameData((TrackFrameDataMessage) message);
            } else if (message instanceof TrackExceptionMessage) {
                handleTrackException((TrackExceptionMessage) message);
            } else if (message instanceof NodeStatisticsMessage) {
                handleNodeStatistics((NodeStatisticsMessage) message);
            }
        }
    } catch (InterruptedException interruption) {
        log.error("Node {} processing thread was interrupted.", nodeAddress);
        Thread.currentThread().interrupt();
        return false;
    } catch (Throwable e) {
        log.error("Error when processing response from node {}.", nodeAddress, e);
        ExceptionTools.rethrowErrors(e);
    } finally {
        tickBuilder.responseSize = countingStream.getCount();
    }
    return true;
}
Also used : RemoteMessage(com.sedmelluq.discord.lavaplayer.remote.message.RemoteMessage) TrackExceptionMessage(com.sedmelluq.discord.lavaplayer.remote.message.TrackExceptionMessage) TrackStartResponseMessage(com.sedmelluq.discord.lavaplayer.remote.message.TrackStartResponseMessage) CountingInputStream(org.apache.commons.io.input.CountingInputStream) DataInputStream(java.io.DataInputStream) TrackFrameDataMessage(com.sedmelluq.discord.lavaplayer.remote.message.TrackFrameDataMessage) NodeStatisticsMessage(com.sedmelluq.discord.lavaplayer.remote.message.NodeStatisticsMessage)

Example 14 with CountingInputStream

use of org.apache.commons.io.input.CountingInputStream in project POL-POM-5 by PhoenicisOrg.

the class PEReader method parseExecutable.

public PEFile parseExecutable(InputStream inputStream) throws IOException {
    try (CountingInputStream executableInputStream = new CountingInputStream(inputStream)) {
        final ImageDOSHeader imageDOSHeader = readDosHeader(executableInputStream);
        final byte[] realModeStubProgram = readRealModeStubProgram(executableInputStream, imageDOSHeader);
        final ImageNTHeaders imageNTHeaders = readImageNTHeaders(executableInputStream);
        final SectionHeader[] sectionHeaders = readSectionHeaders(executableInputStream, imageNTHeaders);
        final RsrcSection resourceSection = readResourceSection(executableInputStream, sectionHeaders);
        return new PEFile(imageDOSHeader, realModeStubProgram, imageNTHeaders, sectionHeaders, resourceSection);
    }
}
Also used : RsrcSection(org.phoenicis.win32.pe.rsrc.RsrcSection) CountingInputStream(org.apache.commons.io.input.CountingInputStream)

Example 15 with CountingInputStream

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

the class ZipContent method of.

public static ZipContent of(InputStream in) throws IOException {
    java.io.File file = null;
    byte[] inMemory = null;
    int count = 0;
    ByteArrayOutputStream inMemorySpool = new ByteArrayOutputStream(KEEP_IN_MEMORY_THRESHOLD);
    int bytes;
    final byte[] buff = new byte[COPY_BUFFER_SIZE];
    while (count <= KEEP_IN_MEMORY_THRESHOLD && (bytes = in.read(buff)) != -1) {
        inMemorySpool.write(buff, 0, bytes);
        count += bytes;
    }
    InputStream spool;
    if (count > KEEP_IN_MEMORY_THRESHOLD) {
        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);
    }
    try (CountingInputStream compressedDataCounter = new CountingInputStream(spool);
        ZipInputStream zip = new ZipInputStream(compressedDataCounter)) {
        try (CountingInputStream uncompressedDataCounter = new CountingInputStream(zip)) {
            ZipEntry zipEntry;
            while ((zipEntry = zip.getNextEntry()) != null) {
                if (!zipEntry.isDirectory()) {
                    while (uncompressedDataCounter.read(buff) != -1) {
                        long uncompressedBytes = uncompressedDataCounter.getByteCount();
                        if (uncompressedBytes > ZIP_THRESHOLD) {
                            long compressedBytes = compressedDataCounter.getByteCount();
                            if (uncompressedBytes > (ZIP_RATIO * compressedBytes)) {
                                throw new IOException("Zip bomb detected");
                            }
                        }
                    }
                }
            }
        }
        return new ZipContent(inMemory == null ? new DeleteOnCloseFileInputStream(file) : new ByteArrayInputStream(inMemory));
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) CountingInputStream(org.apache.commons.io.input.CountingInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream)

Aggregations

CountingInputStream (org.apache.commons.io.input.CountingInputStream)31 Test (org.junit.Test)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 InputStream (java.io.InputStream)8 Chapter (de.danoeh.antennapod.model.feed.Chapter)7 IOException (java.io.IOException)7 FrameHeader (de.danoeh.antennapod.parser.media.id3.model.FrameHeader)4 BufferedInputStream (java.io.BufferedInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 ZipInputStream (java.util.zip.ZipInputStream)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 RsrcSection (org.phoenicis.win32.pe.rsrc.RsrcSection)2 Logger (org.slf4j.Logger)2 NodeStatisticsMessage (com.sedmelluq.discord.lavaplayer.remote.message.NodeStatisticsMessage)1 RemoteMessage (com.sedmelluq.discord.lavaplayer.remote.message.RemoteMessage)1 TrackExceptionMessage (com.sedmelluq.discord.lavaplayer.remote.message.TrackExceptionMessage)1 TrackFrameDataMessage (com.sedmelluq.discord.lavaplayer.remote.message.TrackFrameDataMessage)1 TrackStartResponseMessage (com.sedmelluq.discord.lavaplayer.remote.message.TrackStartResponseMessage)1