Author: markt
Date: Tue Jan 16 12:10:41 2018
New Revision: 1821234
URL: http://svn.apache.org/viewvc?rev=1821234&view=rev
Log:
Clean up - No functional change
Align comments and variable names, re-order methods, etc. to minimise diff
between ByteChunk and CharChunk
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=1821234&r1=1821233&r2=1821234&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java Tue Jan 16
12:10:41 2018
@@ -70,9 +70,9 @@ public final class ByteChunk extends Abs
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
+ * Same as java.nio.channels.ReadableByteChannel
*/
public static interface ByteInputChannel {
@@ -81,12 +81,15 @@ public final class ByteChunk extends Abs
*
* @return The number of bytes read
*
- * @throws IOException If an I/O occurs while reading the bytes
+ * @throws IOException If an I/O error occurs during reading
*/
public int realReadBytes() throws IOException;
}
/**
+ * When we need more space we'll either grow the buffer ( up to the limit )
+ * or send it to a channel.
+ *
* Same as java.nio.channel.WritableByteChannel.
*/
public static interface ByteOutputChannel {
@@ -95,12 +98,12 @@ public final class ByteChunk extends Abs
* Send the bytes ( usually the internal conversion buffer ). Expect 8k
* output if the buffer is full.
*
- * @param cbuf bytes that will be written
+ * @param buf 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 buf[], int off, int len) throws
IOException;
/**
@@ -122,12 +125,13 @@ public final class ByteChunk extends Abs
*/
public static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;
+ private transient Charset charset;
+
// byte[]
private byte[] buff;
- private transient Charset charset;
-
- // How much can it grow, when data is added
+ // -1: grow indefinitely
+ // maximum amount to be cached
private int limit = -1;
// transient as serialization is primarily for values via, e.g. JMX
@@ -139,7 +143,6 @@ public final class ByteChunk extends Abs
* Creates a new, uninitialized ByteChunk object.
*/
public ByteChunk() {
- // NO-OP
}
@@ -188,7 +191,7 @@ public final class ByteChunk extends Abs
/**
- * Sets the message bytes to the specified subarray of bytes.
+ * Sets the buffer to the specified subarray of bytes.
*
* @param b the ascii bytes
* @param off the start offset of the bytes
@@ -217,7 +220,7 @@ public final class ByteChunk extends Abs
/**
- * @return the message bytes.
+ * @return the buffer.
*/
public byte[] getBytes() {
return getBuffer();
@@ -225,7 +228,7 @@ public final class ByteChunk extends Abs
/**
- * @return the message bytes.
+ * @return the buffer.
*/
public byte[] getBuffer() {
return buff;
@@ -605,20 +608,21 @@ public final class ByteChunk extends Abs
* Compares the message bytes to the specified String object.
*
* @param s the String to compare
- * @return true if the comparison succeeded, false otherwise
+ * @return <code>true</code> if the comparison succeeded,
<code>false</code>
+ * otherwise
*/
public boolean equals(String s) {
// XXX ENCODING - this only works if encoding is UTF8-compat
// ( ok for tomcat, where we compare ascii - header names, etc )!!!
byte[] b = buff;
- int blen = end - start;
- if (b == null || blen != s.length()) {
+ int len = end - start;
+ if (b == null || len != s.length()) {
return false;
}
- int boff = start;
- for (int i = 0; i < blen; i++) {
- if (b[boff++] != s.charAt(i)) {
+ int off = start;
+ for (int i = 0; i < len; i++) {
+ if (b[off++] != s.charAt(i)) {
return false;
}
}
@@ -704,10 +708,11 @@ public final class ByteChunk extends Abs
/**
- * Returns true if the message bytes starts with the specified string.
+ * Returns true if the buffer starts with the specified string.
*
* @param s the string
* @param pos The position
+ *
* @return <code>true</code> if the start matches
*/
public boolean startsWithIgnoreCase(String s, int pos) {
@@ -777,19 +782,19 @@ public final class ByteChunk extends Abs
* 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
+ * @param bytes The array to search
+ * @param start The point to start searching from in the array
+ * @param end The point to stop searching in the array
+ * @param s 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) {
+ public static int indexOf(byte bytes[], int start, int end, char s) {
int offset = start;
while (offset < end) {
byte b = bytes[offset];
- if (b == c) {
+ if (b == s) {
return offset;
}
offset++;
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=1821234&r1=1821233&r2=1821234&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java Tue Jan 16
12:10:41 2018
@@ -32,7 +32,9 @@ public final class CharChunk extends Abs
private static final long serialVersionUID = 1L;
- // Input interface, used when the buffer is emptied.
+ /**
+ * Input interface, used when the buffer is empty.
+ */
public static interface CharInputChannel {
/**
@@ -40,7 +42,7 @@ public final class CharChunk extends Abs
*
* @return The number of characters read
*
- * @throws IOException If an I/O error occurs reading the characters
+ * @throws IOException If an I/O error occurs during reading
*/
public int realReadChars() throws IOException;
}
@@ -55,18 +57,18 @@ public final class CharChunk extends Abs
* Send the bytes ( usually the internal conversion buffer ). Expect 8k
* output if the buffer is full.
*
- * @param cbuf characters that will be written
+ * @param buf 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 buf[], int off, int len) throws
IOException;
}
// --------------------
// char[]
- private char buff[];
+ private char[] buff;
// -1: grow indefinitely
// maximum amount to be cached
@@ -86,8 +88,8 @@ public final class CharChunk extends Abs
}
- public CharChunk(int size) {
- allocate(size, -1);
+ public CharChunk(int initial) {
+ allocate(initial, -1);
}
@@ -118,6 +120,13 @@ public final class CharChunk extends Abs
}
+ /**
+ * Sets the buffer to the specified subarray of characters.
+ *
+ * @param c the characters
+ * @param off the start offset of the characters
+ * @param len the length of the characters
+ */
public void setChars(char[] c, int off, int len) {
buff = c;
start = off;
@@ -127,12 +136,17 @@ public final class CharChunk extends Abs
}
- // compat
+ /**
+ * @return the buffer.
+ */
public char[] getChars() {
return getBuffer();
}
+ /**
+ * @return the buffer.
+ */
public char[] getBuffer() {
return buff;
}
@@ -359,6 +373,12 @@ public final class CharChunk extends Abs
}
+ /**
+ * Send the buffer to the sink. Called by append() when the limit is
+ * reached. You can also call it explicitly to force the data to be
written.
+ *
+ * @throws IOException Writing overflow data to the output channel failed
+ */
public void flushBuffer() throws IOException {
// assert out!=null
if (out == null) {
@@ -541,10 +561,12 @@ public final class CharChunk extends Abs
/**
- * @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
+ * Returns true if the buffer starts with the specified string.
+ *
+ * @param s the string
+ * @param pos The position
+ *
+ * @return <code>true</code> if the start matches
*/
public boolean startsWithIgnoreCase(String s, int pos) {
char[] c = buff;
@@ -583,41 +605,6 @@ public final class CharChunk extends Abs
}
- @Override
- protected int getBufferElement(int index) {
- return buff[index];
- }
-
-
- public int indexOf(char c) {
- return indexOf(c, start);
- }
-
-
- /**
- * @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);
- 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) {
- return off;
- }
- off++;
- }
- return -1;
- }
-
-
public int indexOf(String src, int srcOff, int srcLen, int myOff) {
char first = src.charAt(srcOff);
@@ -641,6 +628,58 @@ public final class CharChunk extends Abs
}
return -1;
}
+
+
+ @Override
+ protected int getBufferElement(int index) {
+ return buff[index];
+ }
+
+
+ public int indexOf(char c) {
+ return indexOf(c, start);
+ }
+
+
+ /**
+ * Returns the first instance of the given character in this CharChunk
+ * starting at the specified char. If the character is not found, -1 is
+ * returned. <br>
+ *
+ * @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 char
array
+ * between the specified start and end. <br>
+ *
+ * @param chars The array to search
+ * @param start The point to start searching from in the array
+ * @param end The point to stop searching in the array
+ * @param s 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(char chars[], int start, int end, char s) {
+ int offset = start;
+
+ while (offset < end) {
+ char c = chars[offset];
+ if (c == s) {
+ return offset;
+ }
+ offset++;
+ }
+ return -1;
+ }
// -------------------- utils
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]