commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject cvs commit: jakarta-commons/codec/src/java/org/apache/commons/codec/net URLCodec.java
Date Sun, 21 Mar 2004 01:58:40 GMT
ggregory    2004/03/20 17:58:40

  Modified:    codec/src/java/org/apache/commons/codec/net URLCodec.java
  Log:
  
  PR: Bugzilla Bug 27788
  	  	[Codec][PATCH] Inconsistent use of the word 'encodning' in the URLCodec API
  Submitted by:	Oleg Kalnichevski
  Reviewed by:	Gary Gregory
  
  Revision  Changes    Path
  1.18      +42 -22    jakarta-commons/codec/src/java/org/apache/commons/codec/net/URLCodec.java
  
  Index: URLCodec.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/codec/src/java/org/apache/commons/codec/net/URLCodec.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- URLCodec.java	29 Feb 2004 04:08:31 -0000	1.17
  +++ URLCodec.java	21 Mar 2004 01:58:40 -0000	1.18
  @@ -55,10 +55,11 @@
       private final static String US_ASCII = "US-ASCII";
       
       /**
  -     * The <code>String</code> encoding used for decoding and encoding.
  +     * The default charset used for string decoding and encoding.
        */
  -    protected String encoding = "UTF-8";
  +    protected String charset = "UTF-8";
       
  +    protected static byte ESCAPE_CHAR = '%';
       /**
        * BitSet of www-form-url safe characters.
        */
  @@ -95,13 +96,13 @@
       }
   
       /**
  -     * Constructor which allows for the selection of an Encoding
  +     * Constructor which allows for the selection of a default charset
        * 
  -     * @param encoding the String encoding to use.
  +     * @param charset the default string charset to use.
        */
  -    public URLCodec(String encoding) {
  +    public URLCodec(String charset) {
           super();
  -        this.encoding = encoding;
  +        this.charset = charset;
       }
   
       /**
  @@ -124,7 +125,10 @@
           ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
           for (int i = 0; i < pArray.length; i++) {
               int b = pArray[i];
  -            if (b >= 0 && urlsafe.get(b)) {
  +            if (b < 0) {
  +                b = 256 + b;
  +            }
  +            if (urlsafe.get(b)) {
                   if (b == ' ') {
                       b = '+';
                   }
  @@ -210,38 +214,40 @@
   
       /**
        * Encodes a string into its URL safe form using the specified
  -     * encoding. Unsafe characters are escaped.
  +     * string charset. Unsafe characters are escaped.
        *
        * @param pString string to convert to a URL safe form
  -     * @param encoding the encoding for pString
  +     * @param charset the charset for pString
        * @return URL safe string
        * @throws UnsupportedEncodingException Thrown if charset is not
        *                                      supported 
        */
  -    public String encode(String pString, String encoding) 
  +    public String encode(String pString, String charset) 
           throws UnsupportedEncodingException  
       {
           if (pString == null) {
               return null;
           }
  -        return new String(encode(pString.getBytes(encoding)), US_ASCII);
  +        return new String(encode(pString.getBytes(charset)), US_ASCII);
       }
   
   
       /**
  -     * Encodes a string into its URL safe form. Unsafe characters are 
  -     * escaped.
  +     * Encodes a string into its URL safe form using the default string 
  +     * charset. Unsafe characters are escaped.
        *
        * @param pString string to convert to a URL safe form
        * @return URL safe string
        * @throws EncoderException Thrown if URL encoding is unsuccessful
  +     * 
  +     * @see #getDefaultCharset()
        */
       public String encode(String pString) throws EncoderException {
           if (pString == null) {
               return null;
           }
           try {
  -            return encode(pString, this.getEncoding());
  +            return encode(pString, getDefaultCharset());
           } catch(UnsupportedEncodingException e) {
               throw new EncoderException(e.getMessage());
           }
  @@ -254,36 +260,39 @@
        * to their original representation.
        *
        * @param pString URL safe string to convert into its original form
  -     * @param encoding the original string encoding
  +     * @param charset the original string charset
        * @return original string 
        * @throws DecoderException Thrown if URL decoding is unsuccessful
        * @throws UnsupportedEncodingException Thrown if charset is not
        *                                      supported 
        */
  -    public String decode(String pString, String encoding) 
  +    public String decode(String pString, String charset) 
           throws DecoderException, UnsupportedEncodingException 
       {
           if (pString == null) {
               return null;
           }
  -        return new String(decode(pString.getBytes(US_ASCII)), encoding);
  +        return new String(decode(pString.getBytes(US_ASCII)), charset);
       }
   
   
       /**
  -     * Decodes a URL safe string into its original form. Escaped 
  -     * characters are converted back to their original representation.
  +     * Decodes a URL safe string into its original form using the default
  +     * string charset. Escaped characters are converted back to their 
  +     * original representation.
        *
        * @param pString URL safe string to convert into its original form
        * @return original string 
        * @throws DecoderException Thrown if URL decoding is unsuccessful
  +     * 
  +     * @see #getDefaultCharset()
        */
       public String decode(String pString) throws DecoderException {
           if (pString == null) {
               return null;
           }
           try {
  -            return decode(pString, this.getEncoding());
  +            return decode(pString, getDefaultCharset());
           } catch(UnsupportedEncodingException e) {
               throw new DecoderException(e.getMessage());
           }
  @@ -341,9 +350,20 @@
        * The <code>String</code> encoding used for decoding and encoding.
        *
        * @return Returns the encoding.
  +     * 
  +     * @deprecated use #getDefaultCharset()
        */
       public String getEncoding() {
  -        return this.encoding;
  +        return this.charset;
  +    }
  +
  +    /**
  +     * The default charset used for string decoding and encoding.
  +     *
  +     * @return the default string charset.
  +     */
  +    public String getDefaultCharset() {
  +        return this.charset;
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message