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;
}
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);
}
}
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;
}
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);
}
}
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));
}
}
Aggregations