Fixed all issues in boundary stream
This commit is contained in:
parent
43dc9a11cb
commit
f2939f819f
9 changed files with 139 additions and 67 deletions
|
|
@ -79,7 +79,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
|
||||
|
||||
/**
|
||||
* @return the next byte in the buffer
|
||||
* @return the next byte from the stream or -1 if EOF or stream is on a boundary
|
||||
*/
|
||||
public final int read() throws IOException{
|
||||
if (fillBuffer() < 0)
|
||||
|
|
@ -94,7 +94,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
* Fills the given array with data from the buffer
|
||||
*
|
||||
* @param b is the array that will be filled
|
||||
* @return the amount of bytes read or -1 if EOF
|
||||
* @return the amount of bytes read or -1 if EOF or stream is on a boundary
|
||||
*/
|
||||
public int read(byte b[]) throws IOException {
|
||||
return read(b, 0, b.length);
|
||||
|
|
@ -106,23 +106,22 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
* @param b is the array that will be filled
|
||||
* @param off is the offset in the array
|
||||
* @param len is the amount to read
|
||||
* @return the amount of bytes read or -1 if EOF
|
||||
* @return the amount of bytes read or -1 if EOF or stream is on a boundary
|
||||
*/
|
||||
public int read(byte b[], int off, int len) throws IOException {
|
||||
if (fillBuffer() < 0)
|
||||
return -1; // EOF
|
||||
if(isOnBoundary())
|
||||
if (isOnBoundary())
|
||||
return -1; // boundary
|
||||
|
||||
// The request is larger then the buffer size
|
||||
int leftover = available();
|
||||
if(len > leftover){
|
||||
if (len > leftover)
|
||||
len = leftover;
|
||||
}
|
||||
|
||||
// the boundary is in the read range
|
||||
if(buf_pos < buf_bound_pos && buf_bound_pos < buf_pos+len){
|
||||
if (buf_pos < buf_bound_pos && buf_bound_pos < buf_pos+len)
|
||||
len = buf_bound_pos - buf_pos;
|
||||
}
|
||||
|
||||
System.arraycopy(buffer, buf_pos, b, off, len);
|
||||
buf_pos += len;
|
||||
|
|
@ -163,8 +162,8 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
searchNextBoundary();
|
||||
}
|
||||
else { // read data until we find the next boundary or get to the end of the stream
|
||||
buf_pos = buf_end;
|
||||
while (buf_bound_pos >= 0 || fillBuffer() < 0);
|
||||
while (buf_bound_pos >= 0 || fillBuffer() >= 0)
|
||||
buf_pos = buf_end;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -191,6 +190,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
*/
|
||||
public void setBoundary(String b){
|
||||
this.boundary = b.getBytes();
|
||||
searchNextBoundary(); // redo the search with the new boundary
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -199,6 +199,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
public void setBoundary(byte[] b){
|
||||
boundary = new byte[b.length];
|
||||
System.arraycopy(b, 0, boundary, 0, b.length);
|
||||
searchNextBoundary(); // redo the search with the new boundary
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -227,10 +228,11 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
* buffer and then fills the buffer with data from
|
||||
* the source stream
|
||||
*
|
||||
* @return the number of new bytes read from the source stream
|
||||
* @return the number of new bytes read from the source stream,
|
||||
* or -1 if the buffer is empty and it is the end of the stream
|
||||
*/
|
||||
private int fillBuffer() throws IOException {
|
||||
int leftover = available();
|
||||
int leftover = this.available();
|
||||
// Do we need to fill the buffer
|
||||
if(buf_pos < buf_end-boundary.length)
|
||||
return 0;
|
||||
|
|
@ -239,17 +241,18 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
return -1; // EOF
|
||||
|
||||
// Move the end of the buffer to the start to not miss any split boundary
|
||||
if (leftover > 0)
|
||||
if (leftover > 0 && buf_pos != 0)
|
||||
System.arraycopy(buffer, buf_pos, buffer, 0, leftover);
|
||||
buf_pos = 0;
|
||||
buf_end = leftover;
|
||||
|
||||
// Copy in new data from the stream
|
||||
int n = super.read(buffer, leftover, buffer.length-leftover );
|
||||
// Reset positions
|
||||
if(n+leftover >= 0) {
|
||||
buf_end = leftover + n;
|
||||
buf_pos = 0;
|
||||
}
|
||||
int n = super.read(buffer, buf_end, buffer.length-buf_end);
|
||||
if (n >= 0)
|
||||
buf_end = buf_end + n;
|
||||
|
||||
searchNextBoundary();
|
||||
return n;
|
||||
return ((n < 0 && this.available() > 0) ? 0 : n);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -88,6 +88,25 @@ public class IOUtil {
|
|||
return str.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads on line terminated by a new line or carriage return from a stream.
|
||||
*
|
||||
* @param in the stream to read from
|
||||
* @return a String that contains one line excluding line terminating
|
||||
* characters, null if it is the end of the stream
|
||||
*/
|
||||
public static String readLine(InputStream in) throws IOException {
|
||||
StringBuilder str = new StringBuilder(80);
|
||||
int c = 0;
|
||||
while ((c=in.read()) >= 0 && (c != '\n') && (c != '\r'))
|
||||
str.append((char)c);
|
||||
if (c == '\r')
|
||||
in.read(); // if the last char is carriage return we assume the next char in the stream will be new line so skip it
|
||||
if (c == -1 && str.length() == 0)
|
||||
return null; // End of the stream
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies all data from one InputStream to another OutputStream.
|
||||
* The streams will not be closed after method has returned.
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ public class StringInputStream extends InputStream{
|
|||
if(buffer.length() == 0)
|
||||
return -1;
|
||||
|
||||
int ret = Character.getNumericValue( buffer.charAt( 0 ));
|
||||
int ret = buffer.charAt( 0 );
|
||||
buffer.deleteCharAt( 0 );
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,12 +39,14 @@ import java.util.Map;
|
|||
public class MultipartFileField implements MultipartField{
|
||||
private String fieldname;
|
||||
private String filename;
|
||||
private String contentType;
|
||||
private InputStream in;
|
||||
|
||||
|
||||
protected MultipartFileField(Map<String,String> header, BufferedReader in) throws IOException {
|
||||
this.fieldname = header.get("fieldname");
|
||||
this.filename = header.get("filename");
|
||||
protected MultipartFileField(String name, String filename, String contentType, BufferedReader in) throws IOException {
|
||||
this.fieldname = name;
|
||||
this.filename = filename;
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -65,6 +67,10 @@ public class MultipartFileField implements MultipartField{
|
|||
return filename;
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
public InputStream getInputStream(){
|
||||
return in;
|
||||
}
|
||||
|
|
@ -79,4 +85,5 @@ public class MultipartFileField implements MultipartField{
|
|||
IOUtil.copyStream(in, out);
|
||||
out.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -104,6 +104,7 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
private BufferedBoundaryInputStream boundaryIn;
|
||||
private BufferedReader buffIn;
|
||||
private HttpHeaderParser parser;
|
||||
private boolean firstIteration;
|
||||
|
||||
|
||||
protected MultiPartIterator(){
|
||||
|
|
@ -113,6 +114,7 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
this.parser.setReadStatusLine(false);
|
||||
|
||||
this.boundaryIn.setBoundary("--"+delimiter);
|
||||
firstIteration = true;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -131,22 +133,35 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
public MultipartField next() {
|
||||
try {
|
||||
boundaryIn.next();
|
||||
if (firstIteration){
|
||||
this.boundaryIn.setBoundary("\n--"+delimiter); // Add new-line to boundary after the first iteration
|
||||
firstIteration = false;
|
||||
}
|
||||
String tmp = buffIn.readLine(); // read the new line after the delimiter
|
||||
if (tmp == null || tmp.equals("--"))
|
||||
return null;
|
||||
|
||||
HttpHeader header = parser.read();
|
||||
String disposition = header.getHeader(HEADER_CONTENT_DISPOSITION);
|
||||
String contentType = header.getHeader("Content-Type");
|
||||
if (contentType != null && !contentType.equalsIgnoreCase("application/octet-stream"))
|
||||
logger.warning("Unsupported ontent-Type: "+contentType);
|
||||
if (disposition != null){
|
||||
HashMap<String,String> map = new HashMap<>();
|
||||
HttpHeaderParser.parseHeaderValue(map, disposition);
|
||||
if (map.containsKey("form-data")){
|
||||
if (map.containsKey("filename")){
|
||||
MultipartFileField field = new MultipartFileField(map, buffIn);
|
||||
MultipartFileField field = new MultipartFileField(
|
||||
map.get("name"),
|
||||
map.get("filename"),
|
||||
contentType,
|
||||
buffIn);
|
||||
return field;
|
||||
}
|
||||
else{
|
||||
MultipartStringField field = new MultipartStringField(map, buffIn);
|
||||
MultipartStringField field = new MultipartStringField(
|
||||
map.get("name"),
|
||||
buffIn);
|
||||
return field;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ public class MultipartStringField implements MultipartField {
|
|||
private String name;
|
||||
private String value;
|
||||
|
||||
protected MultipartStringField(Map<String,String> header, BufferedReader in) throws IOException {
|
||||
this.name = header.get("name");
|
||||
protected MultipartStringField(String name, BufferedReader in) throws IOException {
|
||||
this.name = name;
|
||||
value = in.readLine();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue