Java URL Encode encodeURL(BitSet urlsafe, byte[] bytes)

Here you can find the source of encodeURL(BitSet urlsafe, byte[] bytes)

Description

encode URL

License

Apache License

Declaration

private static final byte[] encodeURL(BitSet urlsafe, byte[] bytes) 

Method Source Code


//package com.java2s;
/*//from www. j  a v a2s.c  o 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.util.BitSet;

public class Main {
    /**
     * Radix used in encoding and decoding.
     */
    static final int RADIX = 16;
    protected static final byte ESCAPE_CHAR = '%';
    /**
     * BitSet of www-form-url safe characters.
     */
    protected static final BitSet WWW_FORM_URL = new BitSet(256);

    private static final byte[] encodeURL(BitSet urlsafe, byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        if (urlsafe == null) {
            urlsafe = WWW_FORM_URL;
        }

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        for (byte c : bytes) {
            int b = c;
            if (b < 0) {
                b = 256 + b;
            }
            if (urlsafe.get(b)) {
                if (b == ' ') {
                    b = '+';
                }
                buffer.write(b);
            } else {
                buffer.write(ESCAPE_CHAR);
                char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
                char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
                buffer.write(hex1);
                buffer.write(hex2);
            }
        }
        return buffer.toByteArray();
    }
}

Related

  1. encodeForUrl(final String s)
  2. encodeSrcUrl(String fullUri)
  3. encodeStringURL(String str)
  4. encodeUri(String url)
  5. encodeURIComponent(String input)
  6. encodeUrl(final String input)
  7. encodeURL(String s)
  8. encodeURL(String s)
  9. encodeUrl(String url, String encoding)