diff --git a/src/zutil/io/BufferedBoundaryInputStream.java b/src/zutil/io/BufferedBoundaryInputStream.java index 23715ec..b31f551 100755 --- a/src/zutil/io/BufferedBoundaryInputStream.java +++ b/src/zutil/io/BufferedBoundaryInputStream.java @@ -162,7 +162,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{ searchNextBoundary(); } else { // read data until we find the next boundary or get to the end of the stream - while (buf_bound_pos >= 0 || fillBuffer() >= 0) + while (buf_bound_pos < 0 && fillBuffer() >= 0) buf_pos = buf_end; } } @@ -207,7 +207,7 @@ public class BufferedBoundaryInputStream extends FilterInputStream{ * over) from this buffered input stream without blocking. * @exception IOException if an I/O error occurs. */ - public int available() throws IOException { + public int available() { return buf_end - buf_pos; } @@ -259,16 +259,19 @@ public class BufferedBoundaryInputStream extends FilterInputStream{ * Searches for the nearest boundary from the current buffer position */ private void searchNextBoundary(){ - for(int i=buf_pos; i= boundary.length) { + for (int i = buf_pos; i < buf_end; i++) { + for (int b = 0; b < boundary.length; b++) { + if (buffer[i + b] != boundary[b]) + break; + else if (b == boundary.length - 1) { + buf_bound_pos = i; + return; + } + } + } + } buf_bound_pos = -1; } } diff --git a/src/zutil/io/IOUtil.java b/src/zutil/io/IOUtil.java index 86f3c2b..24a29b0 100755 --- a/src/zutil/io/IOUtil.java +++ b/src/zutil/io/IOUtil.java @@ -83,6 +83,7 @@ public class IOUtil { while((line = in.readLine()) != null){ str.append(line).append("\n"); } + str.delete(str.length()-1, str.length()); // remove last new line in.close(); return str.toString(); diff --git a/test/zutil/io/BufferedBoundaryInputStreamTest.java b/test/zutil/io/BufferedBoundaryInputStreamTest.java index fc77972..160c991 100755 --- a/test/zutil/io/BufferedBoundaryInputStreamTest.java +++ b/test/zutil/io/BufferedBoundaryInputStreamTest.java @@ -117,11 +117,17 @@ public class BufferedBoundaryInputStreamTest { } @Test public void readArr_multiCharBoundary() throws IOException { - BufferedBoundaryInputStream in = getBufferedBoundaryInputStream("aaa1234", "1234"); + BufferedBoundaryInputStream in = getBufferedBoundaryInputStream( + "------------------------------83ff53821b7chello------------------------------83ff53821b7c--", + "------------------------------83ff53821b7c"); - assertEquals('a', in.read()); - assertEquals('a', in.read()); - assertEquals('a', in.read()); + assertEquals(-1, in.read()); + assertTrue(in.hasNext()); in.next(); + assertEquals("hello", IOUtil.readContentAsString(in)); + assertEquals(-1, in.read()); + assertTrue(in.hasNext()); in.next(); + assertEquals('-', in.read()); + assertEquals('-', in.read()); assertEquals(-1, in.read()); assertFalse(in.hasNext()); @@ -194,12 +200,14 @@ public class BufferedBoundaryInputStreamTest { @Test public void read_largeData() throws IOException { - String data = "aaaaaaaaaaaa#aa#aaaaaaaaaaaaaaa#"; + String data = "#aaaaaaaaaaaa#aa#aaaaaaaaaaaaaaa#"; StringInputStream inin = new StringInputStream(data); BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin, 10); in.setBoundary("#"); + assertEquals(-1, in.read()); assertTrue(in.hasNext()); + in.next(); for (int i=0; i<12; ++i) assertEquals('a', in.read()); assertEquals(-1, in.read()); diff --git a/test/zutil/net/http/multipart/MultipartParserTest.java b/test/zutil/net/http/multipart/MultipartParserTest.java index a33ffa6..05c45a3 100755 --- a/test/zutil/net/http/multipart/MultipartParserTest.java +++ b/test/zutil/net/http/multipart/MultipartParserTest.java @@ -1,8 +1,10 @@ package zutil.net.http.multipart; import org.junit.Test; +import zutil.io.IOUtil; import zutil.io.StringInputStream; +import java.io.IOException; import java.util.Iterator; import static junit.framework.TestCase.assertFalse; @@ -43,24 +45,25 @@ public class MultipartParserTest { } @Test - public void singleFileUpload() { - String input = + public void singleFileUpload() throws IOException { + String input_start = "------------------------------83ff53821b7c\n" + "Content-Disposition: form-data; name=\"img\"; filename=\"a.png\"\n" + "Content-Type: application/octet-stream\n" + - "\n" + + "\n"; + String input_data = "?PNG\n" + "\n" + - "IHD?wS??iCCPICC Profilex?T?kA?6n??Zk?x?\"IY?hE?6?bk\n" + - "Y?<ߡ)??????9Nyx?+=?Y\"|@5-?\u007FM?S?%?@?H8??qR>?\u05CB??inf???O?????b??N?????~N??>?!?\n" + - "??V?J?p?8?da?sZHO?Ln?}&???wVQ?y?g????E??0\n" + - " ??\n" + - " IDAc????????-IEND?B`?\n" + - "------------------------------83ff53821b7c--"; + "lkuytrewacvbnmloiuytrewrtyuiol,mnbvdc xswertyuioplm cdsertyuioölkjgf\n" + + "kgkfdgfhgfhgkhgvytvjgxslkyöiyfödgjdjhföjhdlgdgjfhföjhföiyföudlögföudöfö\n" + + "ögdljgdjhöfjhfdfsgdfg ryrt dtrd ytfc uhöiugljfkdkhdjgd\n" + + " xx\n" + + " kuykutfytdh ytrd trutrd trxxx"; + String input_end = "\n------------------------------83ff53821b7c--"; MultipartParser parser = new MultipartParser( - new StringInputStream(input), + new StringInputStream(input_start+input_data+input_end), "----------------------------83ff53821b7c", - input.length()); + 0); // Assertions Iterator it = parser.iterator(); @@ -71,6 +74,7 @@ public class MultipartParserTest { assertEquals("img", fileField.getName()); assertEquals("a.png", fileField.getFilename()); assertEquals("application/octet-stream", fileField.getContentType()); + assertEquals(input_data, new String(fileField.getContent())); //assertFalse(it.hasNext()); //TODO: does not work, how to solve this? }