You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@locus.apache.org on 2000/05/24 20:57:12 UTC
cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util MessageChars.java Ascii.java MessageBytes.java MessageString.java MimeHeaderField.java
costin 00/05/24 11:57:11
Modified: src/share/org/apache/tomcat/util Ascii.java
MessageBytes.java MessageString.java
MimeHeaderField.java
Added: src/share/org/apache/tomcat/util MessageChars.java
Log:
- MessageString no longer extends MessageBytes. It was nice, but the conversion was out
of control.
- added MessageChars - it's similar with MessageString and MessageBytes
- MimeHeaderFileds will make explicit distinction between the MessageXXX ( it also have
to deal with int and date types ).
- MessageBytes no longer extends Ascii - Ascii contains general static methods, we
just use them.
- removed almost all methods that did implicit conversions between char[] and byte[].
There are still few, but will go away.
- MessageXXX are now "light", and almost ready for use
Revision Changes Path
1.4 +36 -7 jakarta-tomcat/src/share/org/apache/tomcat/util/Ascii.java
Index: Ascii.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/Ascii.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Ascii.java 2000/05/24 16:34:20 1.3
+++ Ascii.java 2000/05/24 18:57:10 1.4
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/Ascii.java,v 1.3 2000/05/24 16:34:20 costin Exp $
- * $Revision: 1.3 $
- * $Date: 2000/05/24 16:34:20 $
+ * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/Ascii.java,v 1.4 2000/05/24 18:57:10 costin Exp $
+ * $Revision: 1.4 $
+ * $Date: 2000/05/24 18:57:10 $
*
* ====================================================================
*
@@ -72,6 +72,8 @@
*/
public class Ascii {
+ static StringManager sm =
+ StringManager.getManager("org.apache.tomcat.util");
/*
* Character translation tables.
*/
@@ -192,10 +194,7 @@
int c;
if (b == null || len <= 0 || !isDigit(c = b[off++])) {
- StringManager sm =
- StringManager.getManager("org.apache.tomcat.util");
String msg = sm.getString("ascii.parseInit.nfe", b);
-
throw new NumberFormatException(msg);
}
@@ -209,10 +208,40 @@
throw new NumberFormatException(msg);
}
-
n = n * 10 + c - '0';
}
return n;
}
+
+ /**
+ * Compares this message string to the specified subarray of bytes.
+ * Case is ignored in the comparison.
+ * @param b the bytes to compare
+ * @param off the start offset of the bytes
+ * @param len the length of the bytes
+ * @return true if the comparison succeeded, false otherwise
+ */
+ public static boolean equalsIgnoreCase(String str, MessageBytes mB ) {
+ byte[] b=mB.getBytes();
+ int off=mB.getOffset();
+ int len=mB.getLength();
+ if (str != null) {
+ String s = str;
+ if (len != s.length()) {
+ return false;
+ }
+ for (int i = 0; i < len; i++) {
+ if (Ascii.toLower(b[off++]) != Ascii.toLower(s.charAt(i))) {
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+
+
+
+
}
1.5 +75 -74 jakarta-tomcat/src/share/org/apache/tomcat/util/MessageBytes.java
Index: MessageBytes.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageBytes.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- MessageBytes.java 2000/05/24 17:19:54 1.4
+++ MessageBytes.java 2000/05/24 18:57:10 1.5
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageBytes.java,v 1.4 2000/05/24 17:19:54 costin Exp $
- * $Revision: 1.4 $
- * $Date: 2000/05/24 17:19:54 $
+ * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageBytes.java,v 1.5 2000/05/24 18:57:10 costin Exp $
+ * $Revision: 1.5 $
+ * $Date: 2000/05/24 18:57:10 $
*
* ====================================================================
*
@@ -74,9 +74,8 @@
* @author dac@eng.sun.com
* @author James Todd [gonzo@eng.sun.com]
*/
+public class MessageBytes {
-public class MessageBytes extends Ascii {
-
private StringManager sm =
StringManager.getManager("org.apache.tomcat.util");
@@ -171,6 +170,76 @@
}
/**
+ * Returns the message bytes parsed as an unsigned integer.
+ * @exception NumberFormatException if the integer format was invalid
+ */
+ public int toInteger() throws NumberFormatException {
+ return Ascii.parseInt(bytes, offset, length);
+ }
+
+ /**
+ * Compares the message bytes to the specified subarray of bytes.
+ * @param b the bytes to compare
+ * @param off the start offset of the bytes
+ * @param len the length of the bytes
+ * @return true if the comparison succeeded, false otherwise
+ */
+ public boolean equals(byte[] b, int off, int len) {
+ byte[] b1 = bytes;
+ if (b1 == null || len != length) {
+ return false;
+ }
+ int off1 = offset;
+ while (len-- > 0) {
+ if (b[off++] != b1[off1++]) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Compares the message bytes to the specified subarray of bytes.
+ * Case is ignored in the comparison.
+ * @param b the bytes to compare
+ * @param off the start offset of the bytes
+ * @param len the length of the bytes
+ * @return true if the comparison succeeded, false otherwise
+ */
+ public boolean equalsIgnoreCase(byte[] b, int off, int len) {
+ byte[] b1 = bytes;
+ if (b1 == null || len != length) {
+ return false;
+ }
+ int off1 = offset;
+ while (len-- > 0) {
+ if (Ascii.toLower(b[off++]) != Ascii.toLower(b1[off1++])) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Writes the message bytes to the specified output stream.
+ * @param out the output stream
+ * @exception IOException if an I/O error has occurred
+ */
+ public void write(OutputStream out) throws IOException {
+ if (bytes != null) {
+ out.write(bytes, offset, length);
+ }
+ }
+
+ /**
+ * Returns the length of the message bytes.
+ */
+ public int length() {
+ return bytes != null ? length : 0;
+ }
+
+ // --------------------
+ /**
* Returns the message bytes as a String object.
*/
public String toString() {
@@ -186,14 +255,6 @@
}
/**
- * Returns the message bytes parsed as an unsigned integer.
- * @exception NumberFormatException if the integer format was invalid
- */
- public int toInteger() throws NumberFormatException {
- return parseInt(bytes, offset, length);
- }
-
- /**
* Compares the message bytes to the specified String object.
* @param s the String to compare
* @return true if the comparison succeeded, false otherwise
@@ -227,28 +288,7 @@
}
int off = offset;
for (int i = 0; i < len; i++) {
- if (toLower(b[off++]) != toLower((byte)s.charAt(i))) {
- return false;
- }
- }
- return true;
- }
-
- /**
- * Compares the message bytes to the specified subarray of bytes.
- * @param b the bytes to compare
- * @param off the start offset of the bytes
- * @param len the length of the bytes
- * @return true if the comparison succeeded, false otherwise
- */
- public boolean equals(byte[] b, int off, int len) {
- byte[] b1 = bytes;
- if (b1 == null || len != length) {
- return false;
- }
- int off1 = offset;
- while (len-- > 0) {
- if (b[off++] != b1[off1++]) {
+ if (Ascii.toLower(b[off++]) != Ascii.toLower((byte)s.charAt(i))) {
return false;
}
}
@@ -256,28 +296,6 @@
}
/**
- * Compares the message bytes to the specified subarray of bytes.
- * Case is ignored in the comparison.
- * @param b the bytes to compare
- * @param off the start offset of the bytes
- * @param len the length of the bytes
- * @return true if the comparison succeeded, false otherwise
- */
- public boolean equalsIgnoreCase(byte[] b, int off, int len) {
- byte[] b1 = bytes;
- if (b1 == null || len != length) {
- return false;
- }
- int off1 = offset;
- while (len-- > 0) {
- if (toLower(b[off++]) != toLower(b1[off1++])) {
- return false;
- }
- }
- return true;
- }
-
- /**
* Returns true if the message bytes starts with the specified string.
* @param s the string
*/
@@ -296,21 +314,4 @@
return true;
}
- /**
- * Writes the message bytes to the specified output stream.
- * @param out the output stream
- * @exception IOException if an I/O error has occurred
- */
- public void write(OutputStream out) throws IOException {
- if (bytes != null) {
- out.write(bytes, offset, length);
- }
- }
-
- /**
- * Returns the length of the message bytes.
- */
- public int length() {
- return bytes != null ? length : 0;
- }
}
1.4 +17 -119 jakarta-tomcat/src/share/org/apache/tomcat/util/MessageString.java
Index: MessageString.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageString.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MessageString.java 2000/05/24 17:19:55 1.3
+++ MessageString.java 2000/05/24 18:57:10 1.4
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageString.java,v 1.3 2000/05/24 17:19:55 costin Exp $
- * $Revision: 1.3 $
- * $Date: 2000/05/24 17:19:55 $
+ * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageString.java,v 1.4 2000/05/24 18:57:10 costin Exp $
+ * $Revision: 1.4 $
+ * $Date: 2000/05/24 18:57:10 $
*
* ====================================================================
*
@@ -70,12 +70,11 @@
import javax.servlet.ServletOutputStream;
/**
- * This class is used to represent a string in an HTTP message,
- * and can either be an actual String or a subarray of bytes.
+ * This class is used to represent a string in an HTTP message.
*
* @author dac@eng.sun.com
*/
-public class MessageString extends MessageBytes {
+public class MessageString {
/**
* The message String.
*/
@@ -96,20 +95,9 @@
}
/**
- * Creates a new message string with the specified bytes.
- * @param b the bytes
- * @param off the offset of the bytes
- * @param len the length of the bytes
- */
- public MessageString(byte[] b, int off, int len) {
- super(b, off, len);
- }
-
- /**
* Resets the message string to an uninitialized state.
*/
public void reset() {
- super.reset();
str = null;
}
@@ -118,53 +106,31 @@
* @param s the String
*/
public void setString(String s) {
- super.reset();
str = s;
}
/**
- * Sets the message string to the specified bytes.
- * @param b the bytes
- * @param off the offset of the bytes
- * @param len the length of the bytes
- */
- public void setBytes(byte[] b, int off, int len) {
- super.setBytes(b, off, len);
- str = null;
- }
-
- /**
* Returns true if the message string is set.
*/
public boolean isSet() {
- return str != null || super.isSet();
+ return str != null;
}
- /**
- * Get the bytes of this message string in buf starting at buf_offset.
- * @return the number of bytes added to buf.
- */
- public int getBytes(byte buf[],
- int buf_offset)
+ public int getChars( char buf[], int buf_off )
{
if (str != null) {
- int len = str.length();
- // deprecated line (kept for reference)
- //str.getBytes(0, len, buf, buf_offset);
- byte[] strBytes = str.getBytes();
- System.arraycopy(strBytes, 0, buf, buf_offset, len);
- return len;
- }
- else {
- return super.getBytes(buf, buf_offset);
+ int length = str.length();
+ str.getChars( 0, length, buf, buf_off);
+ return length;
}
+ return 0;
}
-
+
/**
* Returns the message string as a String object.
*/
public String toString() {
- return str != null ? str : super.toString();
+ return str;
}
/**
@@ -172,7 +138,7 @@
* @exception NumberFormatException if the integer format was invalid
*/
public int toInteger() throws NumberFormatException {
- return str != null ? Integer.parseInt(str) : super.toInteger();
+ return str != null ? Integer.parseInt(str) : -1;
}
/**
@@ -181,7 +147,7 @@
* @return true if the comparison succeeded, false otherwise
*/
public boolean equals(String s) {
- return str != null ? str.equals(s) : super.equals(s);
+ return str != null ? str.equals(s) : null==s;
}
/**
@@ -191,81 +157,13 @@
* @return true if the comparison succeeded, false otherwise
*/
public boolean equalsIgnoreCase(String s) {
- return str != null ? str.equalsIgnoreCase(s) : super.equalsIgnoreCase(s);
- }
-
- /**
- * Compares this message string to the specified subarray of bytes.
- * @param b the bytes to compare
- * @param off the start offset of the bytes
- * @param len the length of the bytes
- * @return true if the comparison succeeded, false otherwise
- */
- public boolean equals(byte[] b, int off, int len) {
- if (str != null) {
- String s = str;
- if (len != s.length()) {
- return false;
- }
- for (int i = 0; i < len; i++) {
- if (b[off++] != s.charAt(i)) {
- return false;
- }
- }
- return true;
- }
- return super.equals(b, off, len);
- }
-
- /**
- * Compares this message string to the specified subarray of bytes.
- * Case is ignored in the comparison.
- * @param b the bytes to compare
- * @param off the start offset of the bytes
- * @param len the length of the bytes
- * @return true if the comparison succeeded, false otherwise
- */
- public boolean equalsIgnoreCase(byte[] b, int off, int len) {
- if (str != null) {
- String s = str;
- if (len != s.length()) {
- return false;
- }
- for (int i = 0; i < len; i++) {
- if (toLower(b[off++]) != toLower(s.charAt(i))) {
- return false;
- }
- }
- return true;
- }
- return super.equalsIgnoreCase(b, off, len);
- }
-
- /**
- * Returns true if the message string starts with the specified string.
- * @param s the string
- */
- public boolean startsWith(String s) {
- return str != null ? str.startsWith(s) : super.startsWith(s);
- }
-
- /**
- * Writes the message string to the specified servlet output stream.
- * @param out the servlet output stream
- * @exception IOException if an I/O error has occurred
- */
- public void write(ServletOutputStream out) throws IOException {
- if (str != null) {
- out.print(str);
- } else {
- super.write(out);
- }
+ return str.equalsIgnoreCase(s);
}
/**
* Returns the length of the message string.
*/
public int length() {
- return str != null ? str.length() : super.length();
+ return str != null ? str.length() : 0;
}
}
1.9 +106 -54 jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaderField.java
Index: MimeHeaderField.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaderField.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- MimeHeaderField.java 2000/05/24 17:19:55 1.8
+++ MimeHeaderField.java 2000/05/24 18:57:10 1.9
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaderField.java,v 1.8 2000/05/24 17:19:55 costin Exp $
- * $Revision: 1.8 $
- * $Date: 2000/05/24 17:19:55 $
+ * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaderField.java,v 1.9 2000/05/24 18:57:10 costin Exp $
+ * $Revision: 1.9 $
+ * $Date: 2000/05/24 18:57:10 $
*
* ====================================================================
*
@@ -91,35 +91,35 @@
/**
* The header field name.
*/
+ protected final MessageBytes nameB = new MessageBytes();
+ protected final MessageChars nameC = new MessageChars();
protected final MessageString name = new MessageString();
/**
* The header field value.
*/
+ protected final MessageBytes valueB = new MessageBytes();
+ protected final MessageChars valueC = new MessageChars();
protected final MessageString value = new MessageString();
- /**
- * The header field integer value.
- */
+ /** The header field integer value. */
protected int intValue;
- /**
- * The header field Date value.
- */
+ /** The header field Date value. */
protected Date dateValue = null;
- StringBuffer sb=null;
- // Will be used to conver date value - _never_ call toString()
-
/**
* The header field value type.
*/
protected int type = T_NULL;
-
+ protected int nameType=T_NULL;
+
protected static final int T_NULL = 0;
protected static final int T_STR = 1;
protected static final int T_INT = 2;
protected static final int T_DATE = 3;
+ protected static final int T_BYTES = 4;
+ protected static final int T_CHARS = 5;
/**
* Creates a new, uninitialized header field.
@@ -133,31 +133,75 @@
public void reset() {
name.reset();
value.reset();
+ nameB.reset();
+ valueB.reset();
+ nameC.reset();
+ valueC.reset();
type = T_NULL;
+ nameType = T_NULL;
}
- public int getType() {
- return type;
- }
-
/**
* Sets the header field name to the specified string.
* @param s the header field name String
*/
public void setName(String s) {
+ nameType = T_STR;
name.setString(s);
}
/**
+ * Sets the header field name to the specified string.
+ * @param s the header field name String
+ */
+ public void setName(char c[], int off, int len ) {
+ nameType = T_CHARS;
+ nameC.setChars(c, off, len);
+ }
+
+ /**
* Sets the header field name to the specified subarray of bytes.
* @param b the header field name bytes
* @param off the start offset of the bytes
* @param len the length of the bytes
*/
public void setName(byte[] b, int off, int len) {
- name.setBytes(b, off, len);
+ nameType = T_BYTES;
+ nameB.setBytes(b, off, len);
+ }
+
+ public int getNameType() {
+ return nameType;
+ }
+
+ /**
+ * Returns the header field name as a String.
+ * @ deprecated - no encoding support
+ */
+ public String getName() {
+ switch (nameType) {
+ case T_STR:
+ return name.toString();
+ case T_CHARS:
+ return nameC.toString();
+ case T_BYTES:
+ return nameB.toString(); // XXX encoding
+ default:
+ return null;
+ }
+ }
+
+ public MessageBytes getNameBytes() {
+ return nameB;
+ }
+
+ public MessageChars getNameChars() {
+ return nameC;
}
+ // -------------------- Value --------------------
+
+
/**
* Sets the header field value to the specified string.
* @param s the header field value String
@@ -174,8 +218,13 @@
* @param len the length of the bytes
*/
public void setValue(byte[] b, int off, int len) {
- value.setBytes(b, off, len);
- type = T_STR;
+ valueB.setBytes(b, off, len);
+ type = T_BYTES;
+ }
+
+ public void setValue(char[] b, int off, int len) {
+ valueC.setChars(b, off, len);
+ type = T_CHARS;
}
/**
@@ -200,14 +249,8 @@
}
/**
- * Returns the header field name as a String.
- */
- public String getName() {
- return name.toString();
- }
-
- /**
* Returns the header field value as a String, or null if not set.
+ * @deprecated No encoding support
*/
public String getValue() {
switch (type) {
@@ -217,17 +260,21 @@
return String.valueOf(intValue);
case T_DATE:
return formatDate(dateValue);
+ case T_CHARS:
+ return valueC.toString();
+ case T_BYTES:
+ return valueB.toString(); // XXX encoding
default:
return null;
}
}
- public MessageString getNameMessageString() {
- return name;
+ public MessageBytes getValueBytes() {
+ return valueB;
}
- public MessageString getValueMessageString() {
- return value;
+ public MessageChars getValueChars() {
+ return valueC;
}
/**
@@ -242,6 +289,10 @@
return intValue;
case T_STR:
return value.toInteger();
+ case T_CHARS:
+ return valueC.toInteger();
+ case T_BYTES:
+ return valueB.toInteger();
default:
String msg = sm.getString("mimeHeaderField.int.nfe");
@@ -263,15 +314,29 @@
return dateValue.getTime();
case T_STR:
return parseDate( value );
+ case T_CHARS:
+ return parseDate( value.toString() );
+ case T_BYTES:
+ return parseDate( valueB.toString() ); // XXX Encoding
}
String msg = sm.getString("mimeHeaderField.date.iae");
throw new IllegalArgumentException(msg);
}
+
+ public int getValueType() {
+ return type;
+ }
String formatDate( Date value ) {
return DateTool.rfc1123Format.format(value);
}
+ long parseDate( String s ) {
+ // XXX inefficient
+ value.setString( s );
+ return parseDate( value );
+ }
+
long parseDate( MessageString value ) {
String dateString=value.toString();
Date date=null;
@@ -300,29 +365,16 @@
* @param s the string to compare
*/
public boolean nameEquals(String s) {
- return name.equalsIgnoreCase(s);
- }
-
- /**
- * Returns true if the header field has the specified name. Character
- * case is ignored in the comparison.
- * @param b the bytes to compare
- * @param off the start offset of the bytes
- * @param len the length of the bytes
- */
- public boolean nameEquals(byte[] b, int off, int len) {
- return name.equalsIgnoreCase(b, off, len);
+ switch (type) {
+ case T_STR:
+ return name.equalsIgnoreCase(s);
+ case T_CHARS:
+ return nameC.equalsIgnoreCase(s);
+ case T_BYTES:
+ return Ascii.equalsIgnoreCase( s, nameB );
+ default:
+ return false;
+ }
}
- /**
- * Returns a string representation of the header field.
- */
- public String toString() {
- StringBuffer sb = new StringBuffer();
-
- sb.append(name.toString());
- sb.append(": ");
- sb.append( getValue() );
- return sb.toString();
- }
}
1.1 jakarta-tomcat/src/share/org/apache/tomcat/util/MessageChars.java
Index: MessageChars.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.tomcat.util;
import java.io.OutputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
/**
* This class is used to represent a char region in an HTTP message.
*
* @author Costin Manolache costin@eng.sun.com
*/
public final class MessageChars {
// cache for toString conversions
String str;
/** The message chars. */
char[] chars;
/** The start offset */
int offset;
/** The length */
int length;
/**
* Creates a new, uninitialized message string.
*/
public MessageChars() {
}
/**
* Creates a new message string with the specified chars
* @param b the chars
* @param off the offset
* @param len the length
*/
public MessageChars(char[] c, int off, int len) {
setChars( c, off, len );
}
/**
* Resets the message string to an uninitialized state.
*/
public void reset() {
chars=null;
str = null;
length=0;
}
/**
* Sets the message string to the specified bytes.
* @param b the bytes
* @param off the offset of the bytes
* @param len the length of the bytes
*/
public void setChars(char[] b, int off, int len) {
chars=b;
this.offset=off;
this.length=len;
str = null;
}
/**
* Returns true if the message string is set.
*/
public boolean isSet() {
return chars != null;
}
public int getChars( char buf[], int buf_off )
{
System.arraycopy(chars, offset, buf, buf_off, length);
return length;
}
/**
* Returns the message string as a String object.
*/
public String toString() {
if( str != null )
return str;
str=new String( chars, offset, length);
return str;
}
/**
* Returns the message string parsed as an unsigned integer.
* @exception NumberFormatException if the integer format was invalid
*/
public int toInteger() throws NumberFormatException {
toString(); // str will be updated
return Integer.parseInt(str);
}
/**
* Compares this message string to the specified String object.
* @param s the String to compare
* @return true if the comparison succeeded, false otherwise
*/
public boolean equals(String s) {
toString();
return str.equals(s);
}
/**
* Compares this message string to the specified String object. Case is
* ignored in the comparison.
* @param s the String to compare
* @return true if the comparison succeeded, false otherwise
*/
public boolean equalsIgnoreCase(String s) {
toString();
return str.equalsIgnoreCase(s);
}
/**
* Returns the length of the message string.
*/
public int length() {
return length;
}
}