multi = new MultipartStream(input, boundary, notifier); } catch (IllegalArgumentException iae) { format("The boundary specified in the %s header is too long", CONTENT_TYPE), iae); multi.setHeaderEncoding(charEncoding);
/** * <p> Reads <code>body-data</code> from the current * <code>encapsulation</code> and discards it. * * <p>Use this method to skip encapsulations you don't need or don't * understand. * * @return The amount of data discarded. * * @throws MalformedStreamException if the stream ends unexpectedly. * @throws IOException if an i/o error occurs. */ public int discardBodyData() throws MalformedStreamException, IOException { return readBodyData(null); }
boolean nextPart; if (skipPreamble) { nextPart = multi.skipPreamble(); } else { nextPart = multi.readBoundary(); multi.setBoundary(boundary); currentFieldName = null; continue; FileItemHeaders headers = getParsedHeaders(multi.readHeaders()); if (currentFieldName == null) { multi.setBoundary(subBoundary); skipPreamble = true; continue; multi.discardBodyData();
/** * Finds the beginning of the first <code>encapsulation</code>. * * @return <code>true</code> if an <code>encapsulation</code> was found in * the stream. * * @throws IOException if an i/o error occurs. */ public boolean skipPreamble() throws IOException { // First delimiter may be not preceeded with a CRLF. System.arraycopy(boundary, 2, boundary, 0, boundary.length - 2); boundaryLength = boundary.length - 2; computeBoundaryTable(); try { // Discard all data up to the delimiter. discardBodyData(); // Read boundary - if succeeded, the stream contains an // encapsulation. return readBoundary(); } catch (MalformedStreamException e) { return false; } finally { // Restore delimiter. System.arraycopy(boundary, 0, boundary, 2, boundary.length - 2); boundaryLength = boundary.length; boundary[0] = CR; boundary[1] = LF; computeBoundaryTable(); } }
marker[0] = readByte(); if (marker[0] == LF) { marker[1] = readByte(); if (arrayequals(marker, STREAM_TERMINATOR, 2)) { nextChunk = false; } else if (arrayequals(marker, FIELD_SEPARATOR, 2)) { nextChunk = true; } else {
String algorithm = null; byte[] payload = null; MultipartStream multipartStream = new MultipartStream(is, boundary.getBytes(StandardCharsets.UTF_8), 4096, null); boolean nextPart = multipartStream.skipPreamble(); while (nextPart) { String header = multipartStream.readHeaders(); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { multipartStream.readBodyData(baos); if (isField(header, "acl")) { nextPart = multipartStream.readBoundary();
private List<LogMessageConverter> extractLogMessages(ResponseEntity<Resource> responseResource) throws IOException { List<LogMessageConverter> parsedLogs = new ArrayList<>(); MediaType contentType = responseResource.getHeaders() .getContentType(); String boundary = contentType.getParameter("boundary"); InputStream responseInputStream = responseResource.getBody() .getInputStream(); try { MultipartStream multipartStream = new MultipartStream(responseInputStream, boundary.getBytes(StandardCharsets.UTF_8), 16 * 1024, null); while (multipartStream.skipPreamble()) { ByteArrayOutputStream part = null; try { part = new ByteArrayOutputStream(); multipartStream.readBodyData(part); parseProtoBufPart(parsedLogs, part); } finally { IOUtils.closeQuietly(part); } } } finally { IOUtils.closeQuietly(responseInputStream); } return parsedLogs; }
@Override protected MultipartFile[] readInternal (Class<? extends MultipartFile[]> clazz, HttpInputMessage inputMessage ) throws IOException { val headers = inputMessage.getHeaders(); if (headers == null) { throw new HttpMessageNotReadableException("There are no headers at all.", inputMessage); } MediaType contentType = headers.getContentType(); if (contentType == null) { throw new HttpMessageNotReadableException("Content-Type is missing.", inputMessage); } val boundaryBytes = getMultiPartBoundary(contentType); MultipartStream multipartStream = new MultipartStream(inputMessage.getBody(), boundaryBytes, bufSize, null); val multiparts = new LinkedList<ByteArrayMultipartFile>(); for (boolean nextPart = multipartStream.skipPreamble(); nextPart; nextPart = multipartStream.readBoundary()) { ByteArrayMultipartFile multiPart; try { multiPart = readMultiPart(multipartStream); } catch (Exception e) { throw new HttpMessageNotReadableException("Multipart body could not be read.", e, inputMessage); } multiparts.add(multiPart); } return multiparts.toArray(new ByteArrayMultipartFile[0]); }
private ByteArrayMultipartFile readMultiPart (MultipartStream multipartStream) throws IOException { val multiPartHeaders = splitIntoKeyValuePairs( multipartStream.readHeaders(), NEWLINES_PATTERN, COLON_PATTERN, false ); val contentDisposition = splitIntoKeyValuePairs( multiPartHeaders.get(CONTENT_DISPOSITION), SEMICOLON_PATTERN, EQUALITY_SIGN_PATTERN, true ); if (!contentDisposition.containsKey("form-data")) { throw new HttpMessageConversionException("Content-Disposition is not of type form-data."); } val bodyStream = new ByteArrayOutputStream(); multipartStream.readBodyData(bodyStream); return new ByteArrayMultipartFile( contentDisposition.get("name"), contentDisposition.get("filename"), multiPartHeaders.get(CONTENT_TYPE), bodyStream.toByteArray() ); }
@Test(expected=IllegalArgumentException.class) public void testSmallBuffer() throws Exception { final String strData = "foobar"; final byte[] contents = strData.getBytes(); InputStream input = new ByteArrayInputStream(contents); byte[] boundary = BOUNDARY_TEXT.getBytes(); int iBufSize = 1; new MultipartStream( input, boundary, iBufSize, new MultipartStream.ProgressNotifier(null, contents.length)); }
/** * Finds the beginning of the first <code>encapsulation</code>. * * @return <code>true</code> if an <code>encapsulation</code> was found in * the stream. * * @throws IOException if an i/o error occurs. */ public boolean skipPreamble() throws IOException { // First delimiter may be not preceeded with a CRLF. System.arraycopy(boundary, 2, boundary, 0, boundary.length - 2); boundaryLength = boundary.length - 2; try { // Discard all data up to the delimiter. discardBodyData(); // Read boundary - if succeeded, the stream contains an // encapsulation. return readBoundary(); } catch (MalformedStreamException e) { return false; } finally { // Restore delimiter. System.arraycopy(boundary, 0, boundary, 2, boundary.length - 2); boundaryLength = boundary.length; boundary[0] = CR; boundary[1] = LF; } }
/** * <p>Reads <code>body-data</code> from the current * <code>encapsulation</code> and writes its contents into the * output <code>Stream</code>. * * <p>Arbitrary large amounts of data can be processed by this * method using a constant size buffer. (see {@link * #MultipartStream(InputStream,byte[],int, * MultipartStream.ProgressNotifier) constructor}). * * @param output The <code>Stream</code> to write data into. May * be null, in which case this method is equivalent * to {@link #discardBodyData()}. * * @return the amount of data written. * * @throws MalformedStreamException if the stream ends unexpectedly. * @throws IOException if an i/o error occurs. */ public int readBodyData(OutputStream output) throws MalformedStreamException, IOException { return (int) Streams.copy(newInputStream(), output, false); // N.B. Streams.copy closes the input stream }
/** * Called for finding the separator. */ private void findSeparator() { pos = MultipartStream.this.findSeparator(); if (pos == -1) { if (tail - head > keepRegion) { pad = keepRegion; } else { pad = tail - head; } } }
while (i < HEADER_SEPARATOR.length) { try { b = readByte(); } catch (FileUploadIOException e) {
MultipartStream multipartStream = new MultipartStream(new ByteArrayInputStream(input), boundary); boolean nextPart = multipartStream.skipPreamble(); String header = multipartStream.readHeaders(); Matcher mN = PATTERN_MULTIPART_PARAM_NAME.matcher(header); Matcher mF = PATTERN_MULTIPART_PARAM_FILENAME.matcher(header); multipartStream.readBodyData(output); paramsPost.put(name,output.toString()); nextPart = multipartStream.readBoundary();
/** * Finds the beginning of the first <code>encapsulation</code>. * * @return <code>true</code> if an <code>encapsulation</code> was found in * the stream. * * @throws IOException if an i/o error occurs. */ public boolean skipPreamble() throws IOException { // First delimiter may be not preceeded with a CRLF. System.arraycopy(boundary, 2, boundary, 0, boundary.length - 2); boundaryLength = boundary.length - 2; computeBoundaryTable(); try { // Discard all data up to the delimiter. discardBodyData(); // Read boundary - if succeeded, the stream contains an // encapsulation. return readBoundary(); } catch (MalformedStreamException e) { return false; } finally { // Restore delimiter. System.arraycopy(boundary, 0, boundary, 2, boundary.length - 2); boundaryLength = boundary.length; boundary[0] = CR; boundary[1] = LF; computeBoundaryTable(); } }
@Test public void testTwoParamConstructor() throws Exception { final String strData = "foobar"; final byte[] contents = strData.getBytes(); InputStream input = new ByteArrayInputStream(contents); byte[] boundary = BOUNDARY_TEXT.getBytes(); MultipartStream ms = new MultipartStream( input, boundary, new MultipartStream.ProgressNotifier(null, contents.length)); assertNotNull(ms); }
/** * Finds the beginning of the first <code>encapsulation</code>. * * @return <code>true</code> if an <code>encapsulation</code> was found in * the stream. * * @throws IOException if an i/o error occurs. */ public boolean skipPreamble() throws IOException { // First delimiter may be not preceeded with a CRLF. System.arraycopy(boundary, 2, boundary, 0, boundary.length - 2); boundaryLength = boundary.length - 2; try { // Discard all data up to the delimiter. discardBodyData(); // Read boundary - if succeded, the stream contains an // encapsulation. return readBoundary(); } catch (MalformedStreamException e) { return false; } finally { // Restore delimiter. System.arraycopy(boundary, 0, boundary, 2, boundary.length - 2); boundaryLength = boundary.length; boundary[0] = CR; boundary[1] = LF; } }
marker[0] = readByte(); if (marker[0] == LF) { marker[1] = readByte(); if (arrayequals(marker, STREAM_TERMINATOR, 2)) { nextChunk = false; } else if (arrayequals(marker, FIELD_SEPARATOR, 2)) { nextChunk = true; } else {
final ItemInputStream itemStream = multi.newInputStream(); InputStream istream = itemStream; if (fileSizeMax != -1) {