Troubleshooting multipart

This commit is contained in:
Ziver Koc 2016-07-10 22:09:56 +02:00
parent 077963ae28
commit a700cf2863
4 changed files with 34 additions and 11 deletions

View file

@ -36,7 +36,7 @@ import java.io.InputStream;
*/
public class BufferedBoundaryInputStream extends FilterInputStream{
/** The size of the buffer in bytes */
public static final int DEFAULT_BUF_SIZE = 64*1024;
public static final int DEFAULT_BUF_SIZE = 8192;
/** The raw buffer */
protected byte buffer[];
@ -133,16 +133,15 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
* @return the amount of bytes read or -1 if EOF
*/
public int read(byte b[], int off, int len) throws IOException {
if(bound_pos == buf_pos)
return -1; // boundary
if(buf_pos >= buf_end-boundary.length) {
if(fillBuffer() <= 0)
return -1; // EOF
}
int leftover = buf_end - buf_pos;
if(bound_pos == buf_pos)
return -1; // boundary
// The request is larger then the buffer size
int leftover = buf_end - buf_pos;
if(len > leftover){
len = leftover;
}
@ -159,11 +158,13 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
/**
* Skips over the boundary at the current position in the buffer
*/
public void next(){
public boolean next(){
if(bound_pos == buf_pos){
buf_pos += boundary.length;
searchNextBoundary();
return buf_end >= buf_pos;
}
return true;
}
/**
@ -218,7 +219,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
/**
* @return if there is more data to read
*/
public boolean hasNext(){
public boolean isOnBoundary(){
return bound_pos == buf_pos;
}

View file

@ -43,7 +43,7 @@ public class IOUtil {
*/
public static byte[] getContent(InputStream stream) throws IOException{
DynamicByteArrayStream dyn_buff = new DynamicByteArrayStream();
byte[] buff = new byte[256];
byte[] buff = new byte[8192];
int len = 0;
while((len = stream.read(buff)) != -1){
dyn_buff.append(buff, 0, len);

View file

@ -120,7 +120,7 @@ public class MultipartParser implements Iterable<MultipartField>{
public boolean hasNext() {
try {
IOUtil.copyStream(buffIn, new NullWriter());
return boundaryIn.hasNext();
return boundaryIn.next();
} catch (IOException e){
logger.log(Level.SEVERE, null, e);
}
@ -133,8 +133,11 @@ public class MultipartParser implements Iterable<MultipartField>{
if (!hasNext())
return null;
try {
boundaryIn.next(); // Skip current boundary
HttpHeader header = parser.read();
String tmp = buffIn.readLine(); // read the new line after the delimiter
if (tmp == null && tmp.equals("--"))
return null;
String disposition = header.getHeader(HEADER_CONTENT_DISPOSITION);
if (disposition != null){
HashMap<String,String> map = new HashMap<>();

View file

@ -68,6 +68,12 @@ public class BufferedBoundaryInputStreamTest {
in.setBoundary("#");
assertEquals(-1, in.read());
inin = new StringInputStream("#aaa");
in = new BufferedBoundaryInputStream(inin);
in.setBoundary("#");
assertEquals(-1, in.read(new byte[10], 0, 10));
}
@Test
@ -82,7 +88,7 @@ public class BufferedBoundaryInputStreamTest {
assertEquals(-1, in.read());
assertEquals(-1, in.read());
in.next();
if(!in.hasNext())
if(!in.isOnBoundary())
break;
}
assertEquals(35, n);
@ -103,4 +109,17 @@ public class BufferedBoundaryInputStreamTest {
assertEquals(data, output.toString());
}
@Test
public void next() throws IOException {
StringInputStream inin = new StringInputStream("a#a#");
BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin);
in.setBoundary("#");
assertEquals('a', in.read());
assertEquals(-1, in.read());
assertEquals(true, in.next());
assertEquals('a', in.read());
assertEquals(-1, in.read());
assertEquals(false, in.next());
}
}