olegk 2003/02/12 04:54:24
Modified: httpclient/src/java/org/apache/commons/httpclient
HttpConstants.java
Log:
- Added methods that accept byte array offset and length parameters when performing
byte array to String conversion
Contributed by Oleg Kalnichevski
Revision Changes Path
1.7 +71 -12
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConstants.java
Index: HttpConstants.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConstants.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- HttpConstants.java 8 Feb 2003 19:22:49 -0000 1.6
+++ HttpConstants.java 12 Feb 2003 12:54:24 -0000 1.7
@@ -119,16 +119,18 @@
* response headers)
*
* @param data the byte array to be encoded
+ * @param offset the index of the first byte to encode
+ * @param length the number of bytes to encode
* @return The resulting string.
*/
- public static String getString(final byte[] data) {
+ public static String getString(final byte[] data, int offset, int length) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null");
}
try {
- return new String(data, HTTP_ELEMENT_CHARSET);
+ return new String(data, offset, length, HTTP_ELEMENT_CHARSET);
} catch (UnsupportedEncodingException e) {
if (LOG.isWarnEnabled()) {
@@ -137,11 +139,23 @@
+ ". System default encoding used");
}
- return new String(data);
+ return new String(data, offset, length);
}
}
/**
+ * Converts the byte array of HTTP element characters to a string This
+ * method is to be used when decoding content of HTTP elements (such as
+ * response headers)
+ *
+ * @param data the byte array to be encoded
+ * @return The resulting string.
+ */
+ public static String getString(final byte[] data) {
+ return getString(data, 0, data.length);
+ }
+
+ /**
* Converts the specified string to a byte array of HTTP content charachetrs
* This method is to be used when encoding content of HTTP request/response
* If the specified charset is not supported, default HTTP content encoding
@@ -193,10 +207,12 @@
* (ISO-8859-1) is applied
*
* @param data the byte array to be encoded
+ * @param offset the index of the first byte to encode
+ * @param length the number of bytes to encode
* @param charset the desired character encoding
* @return The result of the conversion.
*/
- public static String getContentString(final byte[] data, String charset) {
+ public static String getContentString(final byte[] data, int offset, int
length, String charset) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null");
@@ -207,7 +223,7 @@
}
try {
- return new String(data, charset);
+ return new String(data, offset, length, charset);
} catch (UnsupportedEncodingException e) {
if (LOG.isWarnEnabled()) {
@@ -217,7 +233,7 @@
}
try {
- return new String(data, DEFAULT_CONTENT_CHARSET);
+ return new String(data, offset, length, DEFAULT_CONTENT_CHARSET);
} catch (UnsupportedEncodingException e2) {
if (LOG.isWarnEnabled()) {
@@ -226,11 +242,26 @@
+ ". System encoding used");
}
- return new String(data);
+ return new String(data, offset, length);
}
}
}
+
+ /**
+ * Converts the byte array of HTTP content characters to a string This
+ * method is to be used when decoding content of HTTP request/response If
+ * the specified charset is not supported, default HTTP content encoding
+ * (ISO-8859-1) is applied
+ *
+ * @param data the byte array to be encoded
+ * @param charset the desired character encoding
+ * @return The result of the conversion.
+ */
+ public static String getContentString(final byte[] data, String charset) {
+ return getContentString(data, 0, data.length);
+ }
+
/**
* Converts the specified string to a byte array of HTTP content characters
* using default HTTP content encoding (ISO-8859-1) This method is to be
@@ -249,6 +280,20 @@
* decoding content of HTTP request/response
*
* @param data the byte array to be encoded
+ * @param offset the index of the first byte to encode
+ * @param length the number of bytes to encode
+ * @return The string representation of the byte array.
+ */
+ public static String getContentString(final byte[] data, int offset, int
length) {
+ return getContentString(data, offset, length, null);
+ }
+
+ /**
+ * Converts the byte array of HTTP content characters to a string using
+ * default HTTP content encoding (ISO-8859-1) This method is to be used when
+ * decoding content of HTTP request/response
+ *
+ * @param data the byte array to be encoded
* @return The string representation of the byte array.
*/
public static String getContentString(final byte[] data) {
@@ -280,18 +325,32 @@
* headers)
*
* @param data the byte array to be encoded
+ * @param offset the index of the first byte to encode
+ * @param length the number of bytes to encode
* @return The string representation of the byte array
*/
- public static String getAsciiString(final byte[] data) {
+ public static String getAsciiString(final byte[] data, int offset, int length) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null");
}
try {
- return new String(data, "US-ASCII");
+ return new String(data, offset, length, "US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("HttpClient requires ASCII support");
}
+ }
+
+ /**
+ * Converts the byte array of ASCII characters to a string. This method is
+ * to be used when decoding content of HTTP elements (such as response
+ * headers)
+ *
+ * @param data the byte array to be encoded
+ * @return The string representation of the byte array
+ */
+ public static String getAsciiString(final byte[] data) {
+ return getAsciiString(data, 0, data.length);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]