Index: java/org/apache/catalina/connector/CoyoteReader.java =================================================================== --- java/org/apache/catalina/connector/CoyoteReader.java (revision 638800) +++ java/org/apache/catalina/connector/CoyoteReader.java (working copy) @@ -119,7 +119,7 @@ public boolean markSupported() { - return true; + return ib.markSupported(); } Index: java/org/apache/catalina/connector/InputBuffer.java =================================================================== --- java/org/apache/catalina/connector/InputBuffer.java (revision 638800) +++ java/org/apache/catalina/connector/InputBuffer.java (working copy) @@ -342,22 +342,29 @@ if (!gotEnc) setConverter(); + int nRead = 0; if (bb.getLength() <= 0) { - int nRead = realReadBytes(bb.getBytes(), 0, bb.getBytes().length); - if (nRead < 0) { - return -1; - } + nRead = realReadBytes(bb.getBytes(), 0, bb.getBytes().length); + // Data may be remaining in the buffer of ReadConverter('s super). + // So we must try to convert whether nRead is positive or not. } - if (markPos == -1) { cb.setOffset(0); cb.setEnd(0); } + if((markPos >= 0) && (cb.getLimit()-cb.getEnd()) <= 0){ + // markPos will be reset and the status of cb will be modified + // via cb.flushBuffer(), only if more chars exists. + conv.convert(bb, cb, cb.getLimit()); + }else{ + conv.convert(bb, cb, cb.getLimit()-cb.getEnd()); + } state = CHAR_STATE; - conv.convert(bb, cb, len); - bb.setOffset(bb.getEnd()); + if((nRead < 0) && (cb.getLength()==0)){ + return -1; + } return cb.getLength(); } @@ -426,6 +433,9 @@ public void mark(int readAheadLimit) throws IOException { + if (readAheadLimit < 0){ // See BufferedReader#mark(int) JavaDoc + throw new IllegalArgumentException("readAheadLimit value is negative"); + } if (cb.getLength() <= 0) { cb.setOffset(0); cb.setEnd(0); @@ -451,9 +461,7 @@ throws IOException { if (state == CHAR_STATE) { if (markPos < 0) { - cb.recycle(); - markPos = -1; - throw new IOException(); + throw new IOException("The mark is not effective"); } else { cb.setOffset(markPos); } Index: java/org/apache/tomcat/util/buf/B2CConverter.java =================================================================== --- java/org/apache/tomcat/util/buf/B2CConverter.java (revision 638800) +++ java/org/apache/tomcat/util/buf/B2CConverter.java (working copy) @@ -73,13 +73,15 @@ public void convert( ByteChunk bb, CharChunk cb ) throws IOException { - // Set the ByteChunk as input to the Intermediate reader - convert(bb, cb, cb.getBuffer().length - cb.getEnd()); + convert(bb, cb, cb.getLimit() - cb.getEnd()); } - public void convert( ByteChunk bb, CharChunk cb, int limit) - throws IOException + /** Convert a buffer of bytes into a chars + */ + public void convert( ByteChunk bb, CharChunk cb, int limit) + throws IOException { + // Set the ByteChunk as input to the Intermediate reader iis.setByteChunk( bb ); convert(cb, limit); } @@ -89,10 +91,9 @@ { try { // read from the reader - int count = 0; - while( limit > 0 ) { // conv.ready() ) { - int size = limit < BUFFER_SIZE ? limit : BUFFER_SIZE; - int cnt=conv.read( result, 0, size ); + while( limit > 0 ) { + int size = limit < BUFFER_SIZE ? limit : BUFFER_SIZE; + int cnt=conv.read( result, 0, size ); if( cnt <= 0 ) { // End of stream ! - we may be in a bad state if( debug>0) @@ -105,7 +106,7 @@ // XXX go directly cb.append( result, 0, cnt ); - limit -= cnt; + limit -= cnt; } } catch( IOException ex) { if( debug>0) @@ -193,18 +194,16 @@ * */ final class ReadConvertor extends InputStreamReader { - // stream with flush() and close(). overriden. + private IntermediateInputStream iis; - - // Has a private, internal byte[8192] - + /** Create a converter. */ public ReadConvertor( IntermediateInputStream in, String enc ) throws UnsupportedEncodingException { super( in, enc ); - iis=in; + iis=in; } /** Overriden - will do nothing but reset internal state. @@ -221,9 +220,18 @@ return super.read( cbuf, off, len ); } - /** Reset the buffer + /* + * Reset the buffer */ public final void recycle() { + iis.recycle(); + try{ + // Must clear super's buffer. + while(ready()){ + // InputStreamReader#skip(long) will allocate buffer to skip. + read(); + } + }catch(IOException ioe){} } } @@ -246,15 +254,32 @@ } public final int read(byte cbuf[], int off, int len) throws IOException { - return bc.substract(cbuf, off, len); + if(bc == null){ + return -1; + } + return bc.substract(cbuf, off, len); } public final int read() throws IOException { - return bc.substract(); + if(bc == null){ + return -1; + } + return bc.substract(); } + + public int available() throws IOException { + if(bc == null){ + return 0; + } + return bc.getLength(); + } + // -------------------- Internal methods -------------------- + void recycle(){ + bc = null; + } void setByteChunk( ByteChunk mb ) { bc = mb;