* @param boundaryStream A <code>BoundaryDelimitedInputStream</code> that wraps a POST request.
* @return A <code>MultipartHeader</code> object wrapping the extracted header or <code>null</code> if the stream is closed.
* @throws IOException if an error occurs while attempting to read the headers from the current inside stream.
*/
private MultipartHeader readHeader(final BoundaryDelimitedInputStream boundaryStream) throws IOException {
MultipartHeader currentHeader = null;
if (!boundaryStream.isOuterStreamClosed()) {
int separatorCounter = 0;
byte[] separatorBuffer = new byte[POST_HEADER_BOUNDARY.length];
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
// Read the header until the post header separator sequence is found.
while (separatorCounter < POST_HEADER_BOUNDARY.length) {
int current = boundaryStream.read();
if (current == POST_HEADER_BOUNDARY[separatorCounter]) {
separatorBuffer[separatorCounter] = (byte) current;
separatorCounter++;
} else {
if (separatorCounter > 0) {
bos.write(separatorBuffer, 0, separatorCounter);
}
bos.write(current);
separatorCounter = 0;
}
}
} finally {
bos.flush();
bos.close();
}
// Create the header from the read data
currentHeader = new MultipartHeader(bos.toByteArray());
}
return currentHeader;
}