org.apache.struts.upload

Class MultipartBoundaryInputStream

public class MultipartBoundaryInputStream extends InputStream

Deprecated: Use the Commons FileUpload based multipart handler instead. This class will be removed after Struts 1.2.

This class encapsulates parsing functionality for RFC1867, multipart/form-data. See MultipartBoundaryInputStreamTest and MultipartIterator for usage examples.
Field Summary
protected Stringboundary
The boundary.
byte[]boundaryBytes
protected booleanboundaryEncountered
Whether or not the boundary has been encountered.
protected longbytesRead
The total number of bytes read so far.
static byteCARRIAGE_RETURN
static StringCONTENT_TYPE_APPLICATION_OCTET_STREAM
static StringCONTENT_TYPE_TEXT_PLAIN
static byte[]CRLF
static StringDEFAULT_CONTENT_DISPOSITION
static intDEFAULT_LINE_SIZE
static StringDOUBLE_DASH_STRING
protected StringelementCharset
The character encoding of the element, specified in the element's Content-Type header.
protected StringelementContentDisposition
The Content-Disposition for the current form element being read.
protected StringelementContentType
The Content-Type of the current form element being read.
protected StringelementFileName
The filename of the current form element being read, null if the current form element is text data.
protected StringelementName
The name of the current form element being read.
protected booleanendOfStream
Whether or not the end of the stream has been read.
byte[]finalBoundaryBytes
protected booleanfinalBoundaryEncountered
Whether or not the final boundary has been encountered.
protected InputStreaminputStream
The InputStream to read from.
byte[]line
booleanlineHasCarriage
booleanlineHasNewline
intlineIndex
intlineLength
intlineSize
protected longmaxLength
The maximum length in bytes to read from the stream at a time, or -1 for unlimited length.
protected booleanmaxLengthMet
Whether or not the maximum length has been met.
static StringMESSAGE_INVALID_START
static byteNEWLINE_BYTE
static StringPARAMETER_CHARSET
static StringPARAMETER_FILENAME
static StringPARAMETER_NAME
static charTOKEN_COLON
static StringTOKEN_EQUALS
static charTOKEN_QUOTE
static charTOKEN_SEMI_COLON
static charTOKEN_SPACE
Constructor Summary
MultipartBoundaryInputStream()
Method Summary
intavailableInLine()
voidbyteRead()
StringcheckAndFixFilename(String filename)
voidcheckForBoundary()
booleanequals(byte[] comp, int offset, int length, byte[] source)
Checks bytes for equality.
voidfillLine()
voidfillLineBuffer()
longgetBytesRead()
StringgetElementCharset()
Gets the character encoding of the current element.
StringgetElementContentDisposition()
Gets the value for the "Content-Dispositio" header for the current multipart element.
StringgetElementContentType()
Gets the "Content-Type" of the current element.
StringgetElementFileName()
Gets the filename of the current element, which will be null if the current element isn't a file.
StringgetElementName()
Gets the name of the current element.
longgetMaxLength()
booleanisBoundaryEncountered()
Returns whether or not the boundary has been encountered while reading data.
booleanisElementFile()
Gets whether or not the current form element being read is a file.
booleanisEndOfStream()
Whether or not an EOF has been read on the stream.
booleanisFinalBoundaryEncountered()
Returns whether or not the final boundary has been encountered.
booleanisMaxLengthMet()
Whether or not the maximum length has been met.
voidmark(int i)
Marks the underlying stream.
StringparseForParameter(String parameter, String parseString)
StringparseHeaderValue(String headerLine)
intread()
Reads from the stream.
intread(byte[] buffer)
intread(byte[] buffer, int offset, int length)
voidreadContentDisposition()
booleanreadContentType()
voidreadElementHeaders()
voidreadFirstElement()
intreadFromLine()
StringreadLine()
voidreset()
Resets the underlying input stream.
voidresetCrlf()
voidresetForNextBoundary()
Resets this stream for use with the next element, to be used after a boundary is encountered.
voidresetLine()
voidresetStream()
voidsetBoundary(String boundary)
Sets the boundary that terminates the data for the stream, after adding the prefix "--"
voidsetInputStream(InputStream stream)
Sets the input stream used to read multipart data.
voidsetLineSize(int size)
voidsetMaxLength(long maxLength)
Set the maximum length in bytes to read, or -1 for an unlimited length.
voidskipCurrentLineIfBlank()

Field Detail

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.

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

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.

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

Constructor Detail

MultipartBoundaryInputStream

public MultipartBoundaryInputStream()

Method Detail

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()

fillLineBuffer

private final void fillLineBuffer()

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()
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)

read

public int read(byte[] buffer, int offset, int length)

readContentDisposition

private final void readContentDisposition()

readContentType

private final boolean readContentType()

readElementHeaders

private final void readElementHeaders()

readFirstElement

private final void readFirstElement()

readFromLine

private final int readFromLine()

readLine

private final String readLine()

reset

public void reset()
Resets the underlying input stream.

resetCrlf

private final void resetCrlf()

resetForNextBoundary

public void resetForNextBoundary()
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)
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()
Copyright B) 2000-2008 - The Apache Software Foundation