Java URL Encode unpaddedBase64UrlEncoded(final String unencodedString)

Here you can find the source of unpaddedBase64UrlEncoded(final String unencodedString)

Description

unpadded Base Url Encoded

License

Apache License

Declaration

public static String unpaddedBase64UrlEncoded(final String unencodedString) 

Method Source Code


//package com.java2s;
/*/*from ww w.  ja  v  a2 s.  com*/
 * Copyright 2016 the original author or authors.
 *
 * 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.util.Base64;

public class Main {
    public static String unpaddedBase64UrlEncoded(final String unencodedString) {
        return unpaddedBase64UrlEncoded(unencodedString.getBytes());
    }

    public static String unpaddedBase64UrlEncoded(final byte[] unencodedBytes) {
        return stripEndEquals(new String(Base64.getUrlEncoder().encode(unencodedBytes)));
    }

    /**
     * Strips the end equals characters from a base64 encoded string
     *
     * @param base64EncodedString The Base64 encoded string to strip the trailing "=" characters from
     * @return The provided encoded string, minus its trailing = characters
     */
    public static String stripEndEquals(final String base64EncodedString) {

        int equalsSigns = 0;
        int strLength = base64EncodedString.length();

        for (int i = strLength - 1; i >= 0; i--) {
            if (base64EncodedString.charAt(i) == '=') {
                equalsSigns++;
            } else {
                break;
            }
        }

        return base64EncodedString.substring(0, strLength - equalsSigns);

    }
}

Related

  1. encodeURL(String s)
  2. encodeURL(String s)
  3. encodeUrl(String url, String encoding)
  4. encodeURLComponent(final String s)
  5. encodeUrlPath(String pathSegment)
  6. uriDecode(String src)
  7. uriDecode(String uri)
  8. uriEncode(String uriRef)
  9. URLEncode(byte[] input)