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;
|
|
|
|
|
2019-05-08 11:34:40 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2019-04-08 23:03:12 +02:00
|
|
|
import java.util.Base64;
|
2017-02-11 16:16:41 +01:00
|
|
|
|
2014-09-04 11:04:51 +02:00
|
|
|
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.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2013-04-20 23:55:27 +02:00
|
|
|
* <p>
|
2018-05-09 23:06:12 +02:00
|
|
|
* Note: This does NOT produce standard Base 64 encodings, but a variant as defined in
|
2013-04-20 23:55:27 +02:00
|
|
|
* 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>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2013-04-20 23:55:27 +02:00
|
|
|
* @author Robin Collier
|
|
|
|
*/
|
2018-08-21 09:16:11 +02:00
|
|
|
public final class Java7Base64UrlSafeEncoder implements StringEncoder<String> {
|
2013-04-20 23:55:27 +02:00
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private static final Java7Base64UrlSafeEncoder instance = new Java7Base64UrlSafeEncoder();
|
2013-04-20 23:55:27 +02:00
|
|
|
|
2019-04-08 23:03:12 +02:00
|
|
|
private final Base64.Encoder encoder;
|
|
|
|
private final Base64.Decoder decoder;
|
2014-09-05 23:19:27 +02:00
|
|
|
|
2014-09-04 11:04:51 +02:00
|
|
|
private Java7Base64UrlSafeEncoder() {
|
2019-04-08 23:03:12 +02:00
|
|
|
encoder = Base64.getUrlEncoder();
|
|
|
|
decoder = Base64.getUrlDecoder();
|
2013-04-20 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
2014-09-04 11:04:51 +02:00
|
|
|
public static Java7Base64UrlSafeEncoder getInstance() {
|
2013-04-20 23:55:27 +02:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2013-04-20 23:55:27 +02:00
|
|
|
public String encode(String s) {
|
2019-05-08 11:34:40 +02:00
|
|
|
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
|
2019-04-08 23:03:12 +02:00
|
|
|
return encoder.encodeToString(bytes);
|
2013-04-20 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2013-04-20 23:55:27 +02:00
|
|
|
public String decode(String s) {
|
2019-04-08 23:03:12 +02:00
|
|
|
byte[] bytes = decoder.decode(s);
|
2019-05-08 11:34:40 +02:00
|
|
|
return new String(bytes, StandardCharsets.UTF_8);
|
2013-04-20 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|