Java URL Encode encode(byte[] bytes, String encoding)

Here you can find the source of encode(byte[] bytes, String encoding)

Description

Encode a byte string as a String.

License

Apache License

Parameter

Parameter Description
bytes the byte string to encode
encoding the encoding to use - "url" for URL-encoded, "hex" for hexadecimal, or any other valid encoding name

Return

the encoded String

Declaration

public static String encode(byte[] bytes, String encoding) 

Method Source Code

//package com.java2s;
/*// w w  w.j  av a2s.com
 * Copyright ? 2014 Cask Data, Inc.
 *
 * Licensed 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.UnsupportedEncodingException;

import java.net.URLEncoder;

public class Main {
    /**
     * Encode a byte string as a String.
     *
     * @param bytes    the byte string to encode
     * @param encoding the encoding to use - "url" for URL-encoded, "hex" for
     *                 hexadecimal, or any other valid encoding name
     * @return the encoded String
     */
    public static String encode(byte[] bytes, String encoding) {
        if (bytes == null) {
            return null;
        } else if (encoding == null) {
            return urlEncode(bytes);
        } else if ("url".equals(encoding)) {
            return urlEncode(bytes);
        } else if ("hex".equals(encoding)) {
            return toHex(bytes);
        } else {
            try {
                return new String(bytes, encoding);
            } catch (UnsupportedEncodingException e) {
                return urlEncode(bytes);
            }
        }
    }

    /**
     * URL-encode a binary string.
     */
    public static String urlEncode(byte[] binary) {
        if (binary == null) {
            return null;
        }
        try { // we use a base encoding that accepts all byte values
            return URLEncoder.encode(new String(binary, "ISO8859_1"), "ISO8859_1");
        } catch (UnsupportedEncodingException e) {
            // this cannot happen with ISO8859_1 = Latin1
            e.printStackTrace();
            return null;

        }
    }

    /**
     * Convert a byte array into its hex string representation.
     *
     * @param bytes the byte array to convert
     * @return A hex string representing the bytes
     */
    public static String toHex(byte[] bytes) {
        StringBuilder builder = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            try {
                builder.append(Character.forDigit(b >> 4 & 0x0F, 16));
                builder.append(Character.forDigit(b & 0x0F, 16));
            } catch (IllegalArgumentException e) {
                // odd, that should never happen
                e.printStackTrace();
            }
        }
        return builder.toString();
    }
}

Related

  1. encode(final Object id)
  2. encode(final String content, final String encoding)
  3. encode(final String raw)
  4. encode(final String s, final String enc)