001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.util.stringencoder.java7;
018
019import org.jivesoftware.smack.util.stringencoder.StringEncoder;
020
021
022/**
023 * A Base 64 encoding implementation that generates filename and Url safe encodings.
024 * 
025 * <p>
026 * Note: This does NOT produce standard Base 64 encodings, but a variant as defined in 
027 * Section 4 of RFC3548:
028 * <a href="http://www.faqs.org/rfcs/rfc3548.html">http://www.faqs.org/rfcs/rfc3548.html</a>.
029 * </p>
030 * 
031 * @author Robin Collier
032 */
033public class Java7Base64UrlSafeEncoder implements StringEncoder {
034
035    private static Java7Base64UrlSafeEncoder instance = new Java7Base64UrlSafeEncoder();
036
037    private static int BASE64_ENCODER_FLAGS =  Base64.URL_SAFE | Base64.DONT_BREAK_LINES;
038
039    private Java7Base64UrlSafeEncoder() {
040        // Use getInstance()
041    }
042
043    public static Java7Base64UrlSafeEncoder getInstance() {
044        return instance;
045    }
046
047    public String encode(String s) {
048        return Base64.encodeBytes(s.getBytes(), BASE64_ENCODER_FLAGS);
049    }
050
051    public String decode(String s) {
052        return new String(Base64.decode(s, BASE64_ENCODER_FLAGS));
053    }
054
055}