Java URL Encode encode(String s)

Here you can find the source of encode(String s)

Description

Translates a string into x-www-form-urlencoded format.

License

Apache License

Parameter

Parameter Description
s <code>String</code> to be translated.

Return

the translated String.

Declaration

public static String encode(String s) 

Method Source Code


//package com.java2s;
/*/*from  w  w w  . ja  v a  2  s .  co  m*/
 * 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.
 */

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;

public class Main {
    /** A BitSet defining the characters which don't need encoding */
    static BitSet charactersDontNeedingEncoding;
    static final int characterCaseDiff = ('a' - 'A');

    /**
     * Translates a string into <code>x-www-form-urlencoded</code> format.
     *
     * @param   s   <code>String</code> to be translated.
     * @return  the translated <code>String</code>.
     */
    public static String encode(String s) {
        final StringBuffer out = new StringBuffer(s.length());
        final ByteArrayOutputStream buf = new ByteArrayOutputStream(32);
        final OutputStreamWriter writer = new OutputStreamWriter(buf);
        for (int i = 0; i < s.length(); i++) {
            int c = s.charAt(i);
            if (charactersDontNeedingEncoding.get(c)) {
                out.append((char) 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++) {
                    out.append('%');
                    char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
                    // converting to use uppercase letter as part of
                    // the hex value if ch is a letter.
                    if (Character.isLetter(ch)) {
                        ch -= characterCaseDiff;
                    }
                    out.append(ch);
                    ch = Character.forDigit(ba[j] & 0xF, 16);
                    if (Character.isLetter(ch)) {
                        ch -= characterCaseDiff;
                    }
                    out.append(ch);
                }
                buf.reset();
            }
        }

        return out.toString();
    }

    /**
     * Translates a string into <code>x-www-form-urlencoded</code> format
     * with specified encoding
     *
     * @param   s   <code>String</code> to be translated.
     * @param   enc The name of a supported charset
     * @return  the translated <code>String</code>.
     * @throws UnsupportedEncodingException
     */
    public static String encode(String s, String enc) throws UnsupportedEncodingException {
        // Why not use the java.net.URLEncoder for this purpose?
        final StringBuffer out = new StringBuffer(s.length());
        final ByteArrayOutputStream buf = new ByteArrayOutputStream(32);
        final OutputStreamWriter writer = new OutputStreamWriter(buf, enc);
        for (int i = 0; i < s.length(); i++) {
            int c = s.charAt(i);
            if (charactersDontNeedingEncoding.get(c)) {
                out.append((char) 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++) {
                    out.append('%');
                    char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
                    // converting to use uppercase letter as part of
                    // the hex value if ch is a letter.
                    if (Character.isLetter(ch)) {
                        ch -= characterCaseDiff;
                    }
                    out.append(ch);
                    ch = Character.forDigit(ba[j] & 0xF, 16);
                    if (Character.isLetter(ch)) {
                        ch -= characterCaseDiff;
                    }
                    out.append(ch);
                }
                buf.reset();
            }
        }

        return out.toString();
    }
}

Related

  1. encode(String input)
  2. encode(String input)
  3. encode(String raw)
  4. encode(String s)
  5. encode(String s)
  6. encode(String s)
  7. encode(String s)
  8. encode(String s)
  9. encode(String s)