Java URL Encode URLEncode(byte[] input)

Here you can find the source of URLEncode(byte[] input)

Description

URL Encode

License

Open Source License

Declaration

public static String URLEncode(byte[] input) 

Method Source Code

//package com.java2s;
/*/*w  ww.j  av  a2  s  . co  m*/
Copyright (C) 2004-2009  Juho V?h?-Herttua
    
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

public class Main {
    public static String URLEncode(byte[] input) {
        StringBuffer ret;
        int i, temp;

        if (input == null)
            return null;

        ret = new StringBuffer();
        for (i = 0; i < input.length; i++) {
            temp = input[i] & 0xff; // [-128,127] to [0,255]
            if ((temp >= 0x30 && temp <= 0x39) || // 0-9
                    (temp >= 0x41 && temp <= 0x5a) || // A-Z
                    (temp >= 0x61 && temp <= 0x7a) || // a-z
                    temp == 0x2e || temp == 0x2d || // . and -
                    temp == 0x2a || temp == 0x5f) { // * and _
                ret.append((char) temp);
            } else if (temp == 0x20) {
                ret.append('+');
            } else {
                ret.append('%');
                if (temp < 16)
                    ret.append('0');
                ret.append(Integer.toHexString(temp));
            }
        }

        return ret.toString();
    }
}

Related

  1. encodeUrlPath(String pathSegment)
  2. unpaddedBase64UrlEncoded(final String unencodedString)
  3. uriDecode(String src)
  4. uriDecode(String uri)
  5. uriEncode(String uriRef)
  6. urlEncode(byte[] rs)
  7. urlencode(byte[] unencodedBytes)
  8. URLEncode(String in)
  9. urlEncode(String origString)