remm 2003/01/05 03:22:16 Modified: util/java/org/apache/tomcat/util/buf ByteChunk.java CharChunk.java Log: - Add input support to ByteChunk and CharChunk. - I don't know if the new method names are well chosen. Revision Changes Path 1.12 +65 -8 jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/buf/ByteChunk.java Index: ByteChunk.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/buf/ByteChunk.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- ByteChunk.java 23 Apr 2002 20:07:57 -0000 1.11 +++ ByteChunk.java 5 Jan 2003 11:22:16 -0000 1.12 @@ -85,18 +85,31 @@ * * The buffer can be modified and used for both input and output. * - * @author [EMAIL PROTECTED] - * @author James Todd [[EMAIL PROTECTED]] + * @author [EMAIL PROTECTED] + * @author James Todd [[EMAIL PROTECTED]] * @author Costin Manolache + * @author Remy Maucherat */ public final class ByteChunk implements Cloneable, Serializable { + + // Input interface, used when the buffer is emptied. + public static interface ByteInputChannel { + /** + * Read new bytes ( usually the internal conversion buffer ). + * The implementation is allowed to ignore the parameters, + * and mutate the chunk if it wishes to implement its own buffering. + */ + public int realReadBytes(byte cbuf[], int off, int len) + throws IOException; + } // Output interface, used when the buffer is filled. public static interface ByteOutputChannel { - /** Send the bytes ( usually the internal conversion buffer ). - * Expect 8k output if the buffer is full. - */ - public void realWriteBytes( byte cbuf[], int off, int len) - throws IOException; + /** + * Send the bytes ( usually the internal conversion buffer ). + * Expect 8k output if the buffer is full. + */ + public void realWriteBytes(byte cbuf[], int off, int len) + throws IOException; } // -------------------- @@ -120,7 +133,8 @@ // How much can it grow, when data is added private int limit=-1; - private ByteOutputChannel out=null; + private ByteInputChannel in = null; + private ByteOutputChannel out = null; private boolean isOutput=false; @@ -247,6 +261,13 @@ return limit; } + /** + * When the buffer is empty, read the data from the input channel. + */ + public void setByteInputChannel(ByteInputChannel in) { + this.in = in; + } + /** When the buffer is full, write the data to the output channel. * Also used when large amount of data is appended. * @@ -348,6 +369,42 @@ out.realWriteBytes( src, off, len ); } } + + + // -------------------- Removing data from the buffer -------------------- + + public int substract() + throws IOException { + + if ((end - start) == 0) { + int n = in.realReadBytes( buff, 0, buff.length ); + if (n < 0) + return -1; + } + + return (buff[start++] & 0xFF); + + } + + public int substract( byte src[], int off, int len ) + throws IOException { + + if ((end - start) == 0) { + int n = in.realReadBytes( buff, 0, buff.length ); + if (n < 0) + return -1; + } + + int n = len; + if (len > getLength()) { + n = getLength(); + } + System.arraycopy(buff, start, src, off, n); + start += n; + return n; + + } + public void flushBuffer() throws IOException 1.5 +60 -5 jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/buf/CharChunk.java Index: CharChunk.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/buf/CharChunk.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- CharChunk.java 16 Mar 2002 06:49:15 -0000 1.4 +++ CharChunk.java 5 Jan 2003 11:22:16 -0000 1.5 @@ -70,11 +70,23 @@ * it is known to not be the most efficient solution - Strings are * designed as imutable and secure objects. * - * @author [EMAIL PROTECTED] - * @author James Todd [[EMAIL PROTECTED]] + * @author [EMAIL PROTECTED] + * @author James Todd [[EMAIL PROTECTED]] * @author Costin Manolache + * @author Remy Maucherat */ public final class CharChunk implements Cloneable, Serializable { + + // Input interface, used when the buffer is emptied. + public static interface CharInputChannel { + /** + * Read new bytes ( usually the internal conversion buffer ). + * The implementation is allowed to ignore the parameters, + * and mutate the chunk if it wishes to implement its own buffering. + */ + public int realReadChars(char cbuf[], int off, int len) + throws IOException; + } /** * When we need more space we'll either * grow the buffer ( up to the limit ) or send it to a channel. @@ -83,8 +95,8 @@ /** Send the bytes ( usually the internal conversion buffer ). * Expect 8k output if the buffer is full. */ - public void realWriteChars( char cbuf[], int off, int len) - throws IOException; + public void realWriteChars(char cbuf[], int off, int len) + throws IOException; } // -------------------- @@ -102,7 +114,8 @@ // maximum amount to be cached private int limit=-1; - CharOutputChannel out=null; + private CharInputChannel in = null; + private CharOutputChannel out = null; /** * Creates a new, uninitialized CharChunk object. @@ -182,6 +195,13 @@ return limit; } + /** + * When the buffer is empty, read the data from the input channel. + */ + public void setCharInputChannel(CharInputChannel in) { + this.in = in; + } + /** When the buffer is full, write the data to the output channel. * Also used when large amount of data is appended. * @@ -377,6 +397,41 @@ } } + // -------------------- Removing data from the buffer -------------------- + + public int substract() + throws IOException { + + if ((end - start) == 0) { + int n = in.realReadChars(buff, 0, buff.length); + if (n < 0) + return -1; + } + + return (buff[start++]); + + } + + public int substract( char src[], int off, int len ) + throws IOException { + + if ((end - start) == 0) { + int n = in.realReadChars( buff, 0, buff.length ); + if (n < 0) + return -1; + } + + int n = len; + if (len > getLength()) { + n = getLength(); + } + System.arraycopy(buff, start, src, off, n); + start += n; + return n; + + } + + public void flushBuffer() throws IOException {
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>