001/** 002 * 003 * Copyright © 2014-2019 Florian Schmaus 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; 018 019import java.nio.charset.StandardCharsets; 020 021import org.jivesoftware.smack.util.Objects; 022import org.jivesoftware.smack.util.StringUtils; 023 024public class Base64 { 025 026 private static Base64.Encoder base64encoder; 027 028 public static void setEncoder(Base64.Encoder encoder) { 029 Objects.requireNonNull(encoder, "encoder must no be null"); 030 base64encoder = encoder; 031 } 032 033 public static final String encode(String string) { 034 return encodeToString(string.getBytes(StandardCharsets.UTF_8)); 035 } 036 037 public static final String encodeToString(byte[] input) { 038 return base64encoder.encodeToString(input); 039 } 040 041 public static final String encodeToString(byte[] input, int offset, int len) { 042 return encodeToString(slice(input, offset, len)); 043 } 044 045 public static final String encodeToStringWithoutPadding(byte[] input) { 046 return base64encoder.encodeToStringWithoutPadding(input); 047 } 048 049 public static final byte[] encode(byte[] input) { 050 return base64encoder.encode(input); 051 } 052 053 public static final String decodeToString(String string) { 054 byte[] bytes = decode(string); 055 return new String(bytes, StandardCharsets.UTF_8); 056 } 057 058 // TODO: We really should not mask the IllegalArgumentException. But some unit test depend on this behavior, like 059 // ibb.packet.DataPacketExtension.shouldReturnNullIfDataIsInvalid(). 060 public static final byte[] decode(String string) { 061 // xs:base64Binary may include XML whitespace which we need to delete before feeding the string into the Base64 062 // decoder. See also XML Schema Part 2: Datatypes Second Edition § 3.2.16. 063 string = StringUtils.deleteXmlWhitespace(string); 064 try { 065 return base64encoder.decode(string); 066 } catch (IllegalArgumentException e) { 067 return null; 068 } 069 } 070 071 public static final byte[] decode(byte[] input) { 072 String string = new String(input, StandardCharsets.US_ASCII); 073 return decode(string); 074 } 075 076 private static byte[] slice(byte[] input, int offset, int len) { 077 if (offset == 0 && len == input.length) { 078 return input; 079 } 080 081 byte[] res = new byte[len]; 082 System.arraycopy(input, offset, res, 0, len); 083 return res; 084 } 085 086 public interface Encoder { 087 byte[] decode(String string); 088 089 String encodeToString(byte[] input); 090 091 String encodeToStringWithoutPadding(byte[] input); 092 093 byte[] encode(byte[] input); 094 } 095}