This class encapsulates parsing functionality for RFC1867, multipart/form-data. See MultipartBoundaryInputStreamTest
and MultipartIterator for usage examples.
CARRIAGE_RETURN
private static final byte CARRIAGE_RETURN
CONTENT_TYPE_APPLICATION_OCTET_STREAM
private static final String CONTENT_TYPE_APPLICATION_OCTET_STREAM
CONTENT_TYPE_TEXT_PLAIN
private static final String CONTENT_TYPE_TEXT_PLAIN
CRLF
private static final byte[] CRLF
DEFAULT_CONTENT_DISPOSITION
private static final String DEFAULT_CONTENT_DISPOSITION
DEFAULT_LINE_SIZE
private static final int DEFAULT_LINE_SIZE
DOUBLE_DASH_STRING
private static final String DOUBLE_DASH_STRING
MESSAGE_INVALID_START
private static final String MESSAGE_INVALID_START
NEWLINE_BYTE
private static final byte NEWLINE_BYTE
PARAMETER_CHARSET
private static final String PARAMETER_CHARSET
PARAMETER_FILENAME
private static final String PARAMETER_FILENAME
PARAMETER_NAME
private static final String PARAMETER_NAME
TOKEN_COLON
private static final char TOKEN_COLON
TOKEN_EQUALS
private static final String TOKEN_EQUALS
TOKEN_QUOTE
private static final char TOKEN_QUOTE
TOKEN_SEMI_COLON
private static final char TOKEN_SEMI_COLON
TOKEN_SPACE
private static final char TOKEN_SPACE
boundary
protected String boundary
The boundary.
boundaryBytes
private byte[] boundaryBytes
boundaryEncountered
protected boolean boundaryEncountered
Whether or not the boundary has been encountered.
bytesRead
protected long bytesRead
The total number of bytes read so far.
elementCharset
protected String elementCharset
The character encoding of the element, specified in the element's Content-Type header.
elementContentDisposition
protected String elementContentDisposition
The Content-Disposition for the current form element being read.
elementContentType
protected String elementContentType
The Content-Type of the current form element being read.
elementFileName
protected String elementFileName
The filename of the current form element being read, null
if the current form element is
text data.
elementName
protected String elementName
The name of the current form element being read.
endOfStream
protected boolean endOfStream
Whether or not the end of the stream has been read.
finalBoundaryBytes
private byte[] finalBoundaryBytes
finalBoundaryEncountered
protected boolean finalBoundaryEncountered
Whether or not the final boundary has been encountered.
inputStream
protected InputStream inputStream
The InputStream to read from.
lineHasCarriage
private boolean lineHasCarriage
lineHasNewline
private boolean lineHasNewline
lineIndex
private int lineIndex
lineLength
private int lineLength
lineSize
private int lineSize
maxLength
protected long maxLength
The maximum length in bytes to read from the stream at a time, or -1 for unlimited length.
maxLengthMet
protected boolean maxLengthMet
Whether or not the maximum length has been met.
availableInLine
private final int availableInLine()
byteRead
private final void byteRead()
checkAndFixFilename
private final String checkAndFixFilename(String filename)
checkForBoundary
private final void checkForBoundary()
equals
private final boolean equals(byte[] comp,
int offset,
int length,
byte[] source)
Checks bytes for equality. Two byte arrays are equal if each of their elements are
the same. This method checks comp[offset] with source[0] to source[length-1] with
comp[offset + length - 1]
comp
- The byte to compare to source
offset
- The offset to start at in comp
length
- The length of comp
to compare tosource
- The reference byte array to test for equality
fillLine
private final void fillLine()
throws IOException
fillLineBuffer
private final void fillLineBuffer()
throws IOException
getBytesRead
public long getBytesRead()
getElementCharset
public String getElementCharset()
Gets the character encoding of the current element. The character encoding would have been specified
in the Content-Type header for this element, if it wasn't this is null.
getElementContentDisposition
public String getElementContentDisposition()
Gets the value for the "Content-Dispositio" header for the current multipart element.
Usually "form-data".
getElementContentType
public String getElementContentType()
Gets the "Content-Type" of the current element. If this is a text element,
the content type will probably be "text/plain", otherwise it will be the
content type of the file element.
getElementFileName
public String getElementFileName()
Gets the filename of the current element, which will be null if the current element
isn't a file.
getElementName
public String getElementName()
Gets the name of the current element. The name corresponds to the value of
the "name" attribute of the form element.
getMaxLength
public long getMaxLength()
isBoundaryEncountered
public boolean isBoundaryEncountered()
Returns whether or not the boundary has been encountered while reading data.
isElementFile
public boolean isElementFile()
Gets whether or not the current form element being read is a file.
isEndOfStream
public boolean isEndOfStream()
Whether or not an EOF has been read on the stream.
isFinalBoundaryEncountered
public boolean isFinalBoundaryEncountered()
Returns whether or not the final boundary has been encountered.
isMaxLengthMet
public boolean isMaxLengthMet()
Whether or not the maximum length has been met.
mark
public void mark(int i)
Marks the underlying stream.
parseForParameter
private final String parseForParameter(String parameter,
String parseString)
parseHeaderValue
private final String parseHeaderValue(String headerLine)
read
public int read()
throws IOException
Reads from the stream. Returns -1 if it's the end of the stream or if a boundary is encountered.
read
public int read(byte[] buffer)
throws IOException
read
public int read(byte[] buffer,
int offset,
int length)
throws IOException
readContentDisposition
private final void readContentDisposition()
throws IOException
readContentType
private final boolean readContentType()
throws IOException
readElementHeaders
private final void readElementHeaders()
throws IOException
readFirstElement
private final void readFirstElement()
throws IOException
readFromLine
private final int readFromLine()
throws IOException
readLine
private final String readLine()
throws IOException
reset
public void reset()
throws IOException
Resets the underlying input stream.
resetCrlf
private final void resetCrlf()
resetForNextBoundary
public void resetForNextBoundary()
throws IOException
Resets this stream for use with the next element, to be used after a boundary is encountered.
resetLine
private final void resetLine()
resetStream
private final void resetStream()
setBoundary
public void setBoundary(String boundary)
Sets the boundary that terminates the data for the stream, after adding the prefix "--"
setInputStream
public void setInputStream(InputStream stream)
throws IOException
Sets the input stream used to read multipart data. For efficiency purposes, make sure that the stream
you set on this class is buffered. The way this class reads lines is that it continually calls the read()
method until it reaches a newline character. That would be terrible if you were to set a socket's input stream
here, but not as bad on a buffered stream.
setLineSize
public void setLineSize(int size)
setMaxLength
public void setMaxLength(long maxLength)
Set the maximum length in bytes to read, or -1 for an unlimited length.
skipCurrentLineIfBlank
private final void skipCurrentLineIfBlank()
throws IOException