2013-04-20 23:55:27 +02:00
|
|
|
/**
|
2014-02-17 18:57:38 +01:00
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-04-20 23:55:27 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-09-04 11:04:51 +02:00
|
|
|
package org.jivesoftware.smack.util.stringencoder.java7;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.util.stringencoder.StringEncoder;
|
2013-04-20 23:55:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Base 64 encoding implementation that generates filename and Url safe encodings.
|
|
|
|
*
|
|
|
|
* <p>
|
|
|
|
* Note: This does NOT produce standard Base 64 encodings, but a variant as defined in
|
|
|
|
* Section 4 of RFC3548:
|
|
|
|
* <a href="http://www.faqs.org/rfcs/rfc3548.html">http://www.faqs.org/rfcs/rfc3548.html</a>.
|
2014-09-04 11:04:51 +02:00
|
|
|
* </p>
|
2013-04-20 23:55:27 +02:00
|
|
|
*
|
|
|
|
* @author Robin Collier
|
|
|
|
*/
|
2014-09-04 11:04:51 +02:00
|
|
|
public class Java7Base64UrlSafeEncoder implements StringEncoder {
|
2013-04-20 23:55:27 +02:00
|
|
|
|
2014-09-04 11:04:51 +02:00
|
|
|
private static Java7Base64UrlSafeEncoder instance = new Java7Base64UrlSafeEncoder();
|
2013-04-20 23:55:27 +02:00
|
|
|
|
2014-09-05 23:19:27 +02:00
|
|
|
private static int BASE64_ENCODER_FLAGS = Base64.URL_SAFE | Base64.DONT_BREAK_LINES;
|
|
|
|
|
2014-09-04 11:04:51 +02:00
|
|
|
private Java7Base64UrlSafeEncoder() {
|
2013-04-20 23:55:27 +02:00
|
|
|
// Use getInstance()
|
|
|
|
}
|
|
|
|
|
2014-09-04 11:04:51 +02:00
|
|
|
public static Java7Base64UrlSafeEncoder getInstance() {
|
2013-04-20 23:55:27 +02:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String encode(String s) {
|
2014-09-05 23:19:27 +02:00
|
|
|
return Base64.encodeBytes(s.getBytes(), BASE64_ENCODER_FLAGS);
|
2013-04-20 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String decode(String s) {
|
2014-09-05 23:19:27 +02:00
|
|
|
return new String(Base64.decode(s, BASE64_ENCODER_FLAGS));
|
2013-04-20 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|