use of org.apache.http.MalformedChunkCodingException in project robovm by robovm.
the class ChunkedInputStream method getChunkSize.
/**
* Expects the stream to start with a chunksize in hex with optional
* comments after a semicolon. The line must end with a CRLF: "a3; some
* comment\r\n" Positions the stream at the start of the next line.
*
* @param in The new input stream.
* @param required <tt>true<tt/> if a valid chunk must be present,
* <tt>false<tt/> otherwise.
*
* @return the chunk size as integer
*
* @throws IOException when the chunk size could not be parsed
*/
private int getChunkSize() throws IOException {
// skip CRLF
if (!bof) {
int cr = in.read();
int lf = in.read();
if ((cr != HTTP.CR) || (lf != HTTP.LF)) {
throw new MalformedChunkCodingException("CRLF expected at end of chunk");
}
}
//parse data
this.buffer.clear();
int i = this.in.readLine(this.buffer);
if (i == -1) {
throw new MalformedChunkCodingException("Chunked stream ended unexpectedly");
}
int separator = this.buffer.indexOf(';');
if (separator < 0) {
separator = this.buffer.length();
}
try {
return Integer.parseInt(this.buffer.substringTrimmed(0, separator), 16);
} catch (NumberFormatException e) {
throw new MalformedChunkCodingException("Bad chunk header");
}
}
use of org.apache.http.MalformedChunkCodingException in project XobotOS by xamarin.
the class ChunkedInputStream method getChunkSize.
/**
* Expects the stream to start with a chunksize in hex with optional
* comments after a semicolon. The line must end with a CRLF: "a3; some
* comment\r\n" Positions the stream at the start of the next line.
*
* @param in The new input stream.
* @param required <tt>true<tt/> if a valid chunk must be present,
* <tt>false<tt/> otherwise.
*
* @return the chunk size as integer
*
* @throws IOException when the chunk size could not be parsed
*/
private int getChunkSize() throws IOException {
// skip CRLF
if (!bof) {
int cr = in.read();
int lf = in.read();
if ((cr != HTTP.CR) || (lf != HTTP.LF)) {
throw new MalformedChunkCodingException("CRLF expected at end of chunk");
}
}
//parse data
this.buffer.clear();
int i = this.in.readLine(this.buffer);
if (i == -1) {
throw new MalformedChunkCodingException("Chunked stream ended unexpectedly");
}
int separator = this.buffer.indexOf(';');
if (separator < 0) {
separator = this.buffer.length();
}
try {
return Integer.parseInt(this.buffer.substringTrimmed(0, separator), 16);
} catch (NumberFormatException e) {
throw new MalformedChunkCodingException("Bad chunk header");
}
}
use of org.apache.http.MalformedChunkCodingException in project wso2-synapse by wso2.
the class Pipe method copyAndProduce.
/**
* Same as {@link Pipe#produce(org.apache.http.nio.ContentDecoder)} and gives a copy of data produced
*
* @param decoder decoder to read bytes from the underlying stream
* @return bytes of data read (consumed)
* @throws IOException if an error occurs while reading data
*/
public ByteBuffer copyAndProduce(final ContentDecoder decoder) throws IOException {
if (producerIoControl == null) {
throw new IllegalStateException("Producer cannot be null when calling produce");
}
lock.lock();
try {
ByteBuffer duplicate = null;
setInputMode(buffer);
int bytesRead;
try {
// clone original buffer
ByteBuffer originalBuffer = buffer.getByteBuffer();
bytesRead = decoder.read(originalBuffer);
duplicate = originalBuffer.duplicate();
// replicate positions of original buffer in duplicated buffer
int position = originalBuffer.position();
duplicate.limit(position);
if (bytesRead > 0) {
duplicate.position(position - bytesRead);
}
} catch (MalformedChunkCodingException ignore) {
// we assume that this is a truncated chunk, hence simply ignore the exception
// https://issues.apache.org/jira/browse/HTTPCORE-195
// we should add the EoF character
buffer.putInt(-1);
duplicate.putInt(-1);
}
producePostActions(decoder);
return duplicate;
} finally {
lock.unlock();
}
}
use of org.apache.http.MalformedChunkCodingException in project platform_external_apache-http by android.
the class ChunkedInputStream method getChunkSize.
/**
* Expects the stream to start with a chunksize in hex with optional
* comments after a semicolon. The line must end with a CRLF: "a3; some
* comment\r\n" Positions the stream at the start of the next line.
*
* @param in The new input stream.
* @param required <tt>true<tt/> if a valid chunk must be present,
* <tt>false<tt/> otherwise.
*
* @return the chunk size as integer
*
* @throws IOException when the chunk size could not be parsed
*/
private int getChunkSize() throws IOException {
// skip CRLF
if (!bof) {
int cr = in.read();
int lf = in.read();
if ((cr != HTTP.CR) || (lf != HTTP.LF)) {
throw new MalformedChunkCodingException("CRLF expected at end of chunk");
}
}
// parse data
this.buffer.clear();
int i = this.in.readLine(this.buffer);
if (i == -1) {
throw new MalformedChunkCodingException("Chunked stream ended unexpectedly");
}
int separator = this.buffer.indexOf(';');
if (separator < 0) {
separator = this.buffer.length();
}
try {
return Integer.parseInt(this.buffer.substringTrimmed(0, separator), 16);
} catch (NumberFormatException e) {
throw new MalformedChunkCodingException("Bad chunk header");
}
}
use of org.apache.http.MalformedChunkCodingException in project coastal-hazards by USGS-CIDA.
the class FetchAndUnzipProcess method unzipToDir.
File unzipToDir(ZipInputStream zipStream, File zipDir) {
// return only one path back
File unzippedFile = null;
try {
ZipEntry entry;
if (null != (entry = zipStream.getNextEntry())) {
if (!entry.isDirectory()) {
String entryFileName = entry.getName();
String safeFileName = makeSafeFileName();
unzippedFile = new File(zipDir, safeFileName);
String entryFileAbsolutePath = unzippedFile.getAbsolutePath();
LOGGER.fine("unzipping '" + entryFileName + "' to " + entryFileAbsolutePath);
new File(unzippedFile.getParent()).mkdirs();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(unzippedFile);
long start = System.nanoTime();
LOGGER.info("Starting to unzip the zipped stream to local disk");
IOUtils.copyLarge(zipStream, fos);
long end = System.nanoTime();
// duration in ms
long duration = (end - start) / 1000000;
LOGGER.info("Finished unzipping the zipped stream to local disk. Duration: " + duration + " ms");
} catch (FileNotFoundException ex) {
throw new ProcessException("Error finding file '" + entryFileAbsolutePath + "'.", ex);
} catch (MalformedChunkCodingException ex) {
throw new ProcessException("Error writing file '" + entryFileName + "' to '" + entryFileAbsolutePath + "'. This can happen if the zip file contains multiple files. Only one file is allowed.", ex);
} catch (IOException ex) {
throw new ProcessException("Error writing file '" + entryFileName + "' to '" + entryFileAbsolutePath + "'.", ex);
} finally {
IOUtils.closeQuietly(fos);
}
}
}
} catch (IOException ex) {
throw new ProcessException("error getting next entry in zip file", ex);
} finally {
IOUtils.closeQuietly(zipStream);
}
return unzippedFile;
}
Aggregations