Author: markt Date: Fri Jan 12 14:16:08 2018 New Revision: 1820994 URL: http://svn.apache.org/viewvc?rev=1820994&view=rev Log: Format classes. No functional change.
Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java?rev=1820994&r1=1820993&r2=1820994&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java Fri Jan 12 14:16:08 2018 @@ -43,16 +43,15 @@ import java.nio.charset.StandardCharsets // inside this way it could provide the search/etc on ByteBuffer, as a helper. /** - * This class is used to represent a chunk of bytes, and - * utilities to manipulate byte[]. + * This class is used to represent a chunk of bytes, and utilities to manipulate + * byte[]. * * The buffer can be modified and used for both input and output. * * There are 2 modes: The chunk can be associated with a sink - ByteInputChannel * or ByteOutputChannel, which will be used when the buffer is empty (on input) - * or filled (on output). - * For output, it can also grow. This operating mode is selected by calling - * setLimit() or allocate(initial, limit) with limit != -1. + * or filled (on output). For output, it can also grow. This operating mode is + * selected by calling setLimit() or allocate(initial, limit) with limit != -1. * * Various search and append method are defined - similar with String and * StringBuffer, but operating on bytes. @@ -71,11 +70,13 @@ public final class ByteChunk implements private static final long serialVersionUID = 1L; - /** Input interface, used when the buffer is empty + /** + * Input interface, used when the buffer is empty * * Same as java.nio.channel.ReadableByteChannel */ public static interface ByteInputChannel { + /** * Read new bytes. * @@ -86,24 +87,26 @@ public final class ByteChunk implements public int realReadBytes() throws IOException; } - /** Same as java.nio.channel.WritableByteChannel. + /** + * Same as java.nio.channel.WritableByteChannel. */ public static interface ByteOutputChannel { + /** - * Send the bytes ( usually the internal conversion buffer ). - * Expect 8k output if the buffer is full. + * Send the bytes ( usually the internal conversion buffer ). Expect 8k + * output if the buffer is full. * * @param cbuf bytes that will be written * @param off offset in the bytes array * @param len length that will be written * @throws IOException If an I/O occurs while writing the bytes */ - public void realWriteBytes(byte cbuf[], int off, int len) - throws IOException; + 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. + * Send the bytes ( usually the internal conversion buffer ). Expect 8k + * output if the buffer is full. * * @param from bytes that will be written * @throws IOException If an I/O occurs while writing the bytes @@ -113,33 +116,35 @@ public final class ByteChunk implements // -------------------- - /** Default encoding used to convert to strings. It should be UTF8, - as most standards seem to converge, but the servlet API requires - 8859_1, and this object is used mostly for servlets. - */ + /** + * Default encoding used to convert to strings. It should be UTF8, as most + * standards seem to converge, but the servlet API requires 8859_1, and this + * object is used mostly for servlets. + */ public static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1; - private int hashCode=0; + private int hashCode = 0; // did we compute the hashcode ? private boolean hasHashCode = false; // byte[] private byte[] buff; - private int start=0; + private int start = 0; private int end; private transient Charset charset; - private boolean isSet=false; // XXX + private boolean isSet = false; // XXX // How much can it grow, when data is added - private int limit=-1; + private int limit = -1; // transient as serialization is primarily for values via, e.g. JMX private transient ByteInputChannel in = null; private transient ByteOutputChannel out = null; + /** * Creates a new, uninitialized ByteChunk object. */ @@ -147,8 +152,9 @@ public final class ByteChunk implements // NO-OP } - public ByteChunk( int initial ) { - allocate( initial, -1 ); + + public ByteChunk(int initial) { + allocate(initial, -1); } @@ -169,34 +175,38 @@ public final class ByteChunk implements return super.clone(); } + public boolean isNull() { - return ! isSet; // buff==null; + return !isSet; // buff==null; } + /** * Resets the message buff to an uninitialized state. */ public void recycle() { - charset=null; - start=0; - end=0; - isSet=false; + charset = null; + start = 0; + end = 0; + isSet = false; hasHashCode = false; } + // -------------------- Setup -------------------- - public void allocate( int initial, int limit ) { - if( buff==null || buff.length < initial ) { - buff=new byte[initial]; - } - this.limit=limit; - start=0; - end=0; - isSet=true; + public void allocate(int initial, int limit) { + if (buff == null || buff.length < initial) { + buff = new byte[initial]; + } + this.limit = limit; + start = 0; + end = 0; + isSet = true; hasHashCode = false; } + /** * Sets the message bytes to the specified subarray of bytes. * @@ -207,15 +217,17 @@ public final class ByteChunk implements public void setBytes(byte[] b, int off, int len) { buff = b; start = off; - end = start+ len; - isSet=true; + end = start + len; + isSet = true; hasHashCode = false; } + public void setCharset(Charset charset) { this.charset = charset; } + public Charset getCharset() { if (charset == null) { charset = DEFAULT_CHARSET; @@ -223,6 +235,7 @@ public final class ByteChunk implements return charset; } + /** * @return the message bytes. */ @@ -230,6 +243,7 @@ public final class ByteChunk implements return getBuffer(); } + /** * @return the message bytes. */ @@ -237,95 +251,107 @@ public final class ByteChunk implements return buff; } + /** - * @return the start offset of the bytes. - * For output this is the end of the buffer. + * @return the start offset of the bytes. For output this is the end of the + * buffer. */ public int getStart() { return start; } + public int getOffset() { return start; } + public void setOffset(int off) { - if (end < off ) { - end=off; + if (end < off) { + end = off; } - start=off; + start = off; } + /** * @return the length of the bytes. */ public int getLength() { - return end-start; + return end - start; } + /** - * Maximum amount of data in this buffer. - * If -1 or not set, the buffer will grow indefinitely. - * Can be smaller than the current buffer size ( which will not shrink ). - * When the limit is reached, the buffer will be flushed ( if out is set ) - * or throw exception. + * Maximum amount of data in this buffer. If -1 or not set, the buffer will + * grow indefinitely. Can be smaller than the current buffer size ( which + * will not shrink ). When the limit is reached, the buffer will be flushed + * ( if out is set ) or throw exception. + * * @param limit The new limit */ public void setLimit(int limit) { - this.limit=limit; + this.limit = limit; } + public int getLimit() { return limit; } + /** * When the buffer is empty, read the data from the input channel. + * * @param in 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. - * If not set, the buffer will grow to the limit. + * When the buffer is full, write the data to the output channel. Also used + * when large amount of data is appended. If not set, the buffer will grow + * to the limit. + * * @param out The output channel */ public void setByteOutputChannel(ByteOutputChannel out) { - this.out=out; + this.out = out; } + public int getEnd() { return end; } - public void setEnd( int i ) { - end=i; + + public void setEnd(int i) { + end = i; } + // -------------------- Adding data to the buffer -------------------- - public void append( byte b ) - throws IOException - { - makeSpace( 1 ); + public void append(byte b) throws IOException { + makeSpace(1); // couldn't make space - if( limit >0 && end >= limit ) { + if (limit > 0 && end >= limit) { flushBuffer(); } - buff[end++]=b; + buff[end++] = b; } - public void append( ByteChunk src ) - throws IOException - { - append( src.getBytes(), src.getStart(), src.getLength()); + + public void append(ByteChunk src) throws IOException { + append(src.getBytes(), src.getStart(), src.getLength()); } + /** * Add data to the buffer. + * * @param src Bytes array * @param off Offset * @param len Length @@ -333,13 +359,13 @@ public final class ByteChunk implements */ public void append(byte src[], int off, int len) throws IOException { // will grow, up to limit - makeSpace( len ); + makeSpace(len); // if we don't have limit: makeSpace can grow as it wants - if( limit < 0 ) { + if (limit < 0) { // assert: makeSpace made enough space - System.arraycopy( src, off, buff, end, len ); - end+=len; + System.arraycopy(src, off, buff, end, len); + end += len; return; } @@ -347,16 +373,16 @@ public final class ByteChunk implements // If the buffer is empty and the source is going to fill up all the // space in buffer, may as well write it directly to the output, // and avoid an extra copy - if ( len == limit && end == start && out != null ) { - out.realWriteBytes( src, off, len ); + if (len == limit && end == start && out != null) { + out.realWriteBytes(src, off, len); return; } // if we have limit and we're below - if( len <= limit - end ) { + if (len <= limit - end) { // makeSpace will grow the buffer to the limit, // so we have space - System.arraycopy( src, off, buff, end, len ); - end+=len; + System.arraycopy(src, off, buff, end, len); + end += len; return; } @@ -368,7 +394,7 @@ public final class ByteChunk implements // We chunk the data into slices fitting in the buffer limit, although // if the data is written directly if it doesn't fit - int avail=limit-end; + int avail = limit - end; System.arraycopy(src, off, buff, end, avail); end += avail; @@ -377,7 +403,7 @@ public final class ByteChunk implements int remain = len - avail; while (remain > (limit - end)) { - out.realWriteBytes( src, (off + len) - remain, limit - end ); + out.realWriteBytes(src, (off + len) - remain, limit - end); remain = remain - (limit - end); } @@ -473,7 +499,7 @@ public final class ByteChunk implements } - public int substract(byte dest[], int off, int len ) throws IOException { + public int substract(byte dest[], int off, int len) throws IOException { if (checkEof()) { return -1; } @@ -490,8 +516,8 @@ public final class ByteChunk implements /** * Transfers bytes from the buffer to the specified ByteBuffer. After the * operation the position of the ByteBuffer will be returned to the one - * before the operation, the limit will be the position incremented by - * the number of the transfered bytes. + * before the operation, the limit will be the position incremented by the + * number of the transfered bytes. * * @param to the ByteBuffer into which bytes are to be written. * @return an integer specifying the actual number of bytes read, or -1 if @@ -531,24 +557,21 @@ public final class ByteChunk implements * * @throws IOException Writing overflow data to the output channel failed */ - public void flushBuffer() - throws IOException - { - //assert out!=null - if( out==null ) { - throw new IOException( "Buffer overflow, no sink " + limit + " " + - buff.length ); + public void flushBuffer() throws IOException { + // assert out!=null + if (out == null) { + throw new IOException("Buffer overflow, no sink " + limit + " " + buff.length); } - out.realWriteBytes( buff, start, end-start ); - end=start; + out.realWriteBytes(buff, start, end - start); + end = start; } + /** * Make space for len bytes. If len is small, allocate a reserve space too. * Never grow bigger than limit. * - * @param count - * The size + * @param count The size */ public void makeSpace(int count) { byte[] tmp = null; @@ -592,18 +615,20 @@ public final class ByteChunk implements start = 0; } + // -------------------- Conversion and getters -------------------- @Override public String toString() { if (null == buff) { return null; - } else if (end-start == 0) { + } else if (end - start == 0) { return ""; } return StringCache.toString(this); } + public String toStringInternal() { if (charset == null) { charset = DEFAULT_CHARSET; @@ -611,12 +636,13 @@ public final class ByteChunk implements // new String(byte[], int, int, Charset) takes a defensive copy of the // entire byte array. This is expensive if only a small subset of the // bytes will be used. The code below is from Apache Harmony. - CharBuffer cb = charset.decode(ByteBuffer.wrap(buff, start, end-start)); + CharBuffer cb = charset.decode(ByteBuffer.wrap(buff, start, end - start)); return new String(cb.array(), cb.arrayOffset(), cb.length()); } + public long getLong() { - return Ascii.parseLong(buff, start,end-start); + return Ascii.parseLong(buff, start, end - start); } @@ -630,8 +656,10 @@ public final class ByteChunk implements return false; } + /** * Compares the message bytes to the specified String object. + * * @param s the String to compare * @return true if the comparison succeeded, false otherwise */ @@ -640,7 +668,7 @@ public final class ByteChunk implements // ( ok for tomcat, where we compare ascii - header names, etc )!!! byte[] b = buff; - int blen = end-start; + int blen = end - start; if (b == null || blen != s.length()) { return false; } @@ -653,14 +681,16 @@ public final class ByteChunk implements return true; } + /** * Compares the message bytes to the specified String object. + * * @param s the String to compare * @return true if the comparison succeeded, false otherwise */ public boolean equalsIgnoreCase(String s) { byte[] b = buff; - int blen = end-start; + int blen = end - start; if (b == null || blen != s.length()) { return false; } @@ -673,24 +703,26 @@ public final class ByteChunk implements return true; } - public boolean equals( ByteChunk bb ) { - return equals( bb.getBytes(), bb.getStart(), bb.getLength()); + + public boolean equals(ByteChunk bb) { + return equals(bb.getBytes(), bb.getStart(), bb.getLength()); } - public boolean equals( byte b2[], int off2, int len2) { - byte b1[]=buff; - if( b1==null && b2==null ) { + + public boolean equals(byte b2[], int off2, int len2) { + byte b1[] = buff; + if (b1 == null && b2 == null) { return true; } - int len=end-start; - if ( len2 != len || b1==null || b2==null ) { + int len = end - start; + if (len2 != len || b1 == null || b2 == null) { return false; } int off1 = start; - while ( len-- > 0) { + while (len-- > 0) { if (b1[off1++] != b2[off2++]) { return false; } @@ -698,33 +730,37 @@ public final class ByteChunk implements return true; } - public boolean equals( CharChunk cc ) { - return equals( cc.getChars(), cc.getStart(), cc.getLength()); + + public boolean equals(CharChunk cc) { + return equals(cc.getChars(), cc.getStart(), cc.getLength()); } - public boolean equals( char c2[], int off2, int len2) { + + public boolean equals(char c2[], int off2, int len2) { // XXX works only for enc compatible with ASCII/UTF !!! - byte b1[]=buff; - if( c2==null && b1==null ) { + byte b1[] = buff; + if (c2 == null && b1 == null) { return true; } - if (b1== null || c2==null || end-start != len2 ) { + if (b1 == null || c2 == null || end - start != len2) { return false; } int off1 = start; - int len=end-start; + int len = end - start; - while ( len-- > 0) { - if ( (char)b1[off1++] != c2[off2++]) { + while (len-- > 0) { + if ((char) b1[off1++] != c2[off2++]) { return false; } } return true; } + /** * Returns true if the message bytes starts with the specified string. + * * @param s the string * @param pos The position * @return <code>true</code> if the start matches @@ -732,42 +768,43 @@ public final class ByteChunk implements public boolean startsWithIgnoreCase(String s, int pos) { byte[] b = buff; int len = s.length(); - if (b == null || len+pos > end-start) { + if (b == null || len + pos > end - start) { return false; } - int off = start+pos; + int off = start + pos; for (int i = 0; i < len; i++) { - if (Ascii.toLower( b[off++] ) != Ascii.toLower( s.charAt(i))) { + if (Ascii.toLower(b[off++]) != Ascii.toLower(s.charAt(i))) { return false; } } return true; } - public int indexOf( String src, int srcOff, int srcLen, int myOff ) { - char first=src.charAt( srcOff ); + + public int indexOf(String src, int srcOff, int srcLen, int myOff) { + char first = src.charAt(srcOff); // Look for first char int srcEnd = srcOff + srcLen; - mainLoop: - for( int i=myOff+start; i <= (end - srcLen); i++ ) { - if( buff[i] != first ) { + mainLoop: for (int i = myOff + start; i <= (end - srcLen); i++) { + if (buff[i] != first) { continue; } // found first char, now look for a match - int myPos=i+1; - for( int srcPos=srcOff + 1; srcPos< srcEnd;) { - if( buff[myPos++] != src.charAt( srcPos++ )) { + int myPos = i + 1; + for (int srcPos = srcOff + 1; srcPos < srcEnd;) { + if (buff[myPos++] != src.charAt(srcPos++)) { continue mainLoop; } } - return i-start; // found it + return i - start; // found it } return -1; } - // -------------------- Hash code -------------------- + + // -------------------- Hash code -------------------- @Override public int hashCode() { @@ -782,32 +819,34 @@ public final class ByteChunk implements return code; } + // normal hash. public int hash() { - return hashBytes( buff, start, end-start); + return hashBytes(buff, start, end - start); } - private static int hashBytes( byte buff[], int start, int bytesLen ) { - int max=start+bytesLen; - byte bb[]=buff; - int code=0; - for (int i = start; i < max ; i++) { + + private static int hashBytes(byte buff[], int start, int bytesLen) { + int max = start + bytesLen; + byte bb[] = buff; + int code = 0; + for (int i = start; i < max; i++) { code = code * 37 + bb[i]; } return code; } + /** * Returns the first instance of the given character in this ByteChunk * starting at the specified byte. If the character is not found, -1 is - * returned. - * <br> + * returned. <br> * NOTE: This only works for characters in the range 0-127. * - * @param c The character - * @param starting The start position - * @return The position of the first instance of the character or - * -1 if the character is not found. + * @param c The character + * @param starting The start position + * @return The position of the first instance of the character or -1 if the + * character is not found. */ public int indexOf(char c, int starting) { int ret = indexOf(buff, start + starting, end, c); @@ -817,22 +856,21 @@ public final class ByteChunk implements /** * Returns the first instance of the given character in the given byte array - * between the specified start and end. - * <br> + * between the specified start and end. <br> * NOTE: This only works for characters in the range 0-127. * * @param bytes The byte array to search * @param start The point to start searching from in the byte array - * @param end The point to stop searching in the byte array - * @param c The character to search for - * @return The position of the first instance of the character or -1 - * if the character is not found. + * @param end The point to stop searching in the byte array + * @param c The character to search for + * @return The position of the first instance of the character or -1 if the + * character is not found. */ public static int indexOf(byte bytes[], int start, int end, char c) { int offset = start; while (offset < end) { - byte b=bytes[offset]; + byte b = bytes[offset]; if (b == c) { return offset; } @@ -841,16 +879,17 @@ public final class ByteChunk implements return -1; } + /** * Returns the first instance of the given byte in the byte array between * the specified start and end. * * @param bytes The byte array to search * @param start The point to start searching from in the byte array - * @param end The point to stop searching in the byte array - * @param b The byte to search for - * @return The position of the first instance of the byte or -1 if the - * byte is not found. + * @param end The point to stop searching in the byte array + * @param b The byte to search for + * @return The position of the first instance of the byte or -1 if the byte + * is not found. */ public static int findByte(byte bytes[], int start, int end, byte b) { int offset = start; @@ -863,22 +902,23 @@ public final class ByteChunk implements return -1; } + /** * Returns the first instance of any of the given bytes in the byte array * between the specified start and end. * * @param bytes The byte array to search * @param start The point to start searching from in the byte array - * @param end The point to stop searching in the byte array - * @param b The array of bytes to search for - * @return The position of the first instance of the byte or -1 if the - * byte is not found. + * @param end The point to stop searching in the byte array + * @param b The array of bytes to search for + * @return The position of the first instance of the byte or -1 if the byte + * is not found. */ public static int findBytes(byte bytes[], int start, int end, byte b[]) { int blen = b.length; int offset = start; while (offset < end) { - for (int i = 0; i < blen; i++) { + for (int i = 0; i < blen; i++) { if (bytes[offset] == b[i]) { return offset; } @@ -888,6 +928,7 @@ public final class ByteChunk implements return -1; } + /** * Convert specified String to a byte array. This ONLY WORKS for ascii, UTF * chars will be truncated. Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java?rev=1820994&r1=1820993&r2=1820994&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java Fri Jan 12 14:16:08 2018 @@ -20,10 +20,9 @@ import java.io.IOException; import java.io.Serializable; /** - * Utilities to manipulate char chunks. While String is - * the easiest way to manipulate chars ( search, substrings, etc), - * it is known to not be the most efficient solution - Strings are - * designed as immutable and secure objects. + * Utilities to manipulate char chunks. While String is the easiest way to + * manipulate chars ( search, substrings, etc), it is known to not be the most + * efficient solution - Strings are designed as immutable and secure objects. * * @author d...@sun.com * @author James Todd [go...@sun.com] @@ -36,6 +35,7 @@ public final class CharChunk implements // Input interface, used when the buffer is emptied. public static interface CharInputChannel { + /** * Read new characters. * @@ -45,22 +45,23 @@ public final class CharChunk implements */ public int realReadChars() throws IOException; } + /** - * When we need more space we'll either - * grow the buffer ( up to the limit ) or send it to a channel. + * When we need more space we'll either grow the buffer ( up to the limit ) + * or send it to a channel. */ public static interface CharOutputChannel { + /** - * Send the bytes ( usually the internal conversion buffer ). - * Expect 8k output if the buffer is full. + * Send the bytes ( usually the internal conversion buffer ). Expect 8k + * output if the buffer is full. * * @param cbuf characters that will be written * @param off offset in the characters array * @param len length that will be written * @throws IOException If an I/O occurs while writing the characters */ - public void realWriteChars(char cbuf[], int off, int len) - throws IOException; + public void realWriteChars(char cbuf[], int off, int len) throws IOException; } // -------------------- @@ -75,17 +76,18 @@ public final class CharChunk implements private int start; private int end; - private boolean isSet=false; // XXX + private boolean isSet = false; // XXX // -1: grow indefinitely // maximum amount to be cached - private int limit=-1; + private int limit = -1; // transient as serialization is primarily for values via, e.g. JMX private transient CharInputChannel in = null; private transient CharOutputChannel out = null; - private boolean optimizedWrite=true; + private boolean optimizedWrite = true; + /** * Creates a new, uninitialized CharChunk object. @@ -93,10 +95,12 @@ public final class CharChunk implements public CharChunk() { } + public CharChunk(int size) { - allocate( size, -1 ); + allocate(size, -1); } + // -------------------- @Override @@ -104,34 +108,37 @@ public final class CharChunk implements return super.clone(); } + public boolean isNull() { - if( end > 0 ) { + if (end > 0) { return false; } - return !isSet; //XXX + return !isSet; // XXX } + /** * Resets the message bytes to an uninitialized state. */ public void recycle() { - // buff=null; - isSet=false; // XXX + // buff=null; + isSet = false; // XXX hasHashCode = false; - start=0; - end=0; + start = 0; + end = 0; } + // -------------------- Setup -------------------- - public void allocate( int initial, int limit ) { - if( buff==null || buff.length < initial ) { - buff=new char[initial]; - } - this.limit=limit; - start=0; - end=0; - isSet=true; + public void allocate(int initial, int limit) { + if (buff == null || buff.length < initial) { + buff = new char[initial]; + } + this.limit = limit; + start = 0; + end = 0; + isSet = true; hasHashCode = false; } @@ -140,83 +147,94 @@ public final class CharChunk implements this.optimizedWrite = optimizedWrite; } - public void setChars( char[] c, int off, int len ) { - buff=c; - start=off; - end=start + len; - isSet=true; + + public void setChars(char[] c, int off, int len) { + buff = c; + start = off; + end = start + len; + isSet = true; hasHashCode = false; } + /** - * Maximum amount of data in this buffer. - * If -1 or not set, the buffer will grow indefinitely. - * Can be smaller than the current buffer size ( which will not shrink ). - * When the limit is reached, the buffer will be flushed ( if out is set ) - * or throw exception. + * Maximum amount of data in this buffer. If -1 or not set, the buffer will + * grow indefinitely. Can be smaller than the current buffer size ( which + * will not shrink ). When the limit is reached, the buffer will be flushed + * ( if out is set ) or throw exception. + * * @param limit The new limit */ public void setLimit(int limit) { - this.limit=limit; + this.limit = limit; } + public int getLimit() { return limit; } + /** * When the buffer is empty, read the data from the input channel. + * * @param in 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. - * If not set, the buffer will grow to the limit. + * When the buffer is full, write the data to the output channel. Also used + * when large amount of data is appended. If not set, the buffer will grow + * to the limit. + * * @param out The output channel */ public void setCharOutputChannel(CharOutputChannel out) { - this.out=out; + this.out = out; } + // compat - public char[] getChars() - { + public char[] getChars() { return getBuffer(); } - public char[] getBuffer() - { + + public char[] getBuffer() { return buff; } + /** - * @return the start offset of the chars. - * For output this is the end of the buffer. + * @return the start offset of the chars. For output this is the end of the + * buffer. */ public int getStart() { return start; } + public int getOffset() { return start; } + /** * @param off The offset */ public void setOffset(int off) { - start=off; + start = off; } + /** * @return the length of the bytes. */ public int getLength() { - return end-start; + return end - start; } @@ -224,66 +242,65 @@ public final class CharChunk implements return end; } - public void setEnd( int i ) { - end=i; + + public void setEnd(int i) { + end = i; } + // -------------------- Adding data -------------------- - public void append( char b ) - throws IOException - { - makeSpace( 1 ); + public void append(char b) throws IOException { + makeSpace(1); // couldn't make space - if( limit >0 && end >= limit ) { + if (limit > 0 && end >= limit) { flushBuffer(); } - buff[end++]=b; + buff[end++] = b; } - public void append( CharChunk src ) - throws IOException - { - append( src.getBuffer(), src.getOffset(), src.getLength()); + + public void append(CharChunk src) throws IOException { + append(src.getBuffer(), src.getOffset(), src.getLength()); } + /** * Add data to the buffer. + * * @param src Char array * @param off Offset * @param len Length * @throws IOException Writing overflow data to the output channel failed */ - public void append( char src[], int off, int len ) - throws IOException - { + public void append(char src[], int off, int len) throws IOException { // will grow, up to limit - makeSpace( len ); + makeSpace(len); // if we don't have limit: makeSpace can grow as it wants - if( limit < 0 ) { + if (limit < 0) { // assert: makeSpace made enough space - System.arraycopy( src, off, buff, end, len ); - end+=len; + System.arraycopy(src, off, buff, end, len); + end += len; return; } // Optimize on a common case. // If the source is going to fill up all the space in buffer, may // as well write it directly to the output, and avoid an extra copy - if ( optimizedWrite && len == limit && end == start && out != null ) { - out.realWriteChars( src, off, len ); + if (optimizedWrite && len == limit && end == start && out != null) { + out.realWriteChars(src, off, len); return; } // if we have limit and we're below - if( len <= limit - end ) { + if (len <= limit - end) { // makeSpace will grow the buffer to the limit, // so we have space - System.arraycopy( src, off, buff, end, len ); + System.arraycopy(src, off, buff, end, len); - end+=len; + end += len; return; } @@ -299,32 +316,34 @@ public final class CharChunk implements // and still have some space for more. We'll still have 2 writes, but // we write more on the first. - if( len + end < 2 * limit ) { - /* If the request length exceeds the size of the output buffer, - flush the output buffer and then write the data directly. - We can't avoid 2 writes, but we can write more on the second - */ - int avail=limit-end; + if (len + end < 2 * limit) { + /* + * If the request length exceeds the size of the output buffer, + * flush the output buffer and then write the data directly. We + * can't avoid 2 writes, but we can write more on the second + */ + int avail = limit - end; System.arraycopy(src, off, buff, end, avail); end += avail; flushBuffer(); - System.arraycopy(src, off+avail, buff, end, len - avail); - end+= len - avail; + System.arraycopy(src, off + avail, buff, end, len - avail); + end += len - avail; - } else { // len > buf.length + avail + } else { // len > buf.length + avail // long write - flush the buffer and write the rest // directly from source flushBuffer(); - out.realWriteChars( src, off, len ); + out.realWriteChars(src, off, len); } } /** * Append a string to the buffer. + * * @param s The string * @throws IOException Writing overflow data to the output channel failed */ @@ -332,26 +351,28 @@ public final class CharChunk implements append(s, 0, s.length()); } + /** * Append a string to the buffer. + * * @param s The string * @param off Offset * @param len Length * @throws IOException Writing overflow data to the output channel failed */ public void append(String s, int off, int len) throws IOException { - if (s==null) { + if (s == null) { return; } // will grow, up to limit - makeSpace( len ); + makeSpace(len); // if we don't have limit: makeSpace can grow as it wants - if( limit < 0 ) { + if (limit < 0) { // assert: makeSpace made enough space - s.getChars(off, off+len, buff, end ); - end+=len; + s.getChars(off, off + len, buff, end); + end += len; return; } @@ -359,7 +380,7 @@ public final class CharChunk implements int sEnd = off + len; while (sOff < sEnd) { int d = min(limit - end, sEnd - sOff); - s.getChars( sOff, sOff+d, buff, end); + s.getChars(sOff, sOff + d, buff, end); sOff += d; end += d; if (end >= limit) { @@ -368,6 +389,7 @@ public final class CharChunk implements } } + // -------------------- Removing data from the buffer -------------------- public int substract() throws IOException { @@ -383,6 +405,7 @@ public final class CharChunk implements return (buff[start++]); } + public int substract(char dest[], int off, int len) throws IOException { if ((end - start) == 0) { if (in == null) { @@ -405,21 +428,20 @@ public final class CharChunk implements public void flushBuffer() throws IOException { - //assert out!=null - if( out==null ) { - throw new IOException( "Buffer overflow, no sink " + limit + " " + - buff.length ); + // assert out!=null + if (out == null) { + throw new IOException("Buffer overflow, no sink " + limit + " " + buff.length); } - out.realWriteChars( buff, start, end - start ); - end=start; + out.realWriteChars(buff, start, end - start); + end = start; } + /** * Make space for len chars. If len is small, allocate a reserve space too. * Never grow bigger than limit. * - * @param count - * The size + * @param count The size */ public void makeSpace(int count) { char[] tmp = null; @@ -461,22 +483,25 @@ public final class CharChunk implements tmp = null; } + // -------------------- Conversion and getters -------------------- @Override public String toString() { if (null == buff) { return null; - } else if (end-start == 0) { + } else if (end - start == 0) { return ""; } return StringCache.toString(this); } + public String toStringInternal() { - return new String(buff, start, end-start); + return new String(buff, start, end - start); } + // -------------------- equals -------------------- @Override @@ -487,14 +512,17 @@ public final class CharChunk implements return false; } + /** * Compares the message bytes to the specified String object. + * * @param s the String to compare - * @return <code>true</code> if the comparison succeeded, <code>false</code> otherwise + * @return <code>true</code> if the comparison succeeded, <code>false</code> + * otherwise */ public boolean equals(String s) { char[] c = buff; - int len = end-start; + int len = end - start; if (c == null || len != s.length()) { return false; } @@ -507,42 +535,47 @@ public final class CharChunk implements return true; } + /** * Compares the message bytes to the specified String object. + * * @param s the String to compare - * @return <code>true</code> if the comparison succeeded, <code>false</code> otherwise + * @return <code>true</code> if the comparison succeeded, <code>false</code> + * otherwise */ public boolean equalsIgnoreCase(String s) { char[] c = buff; - int len = end-start; + int len = end - start; if (c == null || len != s.length()) { return false; } int off = start; for (int i = 0; i < len; i++) { - if (Ascii.toLower( c[off++] ) != Ascii.toLower( s.charAt(i))) { + if (Ascii.toLower(c[off++]) != Ascii.toLower(s.charAt(i))) { return false; } } return true; } + public boolean equals(CharChunk cc) { - return equals( cc.getChars(), cc.getOffset(), cc.getLength()); + return equals(cc.getChars(), cc.getOffset(), cc.getLength()); } + public boolean equals(char b2[], int off2, int len2) { - char b1[]=buff; - if( b1==null && b2==null ) { + char b1[] = buff; + if (b1 == null && b2 == null) { return true; } - if (b1== null || b2==null || end-start != len2) { + if (b1 == null || b2 == null || end - start != len2) { return false; } int off1 = start; - int len=end-start; - while ( len-- > 0) { + int len = end - start; + while (len-- > 0) { if (b1[off1++] != b2[off2++]) { return false; } @@ -550,14 +583,16 @@ public final class CharChunk implements return true; } + /** - * @return <code>true</code> if the message bytes starts with the specified string. + * @return <code>true</code> if the message bytes starts with the specified + * string. * @param s The string */ public boolean startsWith(String s) { char[] c = buff; int len = s.length(); - if (c == null || len > end-start) { + if (c == null || len > end - start) { return false; } int off = start; @@ -569,20 +604,22 @@ public final class CharChunk implements return true; } + /** - * @return <code>true</code> if the message bytes starts with the specified string. + * @return <code>true</code> if the message bytes starts with the specified + * string. * @param s The string * @param pos The position at which the comparison will be made */ public boolean startsWithIgnoreCase(String s, int pos) { char[] c = buff; int len = s.length(); - if (c == null || len+pos > end-start) { + if (c == null || len + pos > end - start) { return false; } - int off = start+pos; + int off = start + pos; for (int i = 0; i < len; i++) { - if (Ascii.toLower( c[off++] ) != Ascii.toLower( s.charAt(i))) { + if (Ascii.toLower(c[off++]) != Ascii.toLower(s.charAt(i))) { return false; } } @@ -591,13 +628,14 @@ public final class CharChunk implements /** - * @return <code>true</code> if the message bytes end with the specified string. + * @return <code>true</code> if the message bytes end with the specified + * string. * @param s The string */ public boolean endsWith(String s) { char[] c = buff; int len = s.length(); - if (c == null || len > end-start) { + if (c == null || len > end - start) { return false; } int off = end - len; @@ -609,7 +647,8 @@ public final class CharChunk implements return true; } - // -------------------- Hash code -------------------- + + // -------------------- Hash code -------------------- @Override public int hashCode() { @@ -624,34 +663,38 @@ public final class CharChunk implements return code; } + // normal hash. public int hash() { - int code=0; - for (int i = start; i < start + end-start; i++) { + int code = 0; + for (int i = start; i < start + end - start; i++) { code = code * 37 + buff[i]; } return code; } + public int indexOf(char c) { - return indexOf( c, start); + return indexOf(c, start); } + /** - * @return <code>true</code> if the message bytes starts with the specified string. + * @return <code>true</code> if the message bytes starts with the specified + * string. * @param c the character * @param starting Start position */ public int indexOf(char c, int starting) { - int ret = indexOf( buff, start+starting, end, c ); + int ret = indexOf(buff, start + starting, end, c); return (ret >= start) ? ret - start : -1; } - public static int indexOf( char chars[], int off, int cend, char qq ) - { - while( off < cend ) { - char b=chars[off]; - if( b==qq ) { + + public static int indexOf(char chars[], int off, int cend, char qq) { + while (off < cend) { + char b = chars[off]; + if (b == qq) { return off; } off++; @@ -660,31 +703,31 @@ public final class CharChunk implements } - public int indexOf( String src, int srcOff, int srcLen, int myOff ) { - char first=src.charAt( srcOff ); + public int indexOf(String src, int srcOff, int srcLen, int myOff) { + char first = src.charAt(srcOff); // Look for first char int srcEnd = srcOff + srcLen; - for( int i=myOff+start; i <= (end - srcLen); i++ ) { - if( buff[i] != first ) { + for (int i = myOff + start; i <= (end - srcLen); i++) { + if (buff[i] != first) { continue; } // found first char, now look for a match - int myPos=i+1; - for( int srcPos=srcOff + 1; srcPos< srcEnd;) { - if( buff[myPos++] != src.charAt( srcPos++ )) { + int myPos = i + 1; + for (int srcPos = srcOff + 1; srcPos < srcEnd;) { + if (buff[myPos++] != src.charAt(srcPos++)) { break; } - if( srcPos==srcEnd ) - { - return i-start; // found it + if (srcPos == srcEnd) { + return i - start; // found it } } } return -1; } + // -------------------- utils private int min(int a, int b) { if (a < b) { @@ -693,6 +736,7 @@ public final class CharChunk implements return b; } + // Char sequence impl @Override @@ -700,6 +744,7 @@ public final class CharChunk implements return buff[index + start]; } + @Override public CharSequence subSequence(int start, int end) { try { @@ -713,6 +758,7 @@ public final class CharChunk implements } } + @Override public int length() { return end - start; --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org