Java URL Encode urlencode(byte[] unencodedBytes)

Here you can find the source of urlencode(byte[] unencodedBytes)

Description

Encodes an array of plain bytes into a urlencoded string

License

MIT License

Parameter

Parameter Description
unencodedBytes The bytes to encode

Return

A urlencoded string

Declaration

public static String urlencode(byte[] unencodedBytes) 

Method Source Code

//package com.java2s;
/*//from w  ww  .  j  a  va  2  s . com
 * Copyright (c) 2010 Matthew J. Francis and Contributors of the Bobbin Project
 * This file is distributed under the MIT licence. See the LICENCE file for further information.
 */

public class Main {
    /**
     * Encodes an array of plain bytes into a urlencoded string
     *
     * @param unencodedBytes The bytes to encode
     * @return A urlencoded string
     */
    public static String urlencode(byte[] unencodedBytes) {

        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < unencodedBytes.length; i++) {

            if (((unencodedBytes[i] >= 'a') && (unencodedBytes[i] <= 'z'))
                    || ((unencodedBytes[i] >= 'A') && (unencodedBytes[i] <= 'Z'))
                    || ((unencodedBytes[i] >= '0') && (unencodedBytes[i] <= '9')) || (unencodedBytes[i] == '.')
                    || (unencodedBytes[i] == '-') || (unencodedBytes[i] == '*') || (unencodedBytes[i] == '_')) {
                buffer.append((char) unencodedBytes[i]);
            } else if (unencodedBytes[i] == ' ') {
                buffer.append('+');
            } else {
                buffer.append(String.format("%%%02x", unencodedBytes[i]));
            }

        }

        return buffer.toString();

    }
}

Related

  1. uriDecode(String src)
  2. uriDecode(String uri)
  3. uriEncode(String uriRef)
  4. URLEncode(byte[] input)
  5. urlEncode(byte[] rs)
  6. URLEncode(String in)
  7. urlEncode(String origString)
  8. URLencode(String s)
  9. urlEncode(String str)