while (stillMorePartsToRead) {
try {
int firstCharacter = inputStream.read();
if (firstCharacter == -1) {
LOGGER.error("Could not read the first of two consecutive characters after inter boundary separator.");
throw new HttpException(HttpStatusCode.STATUS_400_BAD_REQUEST, "");
}
int secondCharacter = inputStream.read();
if (secondCharacter == -1) {
LOGGER.error(String.format("Could not read the second of two consecutive characters after inter boundary separator. Already read: %s.", new String(new int[]{firstCharacter}, 0, 1)));
throw new HttpException(HttpStatusCode.STATUS_400_BAD_REQUEST, "");
}
if (isCR(firstCharacter) && isLF(secondCharacter)) {
stillMorePartsToRead = true;
} else if (isDash(firstCharacter) && isDash(secondCharacter)) {
stillMorePartsToRead = false;
break;
} else {
LOGGER.error(String.format("Read the two consecutive characters after inter boundary separator. Could have been either CRLF or DASHDASH but was: %s (%d %d).", new String(new int[]{firstCharacter, secondCharacter}, 0, 2), firstCharacter, secondCharacter));
throw new HttpException(HttpStatusCode.STATUS_400_BAD_REQUEST, "");
}
}
catch (IOException ioex) {
LOGGER.error("Tried to read the two consecutive characters after inter boundary separator. Could have been either CRLF or DASHDASH.", ioex);
throw new HttpException(HttpStatusCode.STATUS_400_BAD_REQUEST, ioex.getMessage());
}
IHttpHeadersParser headersParser = new HttpHeadersParser();
IHttpHeaders headers = headersParser.parse(inputStream);
ContentDispositionHttpHeader partContentDisposition = (ContentDispositionHttpHeader) headers.getHeader("Content-Disposition");
if (partContentDisposition == null
|| !partContentDisposition.getDispositionType().getType().equalsIgnoreCase("form-data")
|| !partContentDisposition.getDispositionType().getParameters().containsName("name")) {
throw new HttpException(HttpStatusCode.STATUS_400_BAD_REQUEST,
String.format("Bad Content-Disposition: %s.", partContentDisposition.getRawValue()));
}
ContentTypeHttpHeader partContentType = (ContentTypeHttpHeader) headers.getHeader("Content-Type");
BoundaryDelimitedInputStream bdis = new BoundaryDelimitedInputStream(inputStream, boundary);