commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r797687 - in /commons/proper/codec/trunk/src/java/org/apache/commons/codec/net: QuotedPrintableCodec.java URLCodec.java Utils.java
Date Fri, 24 Jul 2009 23:14:05 GMT
Author: ggregory
Date: Fri Jul 24 23:14:04 2009
New Revision: 797687

URL: http://svn.apache.org/viewvc?rev=797687&view=rev
Log:
Refactor common logic between URLCodec and QuotedPrintableCodec. Code coverage 100%/100% for
both classes.

Added:
    commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/Utils.java
Modified:
    commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
    commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/URLCodec.java

Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QuotedPrintableCodec.java?rev=797687&r1=797686&r2=797687&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
(original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
Fri Jul 24 23:14:04 2009
@@ -183,14 +183,11 @@
             int b = bytes[i];
             if (b == ESCAPE_CHAR) {
                 try {
-                    int u = Character.digit((char) bytes[++i], 16);
-                    int l = Character.digit((char) bytes[++i], 16);
-                    if (u == -1 || l == -1) {
-                        throw new DecoderException("Invalid quoted-printable encoding");
-                    }
+                    int u = Utils.digit16(bytes[++i]);
+                    int l = Utils.digit16(bytes[++i]);
                     buffer.write((char) ((u << 4) + l));
                 } catch (ArrayIndexOutOfBoundsException e) {
-                    throw new DecoderException("Invalid quoted-printable encoding");
+                    throw new DecoderException("Invalid quoted-printable encoding", e);
                 }
             } else {
                 buffer.write(b);

Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/URLCodec.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/URLCodec.java?rev=797687&r1=797686&r2=797687&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/URLCodec.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/URLCodec.java Fri Jul
24 23:14:04 2009
@@ -55,7 +55,7 @@
     /**
      * Radix used in encoding and decoding.
      */
-    private static final int RADIX = 16;
+    static final int RADIX = 16;
     
     /**
      * The default charset used for string decoding and encoding. Consider this field final.
The next major release may
@@ -113,23 +113,23 @@
     }
 
     /**
-     * Encodes an array of bytes into an array of URL safe 7-bit 
-     * characters. Unsafe characters are escaped.
-     *
-     * @param urlsafe bitset of characters deemed URL safe
-     * @param bytes array of bytes to convert to URL safe characters
+     * Encodes an array of bytes into an array of URL safe 7-bit characters. Unsafe characters
are escaped.
+     * 
+     * @param urlsafe
+     *            bitset of characters deemed URL safe
+     * @param bytes
+     *            array of bytes to convert to URL safe characters
      * @return array of bytes containing URL safe characters
      */
-    public static final byte[] encodeUrl(BitSet urlsafe, byte[] bytes) 
-    {
+    public static final byte[] encodeUrl(BitSet urlsafe, byte[] bytes) {
         if (bytes == null) {
             return null;
         }
         if (urlsafe == null) {
             urlsafe = WWW_FORM_URL;
         }
-        
-        ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
+
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         for (int i = 0; i < bytes.length; i++) {
             int b = bytes[i];
             if (b < 0) {
@@ -142,18 +142,15 @@
                 buffer.write(b);
             } else {
                 buffer.write('%');
-                char hex1 = Character.toUpperCase(
-                  Character.forDigit((b >> 4) & 0xF, RADIX));
-                char hex2 = Character.toUpperCase(
-                  Character.forDigit(b & 0xF, RADIX));
+                char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) &
0xF, RADIX));
+                char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
                 buffer.write(hex1);
                 buffer.write(hex2);
             }
         }
-        return buffer.toByteArray(); 
+        return buffer.toByteArray();
     }
 
-
     /**
      * Decodes an array of URL safe 7-bit characters into an array of 
      * original bytes. Escaped characters are converted back to their 
@@ -174,8 +171,8 @@
                 buffer.write(' ');
             } else if (b == '%') {
                 try {
-                    int u = toCharacterDigit(bytes[++i]);
-                    int l = toCharacterDigit(bytes[++i]);
+                    int u = Utils.digit16(bytes[++i]);
+                    int l = Utils.digit16(bytes[++i]);
                     buffer.write((char) ((u << 4) + l));
                 } catch (ArrayIndexOutOfBoundsException e) {
                     throw new DecoderException("Invalid URL encoding: ", e);
@@ -187,13 +184,6 @@
         return buffer.toByteArray();
     }
 
-    private static int toCharacterDigit(byte b) throws DecoderException {
-        int i = Character.digit((char) b, RADIX);
-        if (i == -1) {
-            throw new DecoderException("Invalid URL encoding: not a valid digit (radix "
+ RADIX + "): " + b);
-        }
-        return i;
-    }
     /**
      * Encodes an array of bytes into an array of URL safe 7-bit 
      * characters. Unsafe characters are escaped.

Added: commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/Utils.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/Utils.java?rev=797687&view=auto
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/Utils.java (added)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/Utils.java Fri Jul 24
23:14:04 2009
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.codec.net;
+
+import org.apache.commons.codec.DecoderException;
+
+/**
+ * Utility methods for this package.
+ * 
+ * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
+ * @version $Id: $
+ */
+class Utils {
+
+    /**
+     * Returns the numeric value of the character <code>b</code> in radix 16.
+     * 
+     * @param b
+     *            the byte to be converted.
+     * @return the numeric value represented by the character in radix 16.
+     * 
+     * @throws DecoderException
+     *             Thrown when the byte is not valid per {@link Character#digit(char,int)}
+     */
+    static int digit16(byte b) throws DecoderException {
+        int i = Character.digit((char) b, 16);
+        if (i == -1) {
+            throw new DecoderException("Invalid URL encoding: not a valid digit (radix "
+ URLCodec.RADIX + "): " + b);
+        }
+        return i;
+    }
+
+}



Mime
View raw message