Some more progress in multipart
This commit is contained in:
parent
a700cf2863
commit
e4a5af69db
5 changed files with 153 additions and 69 deletions
|
|
@ -29,25 +29,28 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* TODO: boundry
|
||||
* A stream that is handled as a iterator. The stream will read
|
||||
* until it gets to a boundary and will return -1 (end of stream).
|
||||
* The stream will not return any data until the {@link #next()}
|
||||
* method is called.
|
||||
*
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public class BufferedBoundaryInputStream extends FilterInputStream{
|
||||
/** The size of the buffer in bytes */
|
||||
public static final int DEFAULT_BUF_SIZE = 8192;
|
||||
private static final int DEFAULT_BUF_SIZE = 8192;
|
||||
|
||||
/** The raw buffer */
|
||||
protected byte buffer[];
|
||||
private byte buffer[];
|
||||
/** The current position in the buffer */
|
||||
private int buf_pos = 0;
|
||||
/** The end position of the buffer */
|
||||
protected int buf_end = 0;
|
||||
/** The current position in the buffer */
|
||||
protected int buf_pos = 0;
|
||||
private int buf_end = 0;
|
||||
/** Boundary position, 0< means no boundary found */
|
||||
private int buf_bound_pos;
|
||||
/** The boundary (the delimiter) */
|
||||
protected byte[] boundary;
|
||||
/** Boundary position, 0< means no boundary found */
|
||||
protected int bound_pos;
|
||||
private byte[] boundary;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -67,10 +70,10 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
*/
|
||||
public BufferedBoundaryInputStream(InputStream in, int buf_size){
|
||||
super(in);
|
||||
buf_pos = 0;
|
||||
buf_end = 0;
|
||||
buf_pos = 0;
|
||||
buf_bound_pos = -1;
|
||||
buffer = new byte[buf_size];
|
||||
bound_pos = -1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -109,7 +112,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
return -1; // EOF
|
||||
}
|
||||
|
||||
if(bound_pos == buf_pos)
|
||||
if(buf_bound_pos == buf_pos)
|
||||
return -1; // boundary
|
||||
return buffer[buf_pos++];
|
||||
}
|
||||
|
|
@ -137,7 +140,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
if(fillBuffer() <= 0)
|
||||
return -1; // EOF
|
||||
}
|
||||
if(bound_pos == buf_pos)
|
||||
if(buf_bound_pos == buf_pos)
|
||||
return -1; // boundary
|
||||
|
||||
// The request is larger then the buffer size
|
||||
|
|
@ -146,8 +149,8 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
len = leftover;
|
||||
}
|
||||
// the boundary is in the read range
|
||||
if(buf_pos < bound_pos && bound_pos < buf_pos+len){
|
||||
len = buf_pos - bound_pos;
|
||||
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);
|
||||
|
|
@ -155,11 +158,19 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* @return if there is more data to read
|
||||
*/
|
||||
public boolean isOnBoundary(){
|
||||
return buf_bound_pos == buf_pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Skips over the boundary at the current position in the buffer
|
||||
*/
|
||||
public boolean next(){
|
||||
if(bound_pos == buf_pos){
|
||||
if(buf_bound_pos == buf_pos){
|
||||
buf_pos += boundary.length;
|
||||
searchNextBoundary();
|
||||
return buf_end >= buf_pos;
|
||||
|
|
@ -173,15 +184,15 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
protected void searchNextBoundary(){
|
||||
for(int i=buf_pos; i<buf_end; i++){
|
||||
for(int b=0; b < boundary.length; b++){
|
||||
if(buffer[i] != boundary[b])
|
||||
if(buffer[i+b] != boundary[b])
|
||||
break;
|
||||
else if(b == boundary.length-1){
|
||||
bound_pos = i;
|
||||
buf_bound_pos = i;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
bound_pos = -1;
|
||||
buf_bound_pos = -1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -216,13 +227,6 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
System.arraycopy(b, 0, boundary, 0, b.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return if there is more data to read
|
||||
*/
|
||||
public boolean isOnBoundary(){
|
||||
return bound_pos == buf_pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return an estimate of the number of bytes that can be read (or skipped
|
||||
* over) from this buffered input stream without blocking.
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ public class IOUtil {
|
|||
StringBuilder str = new StringBuilder();
|
||||
BufferedReader in = null;
|
||||
if(reader instanceof BufferedReader)
|
||||
reader = (BufferedReader) reader;
|
||||
in = (BufferedReader) reader;
|
||||
else
|
||||
in = new BufferedReader(reader);
|
||||
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
public boolean hasNext() {
|
||||
try {
|
||||
IOUtil.copyStream(buffIn, new NullWriter());
|
||||
return boundaryIn.next();
|
||||
return boundaryIn.isOnBoundary();
|
||||
} catch (IOException e){
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
|
|
@ -130,14 +130,13 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
|
||||
@Override
|
||||
public MultipartField next() {
|
||||
if (!hasNext())
|
||||
return null;
|
||||
try {
|
||||
HttpHeader header = parser.read();
|
||||
boundaryIn.next();
|
||||
String tmp = buffIn.readLine(); // read the new line after the delimiter
|
||||
if (tmp == null && tmp.equals("--"))
|
||||
if (tmp == null || tmp.equals("--"))
|
||||
return null;
|
||||
|
||||
HttpHeader header = parser.read();
|
||||
String disposition = header.getHeader(HEADER_CONTENT_DISPOSITION);
|
||||
if (disposition != null){
|
||||
HashMap<String,String> map = new HashMap<>();
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ public class MultipartStringField implements MultipartField {
|
|||
|
||||
protected MultipartStringField(Map<String,String> header, BufferedReader in) throws IOException {
|
||||
this.name = header.get("name");
|
||||
value = IOUtil.getContentAsString(in);
|
||||
value = in.readLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue