@Nonnull public static byte[] readHttpPayload( @Nonnull final InputStream aIS, @Nonnull final IAS2HttpResponseHandler aResponseHandler, @Nonnull final IMessage aMsg) throws IOException { ValueEnforcer.notNull(aIS, "InputStream"); ValueEnforcer.notNull(aResponseHandler, "ResponseHandler"); ValueEnforcer.notNull(aMsg, "Msg"); final DataInputStream aDataIS = new DataInputStream(aIS); // Retrieve the message content byte[] aData = null; final String sContentLength = aMsg.getHeader(CAS2Header.HEADER_CONTENT_LENGTH); if (sContentLength == null) { // No "Content-Length" header present final String sTransferEncoding = aMsg.getHeader(CAS2Header.HEADER_TRANSFER_ENCODING); if (sTransferEncoding != null) { // Remove all whitespaces in the value if (sTransferEncoding.replaceAll("\\s+", "").equalsIgnoreCase("chunked")) { // chunked encoding int nLength = 0; for (; ; ) { // First get hex chunk length; followed by CRLF int nBlocklen = 0; for (; ; ) { int ch = aDataIS.readByte(); if (ch == '\n') break; if (ch >= 'a' && ch <= 'f') ch -= ('a' - 10); else if (ch >= 'A' && ch <= 'F') ch -= ('A' - 10); else if (ch >= '0' && ch <= '9') ch -= '0'; else continue; nBlocklen = (nBlocklen * 16) + ch; } // Zero length is end of chunks if (nBlocklen == 0) break; // Ok, now read new chunk final int nNewlen = nLength + nBlocklen; final byte[] aNewData = new byte[nNewlen]; if (nLength > 0) System.arraycopy(aData, 0, aNewData, 0, nLength); aDataIS.readFully(aNewData, nLength, nBlocklen); aData = aNewData; nLength = nNewlen; // And now the CRLF after the chunk; while (true) { final int n = aDataIS.readByte(); if (n == '\n') break; } } aMsg.setHeader(CAS2Header.HEADER_CONTENT_LENGTH, Integer.toString(nLength)); } else { // No "Content-Length" and unsupported "Transfer-Encoding" sendSimpleHTTPResponse(aResponseHandler, HttpURLConnection.HTTP_LENGTH_REQUIRED); throw new IOException("Transfer-Encoding unimplemented: " + sTransferEncoding); } } else { // No "Content-Length" and no "Transfer-Encoding" sendSimpleHTTPResponse(aResponseHandler, HttpURLConnection.HTTP_LENGTH_REQUIRED); throw new IOException("Content-Length missing"); } } else { // "Content-Length" is present // Receive the transmission's data // XX if a value > 2GB comes in, this will fail!! final int nContentSize = Integer.parseInt(sContentLength); aData = new byte[nContentSize]; aDataIS.readFully(aData); } return aData; }