📄 multipartboundaryinputstream.java
字号:
{
fillLine();
if (!this.boundaryEncountered)
{
throw new IOException(MESSAGE_INVALID_START);
}
fillLine();
readElementHeaders();
}
private final void readElementHeaders() throws IOException
{
readContentDisposition();
resetCrlf();
boolean hadContentType = readContentType();
resetCrlf();
if (hadContentType)
{
skipCurrentLineIfBlank();
}
}
private final void readContentDisposition() throws IOException
{
String line = readLine();
if (line != null)
{
int colonIndex = line.indexOf(TOKEN_COLON);
if (colonIndex != -1)
{
int firstSemiColonIndex = line.indexOf(TOKEN_SEMI_COLON);
if (firstSemiColonIndex != -1)
{
this.elementContentDisposition = line.substring(colonIndex+1, firstSemiColonIndex).trim();
}
}
else
{
this.elementContentDisposition = DEFAULT_CONTENT_DISPOSITION;
}
this.elementName = parseForParameter(PARAMETER_NAME, line);
this.elementFileName = parseForParameter(PARAMETER_FILENAME, line);
//do platform-specific checking
if (this.elementFileName != null)
{
this.elementFileName = checkAndFixFilename(this.elementFileName);
}
}
}
private final String checkAndFixFilename(String filename)
{
filename = new File(filename).getName();
//check for windows filenames,
//from linux jdk's the entire filepath
//isn't parsed correctly from File.getName()
int colonIndex = filename.indexOf(":");
if (colonIndex == -1) {
//check for Window's SMB server file paths
colonIndex = filename.indexOf("\\\\");
}
int slashIndex = filename.lastIndexOf("\\");
if ((colonIndex > -1) && (slashIndex > -1)) {
//then consider this filename to be a full
//windows filepath, and parse it accordingly
//to retrieve just the file name
filename = filename.substring(slashIndex+1, filename.length());
}
return filename;
}
private final String parseForParameter(String parameter, String parseString)
{
int nameIndex = parseString.indexOf(parameter + TOKEN_EQUALS);
if (nameIndex != -1)
{
nameIndex += parameter.length() + 1;
int startIndex = -1;
int endIndex = -1;
if (parseString.charAt(nameIndex) == TOKEN_QUOTE)
{
startIndex = nameIndex + 1;
int endQuoteIndex = parseString.indexOf(TOKEN_QUOTE, startIndex);
if (endQuoteIndex != -1)
{
endIndex = endQuoteIndex;
}
}
else
{
startIndex = nameIndex;
int spaceIndex = parseString.indexOf(TOKEN_SPACE, startIndex);
if (spaceIndex != -1)
{
endIndex = spaceIndex;
}
else
{
int carriageIndex = parseString.indexOf(CARRIAGE_RETURN, startIndex);
if (carriageIndex != -1)
{
endIndex = carriageIndex;
}
else
{
endIndex = parseString.length();
}
}
}
if ((startIndex != -1) && (endIndex != -1))
{
return parseString.substring(startIndex, endIndex);
}
}
return null;
}
private final boolean readContentType() throws IOException
{
String line = readLine();
if (line != null)
{
//if it's not a blank line (a blank line has just a CRLF)
if (line.length() > 2)
{
this.elementContentType = parseHeaderValue(line);
if (this.elementContentType == null)
{
this.elementContentType = CONTENT_TYPE_APPLICATION_OCTET_STREAM;
}
this.elementCharset = parseForParameter(PARAMETER_CHARSET, line);
return true;
}
//otherwise go to the default content type
this.elementContentType = CONTENT_TYPE_TEXT_PLAIN;
}
return false;
}
private final String parseHeaderValue(String headerLine)
{
//get the index of the colon
int colonIndex = headerLine.indexOf(TOKEN_COLON);
if (colonIndex != -1)
{
int endLineIndex;
//see if there's a semi colon
int semiColonIndex = headerLine.indexOf(TOKEN_SEMI_COLON, colonIndex);
if (semiColonIndex != -1)
{
endLineIndex = semiColonIndex;
}
else
{
//get the index of where the carriage return is, past the colon
endLineIndex = headerLine.indexOf(CARRIAGE_RETURN, colonIndex);
}
if (endLineIndex == -1)
{
//make index the last character in the line
endLineIndex = headerLine.length();
}
//and return a substring representing everything after the "headerName: "
return headerLine.substring(colonIndex+1, endLineIndex).trim();
}
return null;
}
private final void skipCurrentLineIfBlank() throws IOException
{
boolean fill = false;
if (this.lineLength == 1)
{
if (this.line[0] == NEWLINE_BYTE)
{
fill = true;
}
}
else if (this.lineLength == 2)
{
if (equals(this.line, 0, 2, CRLF))
{
fill = true;
}
}
if (fill && !this.endOfStream)
{
fillLine();
}
}
private final void resetCrlf()
{
this.lineHasCarriage = false;
this.lineHasNewline = false;
}
private final void resetStream()
{
this.line = new byte[this.lineSize];
this.lineIndex = 0;
this.lineLength = 0;
this.lineHasCarriage = false;
this.lineHasNewline = false;
this.boundaryEncountered = false;
this.finalBoundaryEncountered = false;
this.endOfStream = false;
this.maxLengthMet = false;
this.bytesRead = 0;
}
private final String readLine() throws IOException
{
String line = null;
if (availableInLine() > 0)
{
line = new String(this.line, 0, this.lineLength);
if (!this.endOfStream)
{
fillLine();
}
}
else
{
if (!this.endOfStream)
{
fillLine();
line = readLine();
}
}
return line;
}
private final int readFromLine() throws IOException
{
if (!this.boundaryEncountered)
{
if (availableInLine() > 0)
{
return this.line[this.lineIndex++];
}
else
{
if (!this.endOfStream)
{
fillLine();
return readFromLine();
}
}
}
return -1;
}
private final int availableInLine()
{
return (this.lineLength - this.lineIndex);
}
private final void fillLine() throws IOException
{
resetLine();
if (!this.finalBoundaryEncountered && !this.endOfStream)
{
fillLineBuffer();
checkForBoundary();
}
}
private final void resetLine()
{
this.lineIndex = 0;
}
private final void fillLineBuffer() throws IOException
{
int read = 0;
int index = 0;
//take care of any CRLF's from the previous read
if (this.lineHasCarriage)
{
this.line[index++] = CARRIAGE_RETURN;
this.lineHasCarriage = false;
}
if (this.lineHasNewline)
{
this.line[index++] = NEWLINE_BYTE;
this.lineHasNewline = false;
}
while ((index < this.line.length) && (!this.maxLengthMet))
{
read = this.inputStream.read();
byteRead();
if ((read != -1) || ((read == -1) && (this.inputStream.available() > 0)) && !this.maxLengthMet)
{
this.line[index++] = (byte) read;
if (read == NEWLINE_BYTE)
{
//flag the newline, but don't put in buffer
this.lineHasNewline= true;
index--;
if (index > 0)
{
//flag the carriage return, but don't put in buffer
if (this.line[index - 1] == CARRIAGE_RETURN)
{
this.lineHasCarriage = true;
index--;
}
}
break;
}
}
else
{
this.endOfStream = true;
break;
}
}
this.lineLength = index;
}
private final void byteRead()
{
this.bytesRead++;
if (this.maxLength > -1)
{
if (this.bytesRead >= this.maxLength)
{
this.maxLengthMet = true;
this.endOfStream = true;
}
}
}
private final void checkForBoundary()
{
this.boundaryEncountered = false;
int actualLength = this.lineLength;
int startIndex;
if ((this.line[0] == CARRIAGE_RETURN) || (this.line[0] == NEWLINE_BYTE))
{
actualLength--;
}
if (this.line[1] == NEWLINE_BYTE)
{
actualLength--;
}
startIndex = (this.lineLength - actualLength);
if (actualLength == this.boundaryBytes.length)
{
if (equals(this.line, startIndex, this.boundaryBytes.length, this.boundaryBytes))
{
this.boundaryEncountered = true;
}
}
else if (actualLength == (this.boundaryBytes.length + 2))
{
if (equals(this.line, startIndex, this.finalBoundaryBytes.length, this.finalBoundaryBytes))
{
this.boundaryEncountered = true;
this.finalBoundaryEncountered = true;
this.endOfStream = true;
}
}
}
/**
* 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]
* @param comp The byte to compare to <code>source</code>
* @param offset The offset to start at in <code>comp</code>
* @param length The length of <code>comp</code> to compare to
* @param source The reference byte array to test for equality
*/
private final boolean equals(byte[] comp, int offset, int length, byte[] source)
{
if ((length != source.length) || (comp.length - offset < length))
{
return false;
}
for (int i = 0; i < length; i++)
{
if (comp[offset+i] != source[i])
{
return false;
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -