Index: src/test/java/org/apache/james/mime4j/message/MessageParserTest.java
===================================================================
--- src/test/java/org/apache/james/mime4j/message/MessageParserTest.java	(revision 677535)
+++ src/test/java/org/apache/james/mime4j/message/MessageParserTest.java	(working copy)
@@ -90,11 +90,8 @@
         
         System.out.println("Parsing " + f.getName());
         
-        InputStream in = new EOLConvertingInputStream(new BufferedInputStream(
-                        new FileInputStream(f))); //, 
+        Message m = new Message(new FileInputStream(f));
         
-        Message m = new Message(in);
-        
         String prefix = f.getName().substring(0, f.getName().length() - 4);
         String xmlFileName = fileName.substring(0, fileName.length() - 4) 
                                     + "_decoded.xml";
@@ -105,7 +102,8 @@
         String expected = null;
         try {
             expected = IOUtils.toString(
-                    new FileInputStream(xmlFileName), "ISO8859-1");
+                    new EOLConvertingInputStream(
+                            new FileInputStream(xmlFileName)), "ISO8859-1");
         } catch (FileNotFoundException ex) {
             writeToFile(result, mime4jFileName);
             fail("Test file not found. Generated the expected result with mime4j prefix: "+ex.getMessage());
Index: src/test/java/org/apache/james/mime4j/MimeBoundaryInputStreamTest.java
===================================================================
--- src/test/java/org/apache/james/mime4j/MimeBoundaryInputStreamTest.java	(revision 677535)
+++ src/test/java/org/apache/james/mime4j/MimeBoundaryInputStreamTest.java	(working copy)
@@ -330,4 +330,17 @@
         assertEquals(-1, instream.readLine(linebuf));
     }
     
+    public void testboundaryLongerThanBuffer() throws IOException {
+        String text = "--looooooooooooooooooooooooooong-boundary\r\n";
+        
+        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes());
+        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 10); 
+        
+        try {
+            new MimeBoundaryInputStream(buffer, "looooooooooooooooooooooooooong-boundary");
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
 }
Index: src/main/java/org/apache/james/mime4j/MimeEntity.java
===================================================================
--- src/main/java/org/apache/james/mime4j/MimeEntity.java	(revision 677535)
+++ src/main/java/org/apache/james/mime4j/MimeEntity.java	(working copy)
@@ -162,10 +162,17 @@
     }
 
     private void createMimeStream() throws IOException {
+        String boundary = body.getBoundary();
+        int bufferSize = 2 * boundary.length();
+        if (bufferSize < 4096) {
+            bufferSize = 4096;
+        }
         if (mimeStream != null) {
-            mimeStream = new MimeBoundaryInputStream(new BufferedLineReaderInputStream(mimeStream, 4 * 1024), body.getBoundary());
+            mimeStream = new MimeBoundaryInputStream(
+                    new BufferedLineReaderInputStream(mimeStream, bufferSize), boundary);
         } else {
-            mimeStream = new MimeBoundaryInputStream(inbuffer, body.getBoundary());
+            inbuffer.ensureCapacity(bufferSize);
+            mimeStream = new MimeBoundaryInputStream(inbuffer, boundary);
         }
         dataStream = new LineReaderInputStreamAdaptor(mimeStream); 
     }
Index: src/main/java/org/apache/james/mime4j/MimeBoundaryInputStream.java
===================================================================
--- src/main/java/org/apache/james/mime4j/MimeBoundaryInputStream.java	(revision 677535)
+++ src/main/java/org/apache/james/mime4j/MimeBoundaryInputStream.java	(working copy)
@@ -49,7 +49,10 @@
     public MimeBoundaryInputStream(BufferedLineReaderInputStream inbuffer, String boundary) 
             throws IOException {
         super(inbuffer);
-        this.buffer = (BufferedLineReaderInputStream) in;
+        if (inbuffer.capacity() <= boundary.length()) {
+            throw new IllegalArgumentException("Boundary is too long");
+        }
+        this.buffer = inbuffer;
         this.eof = false;
         this.limit = -1;
         this.atBoundary = false;
Index: src/main/java/org/apache/james/mime4j/BufferedLineReaderInputStream.java
===================================================================
--- src/main/java/org/apache/james/mime4j/BufferedLineReaderInputStream.java	(revision 677535)
+++ src/main/java/org/apache/james/mime4j/BufferedLineReaderInputStream.java	(working copy)
@@ -28,7 +28,7 @@
  */
 public class BufferedLineReaderInputStream extends LineReaderInputStream {
 
-    private final byte[] buffer;
+    private byte[] buffer;
     
     private int bufpos;
     private int buflen;
@@ -46,6 +46,21 @@
         this.buflen = 0;
     }
     
+    private void expand(int newlen) {
+        byte newbuffer[] = new byte[newlen];
+        int len = this.buflen - this.bufpos;
+        if (len > 0) {
+            System.arraycopy(this.buffer, this.bufpos, newbuffer, this.bufpos, len);
+        }
+        this.buffer = newbuffer;
+    }
+    
+    public void ensureCapacity(int len) {
+        if (len > this.buffer.length) {
+            expand(len);
+        }
+    }
+    
     public int fillBuffer() throws IOException {
         // compact the buffer if necessary
         if (this.bufpos > 0) {
@@ -253,6 +268,10 @@
         return this.buflen - this.bufpos;
     }
     
+    public int capacity() {
+        return this.buffer.length;
+    }
+    
     public int skip(int n) {
         int chunk = Math.min(n, this.buflen - this.bufpos);
         this.bufpos += chunk; 
