jericho 01/04/26 19:23:06
Modified: src/webdav/client/src/org/apache/webdav/util URIUtil.java
Log:
- Add the escape(byte[]) method.
- Fix a bug to cast the get method of BitSet class.
- Fix a bug to handle the null string.
Revision Changes Path
1.5 +43 -50
jakarta-slide/src/webdav/client/src/org/apache/webdav/util/URIUtil.java
Index: URIUtil.java
===================================================================
RCS file:
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/URIUtil.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- URIUtil.java 2001/04/26 13:52:18 1.4
+++ URIUtil.java 2001/04/27 02:23:05 1.5
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/URIUtil.java,v
1.4 2001/04/26 13:52:18 jericho Exp $
- * $Revision: 1.4 $
- * $Date: 2001/04/26 13:52:18 $
+ * $Header:
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/URIUtil.java,v
1.5 2001/04/27 02:23:05 jericho Exp $
+ * $Revision: 1.5 $
+ * $Date: 2001/04/27 02:23:05 $
*
* ====================================================================
*
@@ -81,7 +81,7 @@
* @author Tim Tye
* @author Remy Maucherat
* @author Park, Sung-Gu
- * @version $Revision: 1.4 $ $Date: 2001/04/26 13:52:18 $
+ * @version $Revision: 1.5 $ $Date: 2001/04/27 02:23:05 $
* @see <a href=http://www.ietf.org/rfc/rfc2396.txt?number=2396>RFC 2396</a>
*/
@@ -297,21 +297,21 @@
/**
- * Unescape and return the specified URI-escaped String.
+ * Unescape the escaped URI string.
*
- * @param str The uri-escaped string
+ * @param str The URI-escaped string.
* @exception IllegalArgumentException if a '%' character is not followed
* by a valid 2-digit hexadecimal number
*/
public static String unescape(String str) {
- return unescape(str.getBytes());
+ return (str == null) ? null : unescape(str.getBytes());
}
/**
- * Unescape and return the specified URI-escaped byte array.
+ * Unescape the escaped URI string.
*
- * @param bytes The uri-escaped byte array
+ * @param bytes The URI-escaped byte array.
* @exception IllegalArgumentException if a '%' character is not followed
* by a valid 2-digit hexadecimal number
*/
@@ -326,7 +326,7 @@
while (ix < len) {
byte b = bytes[ix++]; // Get byte to test
if (b == '+') {
- b = (byte)' ';
+ b = (byte) ' ';
} else if (b == '%') {
b = (byte) ((convertHexDigit(bytes[ix++]) << 4)
+ convertHexDigit(bytes[ix++]));
@@ -335,14 +335,13 @@
}
return new String(bytes, 0, ox);
-
}
/**
- * URI rewriter.
+ * Escape the unescaped URI string.
*
- * @param str The string which has to be rewritten.
+ * @param str The unescaped URI string which has to be rewritten.
*/
public static String escape(String str) {
return escape(str, null);
@@ -350,52 +349,46 @@
/**
- * URI rewriter.
+ * Escape the unescaped URI string.
*
- * @param str The string which has to be rewritten.
- * @param allowed The additional allowed URI character not to escape.
+ * @param str The unescaped URI string which has to be rewritten.
+ * @param reserved The additional reserved URI character set.
*/
- public static synchronized String escape(String str, BitSet allowed) {
+ public static String escape(String str, BitSet reserved) {
+ return (str == null) ? null : escape(str.getBytes(), reserved);
+ }
- if (str == null)
- return (null);
- int maxBytesPerChar = 10;
- int caseDiff = ('a' - 'A');
- StringBuffer rewrittenStr = new StringBuffer(str.length());
- ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
- OutputStreamWriter writer = new OutputStreamWriter(buf);
+ /**
+ * Escape the unescaped URI byte array.
+ *
+ * @param bytes The unescaped URI byte array which has to be rewritten.
+ * @param reserved The additional reserved URI character set.
+ */
+ public static synchronized String escape(byte[] bytes, BitSet reserved) {
- for (int i = 0; i < str.length(); i++) {
- int c = (int) str.charAt(i);
+ if (bytes == null)
+ return (null);
+
+ StringBuffer rewrittenStr = new StringBuffer(bytes.length);
+
+ for (int i = 0; i < bytes.length; i++) {
+ char c = (char) bytes[i];
if (alphanum.get(c)) {
- rewrittenStr.append((char)c);
- } else if (allowed != null && allowed.get(c)) {
- rewrittenStr.append((char)c);
+ rewrittenStr.append(c);
+ } else if (reserved != null && reserved.get(c)) {
+ rewrittenStr.append(c);
} else {
- try {
- writer.write(c);
- writer.flush();
- } catch(IOException e) {
- buf.reset();
- continue;
- }
- byte[] ba = buf.toByteArray();
- for (int j = 0; j < ba.length; j++) {
- // Converting each byte in the buffer
- byte toEscape = ba[j];
- rewrittenStr.append('%');
- int low = (int) (toEscape & 0x0f);
- int high = (int) ((toEscape & 0xf0) >> 4);
- rewrittenStr.append(hexadecimal[high]);
- rewrittenStr.append(hexadecimal[low]);
- }
- buf.reset();
+ byte toEscape = bytes[i];
+ rewrittenStr.append('%');
+ int low = (int) (toEscape & 0x0f);
+ int high = (int) ((toEscape & 0xf0) >> 4);
+ rewrittenStr.append(hexadecimal[high]);
+ rewrittenStr.append(hexadecimal[low]);
}
}
-
+
return rewrittenStr.toString();
-
}
-
+
}