Author: markt
Date: Tue Jan 16 21:24:25 2018
New Revision: 1821317
URL: http://svn.apache.org/viewvc?rev=1821317&view=rev
Log:
Back-port preparation.
Fix formatting. No functional change.
Modified:
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/CharChunk.java
Modified: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java
URL:
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java?rev=1821317&r1=1821316&r2=1821317&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java
(original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java Tue Jan
16 21:24:25 2018
@@ -40,16 +40,15 @@ import java.nio.charset.Charset;
// 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.
@@ -68,60 +67,64 @@ 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 ( 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.
+ * 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;
+ public int realReadBytes(byte cbuf[], int off, int len) 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.
*/
- public void realWriteBytes(byte cbuf[], int off, int len)
- throws IOException;
+ public void realWriteBytes(byte cbuf[], int off, int len) throws
IOException;
}
// --------------------
- /** 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 = B2CConverter.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 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;
private ByteInputChannel in = null;
private ByteOutputChannel out = null;
- private boolean optimizedWrite=true;
+ private boolean optimizedWrite = true;
+
/**
* Creates a new, uninitialized ByteChunk object.
@@ -130,55 +133,62 @@ public final class ByteChunk implements
// NO-OP
}
- public ByteChunk( int initial ) {
- allocate( initial, -1 );
+
+ public ByteChunk(int initial) {
+ allocate(initial, -1);
}
+
/**
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
public ByteChunk getClone() {
try {
- return (ByteChunk)this.clone();
- } catch( Exception ex) {
+ return (ByteChunk) this.clone();
+ } catch (Exception ex) {
return null;
}
}
+
public boolean isNull() {
- return ! isSet; // buff==null;
+ return !isSet; // buff==null;
}
+
/**
* Resets the message buff to an uninitialized state.
*/
public void recycle() {
- // buff = null;
- charset=null;
- start=0;
- end=0;
- isSet=false;
+ // buff = null;
+ charset = null;
+ start = 0;
+ end = 0;
+ isSet = false;
hasHashCode = false;
}
+
public void reset() {
- buff=null;
+ buff = null;
}
+
// -------------------- 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.
*
@@ -189,11 +199,12 @@ 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;
}
+
/**
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@@ -202,10 +213,12 @@ public final class ByteChunk implements
this.optimizedWrite = optimizedWrite;
}
+
public void setCharset(Charset charset) {
this.charset = charset;
}
+
public Charset getCharset() {
if (charset == null) {
charset = DEFAULT_CHARSET;
@@ -213,6 +226,7 @@ public final class ByteChunk implements
return charset;
}
+
/**
* Returns the message bytes.
*/
@@ -220,6 +234,7 @@ public final class ByteChunk implements
return getBuffer();
}
+
/**
* Returns the message bytes.
*/
@@ -227,48 +242,54 @@ public final class ByteChunk implements
return buff;
}
+
/**
- * Returns the start offset of the bytes.
- * For output this is the end of the buffer.
+ * Returns 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;
}
+
/**
- * Returns the length of the bytes.
- * XXX need to clean this up
+ * Returns the length of the bytes. XXX need to clean this up
*/
public int getLength() {
- return end-start;
+ return end - start;
}
- /** Maximum amount of data in this buffer.
+
+ /**
+ * 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.
+ * 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.
*/
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.
*/
@@ -276,68 +297,70 @@ public final class ByteChunk implements
this.in = in;
}
- /** When the buffer is full, write the data to the output channel.
- * Also used when large amount of data is appended.
+
+ /**
+ * 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.
+ * If not set, the buffer will grow to the limit.
*/
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 --------------------
- /** Append a char, by casting it to byte. This IS NOT intended for unicode.
+ /**
+ * Append a char, by casting it to byte. This IS NOT intended for unicode.
*
* @param c
* @throws IOException
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
- public void append( char c )
- throws IOException
- {
- append( (byte)c);
+ public void append(char c) throws IOException {
+ append((byte) c);
}
- 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
+
+ /**
+ * Add data to the buffer
*/
- public void append( byte src[], int off, int len )
- throws IOException
- {
+ 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;
}
@@ -345,16 +368,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 ( optimizedWrite && len == limit && end == start && out != null ) {
- out.realWriteBytes( src, off, len );
+ if (optimizedWrite && 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;
}
@@ -366,7 +389,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;
@@ -375,7 +398,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);
}
@@ -387,14 +410,13 @@ public final class ByteChunk implements
// -------------------- Removing data from the buffer --------------------
- public int substract()
- throws IOException {
+ public int substract() throws IOException {
if ((end - start) == 0) {
if (in == null) {
return -1;
}
- int n = in.realReadBytes( buff, 0, buff.length );
+ int n = in.realReadBytes(buff, 0, buff.length);
if (n < 0) {
return -1;
}
@@ -409,14 +431,13 @@ public final class ByteChunk implements
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
- public int substract(ByteChunk src)
- throws IOException {
+ public int substract(ByteChunk src) throws IOException {
if ((end - start) == 0) {
if (in == null) {
return -1;
}
- int n = in.realReadBytes( buff, 0, buff.length );
+ int n = in.realReadBytes(buff, 0, buff.length);
if (n < 0) {
return -1;
}
@@ -430,13 +451,12 @@ public final class ByteChunk implements
}
- public byte substractB()
- throws IOException {
+ public byte substractB() throws IOException {
if ((end - start) == 0) {
if (in == null)
return -1;
- int n = in.realReadBytes( buff, 0, buff.length );
+ int n = in.realReadBytes(buff, 0, buff.length);
if (n < 0)
return -1;
}
@@ -446,14 +466,13 @@ public final class ByteChunk implements
}
- public int substract( byte src[], int off, int len )
- throws IOException {
+ public int substract(byte src[], int off, int len) throws IOException {
if ((end - start) == 0) {
if (in == null) {
return -1;
}
- int n = in.realReadBytes( buff, 0, buff.length );
+ int n = in.realReadBytes(buff, 0, buff.length);
if (n < 0) {
return -1;
}
@@ -476,18 +495,16 @@ public final class ByteChunk implements
*
* @throws IOException
*/
- 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 chars. If len is small, allocate a reserve space too.
* Never grow bigger than limit.
@@ -496,63 +513,61 @@ public final class ByteChunk implements
byte[] tmp = null;
int newSize;
- int desiredSize=end + count;
+ int desiredSize = end + count;
// Can't grow above the limit
- if( limit > 0 &&
- desiredSize > limit) {
- desiredSize=limit;
+ if (limit > 0 && desiredSize > limit) {
+ desiredSize = limit;
}
- if( buff==null ) {
- if( desiredSize < 256 )
- {
- desiredSize=256; // take a minimum
+ if (buff == null) {
+ if (desiredSize < 256) {
+ desiredSize = 256; // take a minimum
}
- buff=new byte[desiredSize];
+ buff = new byte[desiredSize];
}
// limit < buf.length ( the buffer is already big )
// or we already have space XXX
- if( desiredSize <= buff.length ) {
+ if (desiredSize <= buff.length) {
return;
}
// grow in larger chunks
- if( desiredSize < 2 * buff.length ) {
- newSize= buff.length * 2;
- if( limit >0 &&
- newSize > limit ) {
- newSize=limit;
+ if (desiredSize < 2 * buff.length) {
+ newSize = buff.length * 2;
+ if (limit > 0 && newSize > limit) {
+ newSize = limit;
}
- tmp=new byte[newSize];
+ tmp = new byte[newSize];
} else {
- newSize= buff.length * 2 + count ;
- if( limit > 0 &&
- newSize > limit ) {
- newSize=limit;
+ newSize = buff.length * 2 + count;
+ if (limit > 0 && newSize > limit) {
+ newSize = limit;
}
- tmp=new byte[newSize];
+ tmp = new byte[newSize];
}
- System.arraycopy(buff, start, tmp, 0, end-start);
+ System.arraycopy(buff, start, tmp, 0, end - start);
buff = tmp;
tmp = null;
- end=end-start;
- start=0;
+ end = end - start;
+ 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;
@@ -561,21 +576,22 @@ public final class ByteChunk implements
// 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;
- cb = charset.decode(ByteBuffer.wrap(buff, start, end-start));
+ cb = charset.decode(ByteBuffer.wrap(buff, start, end - start));
return new String(cb.array(), cb.arrayOffset(), cb.length());
}
+
/**
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
- public int getInt()
- {
- return Ascii.parseInt(buff, start,end-start);
+ public int getInt() {
+ return Ascii.parseInt(buff, start, end - start);
}
+
public long getLong() {
- return Ascii.parseLong(buff, start,end-start);
+ return Ascii.parseLong(buff, start, end - start);
}
@@ -589,8 +605,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
*/
@@ -599,7 +617,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;
}
@@ -612,14 +630,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;
}
@@ -632,24 +652,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;
}
@@ -657,33 +679,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
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@@ -692,7 +718,7 @@ public final class ByteChunk implements
// Works only if enc==UTF
byte[] b = buff;
int blen = s.length();
- if (b == null || blen > end-start) {
+ if (b == null || blen > end - start) {
return false;
}
int boff = start;
@@ -704,8 +730,10 @@ public final class ByteChunk implements
return true;
}
+
/**
* Returns true if the message bytes start with the specified byte array.
+ *
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
@@ -727,50 +755,53 @@ public final class ByteChunk implements
return true;
}
+
/**
* Returns true if the message bytes starts with the specified string.
+ *
* @param s the string
* @param pos The position
*/
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() {
@@ -785,76 +816,78 @@ public final class ByteChunk implements
return code;
}
+
// normal hash.
public int hash() {
- return hashBytes( buff, start, end-start);
+ return hashBytes(buff, start, end - start);
}
+
/**
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
public int hashIgnoreCase() {
- return hashBytesIC( buff, start, end-start );
+ return hashBytesIC(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;
}
- private static int hashBytesIC( byte bytes[], int start,
- int bytesLen )
- {
- int max=start+bytesLen;
- byte bb[]=bytes;
- int code=0;
- for (int i = start; i < max ; i++) {
+
+ private static int hashBytesIC(byte bytes[], int start, int bytesLen) {
+ int max = start + bytesLen;
+ byte bb[] = bytes;
+ int code = 0;
+ for (int i = start; i < max; i++) {
code = code * 37 + Ascii.toLower(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);
return (ret >= start) ? ret - start : -1;
}
+
/**
* 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;
}
@@ -863,16 +896,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;
@@ -885,22 +919,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;
}
@@ -910,17 +945,17 @@ public final class ByteChunk implements
return -1;
}
+
/**
* Returns the first instance of any byte that is not one 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 list of bytes to search for
- * @return The position of the first instance a byte that is not
- * in the list of bytes to search for or -1 if no such
byte
- * is found.
+ * @param end The point to stop searching in the byte array
+ * @param b The list of bytes to search for
+ * @return The position of the first instance a byte that is not in the
list
+ * of bytes to search for or -1 if no such byte is found.
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
@@ -933,7 +968,7 @@ public final class ByteChunk implements
found = true;
for (int i = 0; i < blen; i++) {
if (bytes[offset] == b[i]) {
- found=false;
+ found = false;
break;
}
}
Modified: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/CharChunk.java
URL:
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/CharChunk.java?rev=1821317&r1=1821316&r2=1821317&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/CharChunk.java
(original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/CharChunk.java Tue Jan
16 21:24:25 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 [email protected]
* @author James Todd [[email protected]]
@@ -36,24 +35,26 @@ public final class CharChunk implements
// 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.
+ * 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;
+ 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.
+ * 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.
*/
- public void realWriteChars(char cbuf[], int off, int len)
- throws IOException;
+ public void realWriteChars(char cbuf[], int off, int len) throws
IOException;
}
// --------------------
@@ -68,16 +69,17 @@ 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;
private CharInputChannel in = null;
private CharOutputChannel out = null;
- private boolean optimizedWrite=true;
+ private boolean optimizedWrite = true;
+
/**
* Creates a new, uninitialized CharChunk object.
@@ -85,10 +87,12 @@ public final class CharChunk implements
public CharChunk() {
}
+
public CharChunk(int size) {
- allocate( size, -1 );
+ allocate(size, -1);
}
+
// --------------------
/**
@@ -97,48 +101,52 @@ public final class CharChunk implements
@Deprecated
public CharChunk getClone() {
try {
- return (CharChunk)this.clone();
- } catch( Exception ex) {
+ return (CharChunk) this.clone();
+ } catch (Exception ex) {
return null;
}
}
+
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;
}
+
/**
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
public void reset() {
- buff=null;
+ buff = null;
}
+
// -------------------- 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;
}
@@ -147,29 +155,33 @@ 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.
+
+ /**
+ * 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.
+ * 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.
*/
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.
*/
@@ -177,50 +189,56 @@ public final class CharChunk implements
this.in = in;
}
- /** When the buffer is full, write the data to the output channel.
- * Also used when large amount of data is appended.
+
+ /**
+ * 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.
+ * If not set, the buffer will grow to the limit.
*/
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;
}
+
/**
- * Returns the start offset of the bytes.
- * For output this is the end of the buffer.
+ * Returns 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;
}
+
/**
* Returns the start offset of the bytes.
*/
public void setOffset(int off) {
- start=off;
+ start = off;
}
+
/**
* Returns the length of the bytes.
*/
public int getLength() {
- return end-start;
+ return end - start;
}
@@ -228,61 +246,60 @@ 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
+
+ /**
+ * Add data to the buffer
*/
- 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;
}
@@ -298,57 +315,57 @@ 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);
}
}
/**
* Add data to the buffer.
+ *
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
- public void append( StringBuilder sb )
- throws IOException
- {
- int len=sb.length();
+ public void append(StringBuilder sb) throws IOException {
+ int len = sb.length();
// 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
- sb.getChars(0, len, buff, end );
- end+=len;
+ sb.getChars(0, len, buff, end);
+ end += len;
return;
}
- int off=0;
+ int off = 0;
int sbOff = off;
int sbEnd = off + len;
while (sbOff < sbEnd) {
int d = min(limit - end, sbEnd - sbOff);
- sb.getChars( sbOff, sbOff+d, buff, end);
+ sb.getChars(sbOff, sbOff + d, buff, end);
sbOff += d;
end += d;
if (end >= limit) {
@@ -357,27 +374,31 @@ public final class CharChunk implements
}
}
- /** Append a string to the buffer
+
+ /**
+ * Append a string to the buffer
*/
public void append(String s) throws IOException {
append(s, 0, s.length());
}
- /** Append a string to the buffer
+
+ /**
+ * Append a string to the buffer
*/
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;
}
@@ -385,7 +406,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) {
@@ -394,10 +415,10 @@ public final class CharChunk implements
}
}
+
// -------------------- Removing data from the buffer --------------------
- public int substract()
- throws IOException {
+ public int substract() throws IOException {
if ((end - start) == 0) {
if (in == null) {
@@ -413,18 +434,18 @@ public final class CharChunk implements
}
+
/**
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
- public int substract(CharChunk src)
- throws IOException {
+ public int substract(CharChunk src) throws IOException {
if ((end - start) == 0) {
if (in == null) {
return -1;
}
- int n = in.realReadChars( buff, end, buff.length - end);
+ int n = in.realReadChars(buff, end, buff.length - end);
if (n < 0) {
return -1;
}
@@ -437,14 +458,14 @@ public final class CharChunk implements
}
- public int substract( char src[], int off, int len )
- throws IOException {
+
+ public int substract(char src[], int off, int len) throws IOException {
if ((end - start) == 0) {
if (in == null) {
return -1;
}
- int n = in.realReadChars( buff, end, buff.length - end);
+ int n = in.realReadChars(buff, end, buff.length - end);
if (n < 0) {
return -1;
}
@@ -461,62 +482,56 @@ 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 );
- }
- out.realWriteChars( buff, start, end - start );
- end=start;
+ public void flushBuffer() throws IOException {
+ // assert out!=null
+ if (out == null) {
+ throw new IOException("Buffer overflow, no sink " + limit + " " +
buff.length);
+ }
+ 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.
+
+ /**
+ * Make space for len chars. If len is small, allocate a reserve space too.
+ * Never grow bigger than limit.
*/
- public void makeSpace(int count)
- {
+ public void makeSpace(int count) {
char[] tmp = null;
int newSize;
- int desiredSize=end + count;
+ int desiredSize = end + count;
// Can't grow above the limit
- if( limit > 0 &&
- desiredSize > limit) {
- desiredSize=limit;
+ if (limit > 0 && desiredSize > limit) {
+ desiredSize = limit;
}
- if( buff==null ) {
- if( desiredSize < 256 )
- {
- desiredSize=256; // take a minimum
+ if (buff == null) {
+ if (desiredSize < 256) {
+ desiredSize = 256; // take a minimum
}
- buff=new char[desiredSize];
+ buff = new char[desiredSize];
}
// limit < buf.length ( the buffer is already big )
// or we already have space XXX
- if( desiredSize <= buff.length) {
+ if (desiredSize <= buff.length) {
return;
}
// grow in larger chunks
- if( desiredSize < 2 * buff.length ) {
- newSize= buff.length * 2;
- if( limit >0 &&
- newSize > limit ) {
- newSize=limit;
+ if (desiredSize < 2 * buff.length) {
+ newSize = buff.length * 2;
+ if (limit > 0 && newSize > limit) {
+ newSize = limit;
}
- tmp=new char[newSize];
+ tmp = new char[newSize];
} else {
- newSize= buff.length * 2 + count ;
- if( limit > 0 &&
- newSize > limit ) {
- newSize=limit;
+ newSize = buff.length * 2 + count;
+ if (limit > 0 && newSize > limit) {
+ newSize = limit;
}
- tmp=new char[newSize];
+ tmp = new char[newSize];
}
System.arraycopy(buff, 0, tmp, 0, end);
@@ -524,32 +539,34 @@ 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);
}
+
/**
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
- public int getInt()
- {
- return Ascii.parseInt(buff, start,
- end-start);
+ public int getInt() {
+ return Ascii.parseInt(buff, start, end - start);
}
+
// -------------------- equals --------------------
@Override
@@ -560,14 +577,16 @@ public final class CharChunk 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
*/
public boolean equals(String s) {
char[] c = buff;
- int len = end-start;
+ int len = end - start;
if (c == null || len != s.length()) {
return false;
}
@@ -580,42 +599,46 @@ public final class CharChunk 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) {
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;
}
@@ -623,38 +646,41 @@ public final class CharChunk implements
return true;
}
+
/**
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
public boolean equals(byte b2[], int off2, int len2) {
- char b1[]=buff;
- if( b2==null && b1==null ) {
+ char b1[] = buff;
+ if (b2 == null && b1 == 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;
+ int len = end - start;
- while ( len-- > 0) {
- if ( b1[off1++] != (char)b2[off2++]) {
+ while (len-- > 0) {
+ if (b1[off1++] != (char) b2[off2++]) {
return false;
}
}
return true;
}
+
/**
* Returns true 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;
@@ -666,19 +692,21 @@ public final class CharChunk implements
return true;
}
+
/**
* Returns true if the message bytes starts with the specified string.
+ *
* @param s the string
*/
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;
}
}
@@ -688,12 +716,13 @@ public final class CharChunk implements
/**
* Returns true 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;
@@ -705,7 +734,8 @@ public final class CharChunk implements
return true;
}
- // -------------------- Hash code --------------------
+
+ // -------------------- Hash code --------------------
@Override
public int hashCode() {
@@ -720,45 +750,50 @@ 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;
}
+
/**
* @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
*/
@Deprecated
public int hashIgnoreCase() {
- int code=0;
+ int code = 0;
for (int i = start; i < end; i++) {
code = code * 37 + Ascii.toLower(buff[i]);
}
return code;
}
+
public int indexOf(char c) {
- return indexOf( c, start);
+ return indexOf(c, start);
}
+
/**
* Returns true if the message bytes starts with the specified string.
+ *
* @param c the character
*/
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++;
@@ -767,31 +802,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) {
@@ -800,6 +835,7 @@ public final class CharChunk implements
return b;
}
+
// Char sequence impl
@Override
@@ -807,6 +843,7 @@ public final class CharChunk implements
return buff[index + start];
}
+
@Override
public CharSequence subSequence(int start, int end) {
try {
@@ -820,6 +857,7 @@ public final class CharChunk implements
}
}
+
@Override
public int length() {
return end - start;
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]