org.apache.struts.upload

Class MultipartBoundaryInputStream


public class MultipartBoundaryInputStream
extends InputStream

This class encapsulates parsing functionality for RFC1867, multipart/form-data. See MultipartBoundaryInputStreamTest and MultipartIterator for usage examples.

Field Summary

private static byte
CARRIAGE_RETURN
private static String
CONTENT_TYPE_APPLICATION_OCTET_STREAM
private static String
CONTENT_TYPE_TEXT_PLAIN
private static byte[]
CRLF
private static String
DEFAULT_CONTENT_DISPOSITION
private static int
DEFAULT_LINE_SIZE
private static String
DOUBLE_DASH_STRING
private static String
MESSAGE_INVALID_START
private static byte
NEWLINE_BYTE
private static String
PARAMETER_CHARSET
private static String
PARAMETER_FILENAME
private static String
PARAMETER_NAME
private static char
TOKEN_COLON
private static String
TOKEN_EQUALS
private static char
TOKEN_QUOTE
private static char
TOKEN_SEMI_COLON
private static char
TOKEN_SPACE
protected String
boundary
The boundary.
private byte[]
boundaryBytes
protected boolean
boundaryEncountered
Whether or not the boundary has been encountered.
protected long
bytesRead
The total number of bytes read so far.
protected String
elementCharset
The character encoding of the element, specified in the element's Content-Type header.
protected String
elementContentDisposition
The Content-Disposition for the current form element being read.
protected String
elementContentType
The Content-Type of the current form element being read.
protected String
elementFileName
The filename of the current form element being read, null if the current form element is text data.
protected String
elementName
The name of the current form element being read.
protected boolean
endOfStream
Whether or not the end of the stream has been read.
private byte[]
finalBoundaryBytes
protected boolean
finalBoundaryEncountered
Whether or not the final boundary has been encountered.
protected InputStream
inputStream
The InputStream to read from.
private byte[]
line
private boolean
lineHasCarriage
private boolean
lineHasNewline
private int
lineIndex
private int
lineLength
private int
lineSize
protected long
maxLength
The maximum length in bytes to read from the stream at a time, or -1 for unlimited length.
protected boolean
maxLengthMet
Whether or not the maximum length has been met.

Constructor Summary

MultipartBoundaryInputStream()

Method Summary

private int
availableInLine()
private void
byteRead()
private String
checkAndFixFilename(String filename)
private void
checkForBoundary()
private boolean
equals(byte[] comp, int offset, int length, byte[] source)
Checks bytes for equality.
private void
fillLine()
private void
fillLineBuffer()
long
getBytesRead()
String
getElementCharset()
Gets the character encoding of the current element.
String
getElementContentDisposition()
Gets the value for the "Content-Dispositio" header for the current multipart element.
String
getElementContentType()
Gets the "Content-Type" of the current element.
String
getElementFileName()
Gets the filename of the current element, which will be null if the current element isn't a file.
String
getElementName()
Gets the name of the current element.
long
getMaxLength()
boolean
isBoundaryEncountered()
Returns whether or not the boundary has been encountered while reading data.
boolean
isElementFile()
Gets whether or not the current form element being read is a file.
boolean
isEndOfStream()
Whether or not an EOF has been read on the stream.
boolean
isFinalBoundaryEncountered()
Returns whether or not the final boundary has been encountered.
boolean
isMaxLengthMet()
Whether or not the maximum length has been met.
void
mark(int i)
Marks the underlying stream.
private String
parseForParameter(String parameter, String parseString)
private String
parseHeaderValue(String headerLine)
int
read()
Reads from the stream.
int
read(byte[] buffer)
int
read(byte[] buffer, int offset, int length)
private void
readContentDisposition()
private boolean
readContentType()
private void
readElementHeaders()
private void
readFirstElement()
private int
readFromLine()
private String
readLine()
void
reset()
Resets the underlying input stream.
private void
resetCrlf()
void
resetForNextBoundary()
Resets this stream for use with the next element, to be used after a boundary is encountered.
private void
resetLine()
private void
resetStream()
void
setBoundary(String boundary)
Sets the boundary that terminates the data for the stream, after adding the prefix "--"
void
setInputStream(InputStream stream)
Sets the input stream used to read multipart data.
void
setLineSize(int size)
void
setMaxLength(long maxLength)
Set the maximum length in bytes to read, or -1 for an unlimited length.
private void
skipCurrentLineIfBlank()

Field Details

CARRIAGE_RETURN

private static final byte CARRIAGE_RETURN
Field Value:
13

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
Field Value:
4096

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
Field Value:
10

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
Field Value:
':'

TOKEN_EQUALS

private static final String TOKEN_EQUALS

TOKEN_QUOTE

private static final char TOKEN_QUOTE
Field Value:
'\'

TOKEN_SEMI_COLON

private static final char TOKEN_SEMI_COLON
Field Value:
';'

TOKEN_SPACE

private static final char TOKEN_SPACE
Field Value:
' '

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.

line

private byte[] line

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.

Constructor Details

MultipartBoundaryInputStream

public MultipartBoundaryInputStream()

Method Details

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]
Parameters:
comp - The byte to compare to source
offset - The offset to start at in comp
length - The length of comp to compare to
source - 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

Copyright B) 2000-2007 - The Apache Software Foundation