public class MultipartStream extends Object implements Serializable
Low level API for processing file uploads.
This class can be used to process data streams conforming to MIME 'multipart' format as defined in RFC 1867. Arbitrarily large amounts of data in the stream can be processed under constant memory usage.
The format of the stream is defined in the following way:
multipart-body := preamble 1*encapsulation close-delimiter epilogue
encapsulation := delimiter body CRLF
delimiter := "--" boundary CRLF
close-delimiter := "--" boudary "--"
preamble := <ignore>
epilogue := <ignore>
body := header-part CRLF body-part
header-part := 1*header CRLF
header := header-name ":" header-value
header-name := <printable ascii characters except ":">
header-value := <any ascii characters except CR & LF>
body-data := <arbitrary data>
Note that body-data can contain another mulipart entity. There is limited
support for single pass processing of such nested streams. The nested stream
is required to have a boundary token of the same length as
the parent stream (see setBoundary(byte[])).
Here is an example of usage of this class.
try
{
MultipartStream multipartStream = new MultipartStream (input, boundary);
boolean nextPart = multipartStream.skipPreamble ();
OutputStream output;
while (nextPart)
{
String header = multipartStream.readHeaders ();
// process headers
// create some output stream
multipartStream.readBodyData (output);
nextPart = multipartStream.readBoundary ();
}
}
catch (MultipartStream.MalformedStreamException e)
{
// the stream failed to follow required syntax
}
catch (IOException e)
{
// a read or write error occurred
}
| Modifier and Type | Class and Description |
|---|---|
class |
MultipartStream.MultipartItemInputStream
An
InputStream for reading an items contents. |
| Modifier and Type | Field and Description |
|---|---|
static byte |
CR
The Carriage Return ASCII character value.
|
static byte |
DASH
The dash (-) ASCII character value.
|
static int |
HEADER_PART_SIZE_MAX
The maximum length of
header-part that will be processed (20
kilobytes = 10240 bytes.). |
static byte |
LF
The Line Feed ASCII character value.
|
| Constructor and Description |
|---|
MultipartStream(InputStream aIS,
byte[] aBoundary,
int nBufSize,
MultipartProgressNotifier aNotifier)
Constructs a
MultipartStream with a custom size buffer. |
MultipartStream(InputStream aIS,
byte[] aBoundary,
MultipartProgressNotifier aNotifier)
Constructs a
MultipartStream with a default size buffer. |
| Modifier and Type | Method and Description |
|---|---|
MultipartStream.MultipartItemInputStream |
createInputStream()
Creates a new
MultipartStream.MultipartItemInputStream. |
int |
discardBodyData()
Reads
body-data from the current encapsulation
and discards it. |
protected int |
findByte(byte nValue,
int nPos)
Searches for a byte of specified value in the
buffer, starting
at the specified position. |
protected int |
findSeparator()
Searches for the
boundary in the buffer region
delimited by head and tail. |
String |
getHeaderEncoding()
Retrieves the character encoding used when reading the headers of an
individual part.
|
int |
readBodyData()
Reads
body-data from the current encapsulation
and writes its contents into the output Stream. |
boolean |
readBoundary()
Skips a
boundary token, and checks whether more
encapsulations are contained in the stream. |
byte |
readByte()
Reads a byte from the
buffer, and refills it as necessary. |
String |
readHeaders()
Reads the
header-part of the current
encapsulation. |
void |
setBoundary(byte[] aBoundary)
Changes the boundary token used for partitioning the stream.
|
void |
setHeaderEncoding(String sHeaderEncoding)
Specifies the character encoding to be used when reading the headers of
individual parts.
|
boolean |
skipPreamble()
Finds the beginning of the first
encapsulation. |
public static final byte CR
public static final byte LF
public static final byte DASH
public static final int HEADER_PART_SIZE_MAX
header-part that will be processed (20
kilobytes = 10240 bytes.).public MultipartStream(InputStream aIS, byte[] aBoundary, int nBufSize, MultipartProgressNotifier aNotifier)
Constructs a MultipartStream with a custom size buffer.
Note that the buffer must be at least big enough to contain the boundary string, plus 4 characters for CR/LF and double dash, plus at least one byte of data. Too small a buffer size setting will degrade performance.
aIS - The InputStream to serve as a data source.aBoundary - The token used for dividing the stream into
encapsulations.nBufSize - The size of the buffer to be used, in bytes.aNotifier - The notifier, which is used for calling the progress listener, if
any.MultipartStream(InputStream, byte[],MultipartProgressNotifier)public MultipartStream(InputStream aIS, byte[] aBoundary, MultipartProgressNotifier aNotifier)
Constructs a MultipartStream with a default size buffer.
aIS - The InputStream to serve as a data source.aBoundary - The token used for dividing the stream into
encapsulations.aNotifier - An object for calling the progress listener, if any.MultipartStream(InputStream, byte[], int, MultipartProgressNotifier)@Nullable public String getHeaderEncoding()
null, the platform
default encoding is used.public void setHeaderEncoding(@Nullable String sHeaderEncoding)
null, the platform
default encoding is used.sHeaderEncoding - The encoding used to read part headers.public byte readByte()
throws IOException
buffer, and refills it as necessary.IOException - if there is no more data available.public boolean readBoundary()
throws MultipartMalformedStreamException
boundary token, and checks whether more
encapsulations are contained in the stream.true if there are more encapsulations in this stream;
false otherwise.MultipartMalformedStreamException - if the stream ends unexpectedly or fails to follow required syntax.public void setBoundary(@Nonnull byte[] aBoundary) throws MultipartIllegalBoundaryException
Changes the boundary token used for partitioning the stream.
This method allows single pass processing of nested multipart streams.
The boundary token of the nested stream is required to be of
the same length as the boundary token in parent stream.
Restoring the parent stream boundary token after processing of a nested stream is left to the application.
aBoundary - The boundary to be used for parsing of the nested stream.MultipartIllegalBoundaryException - if the boundary has a different length than the one
being currently parsed.public String readHeaders() throws MultipartMalformedStreamException
Reads the header-part of the current
encapsulation.
Headers are returned verbatim to the input stream, including the trailing
CRLF marker. Parsing is left to the application.
header-part of the current encapsulation.MultipartMalformedStreamException - if the stream ends unexpectedly.public int readBodyData()
throws IOException
Reads body-data from the current encapsulation
and writes its contents into the output Stream.
Arbitrary large amounts of data can be processed by this method using a
constant size buffer. (see
MultipartStream(InputStream, byte[], int, MultipartProgressNotifier)
).
MultipartMalformedStreamException - if the stream ends unexpectedly.IOException - if an i/o error occurs.@Nonnull public MultipartStream.MultipartItemInputStream createInputStream()
MultipartStream.MultipartItemInputStream.MultipartStream.MultipartItemInputStream.public int discardBodyData()
throws IOException
Reads body-data from the current encapsulation
and discards it.
Use this method to skip encapsulations you don't need or don't understand.
MultipartMalformedStreamException - if the stream ends unexpectedly.IOException - if an i/o error occurs.public boolean skipPreamble()
throws IOException
encapsulation.true if an encapsulation was found in the
stream.IOException - if an i/o error occurs.@CheckForSigned protected int findByte(byte nValue, int nPos)
buffer, starting
at the specified position.nValue - The value to find.nPos - The starting position for searching.buffer, or -1 if not found.protected int findSeparator()
boundary in the buffer region
delimited by head and tail.buffer, or -1 if not found.Copyright © 2014–2019 Philip Helger. All rights reserved.