1 package software.amazon.awssdk.crt.utils; 2 3 public class StringUtils { 4 /** 5 * Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter. 6 * Like `Strings.join()` but works on Android before API 26. 7 * 8 * @param delimiter a sequence of characters that is used to separate each of the elements in the resulting String 9 * @param elements an Iterable that will have its elements joined together 10 * @return a new String that is composed from the elements argument 11 */ join(CharSequence delimiter, Iterable<? extends CharSequence> elements)12 public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) { 13 if (delimiter == null || elements == null) throw new NullPointerException("delimiter and elements must not be null"); 14 StringBuilder sb = new StringBuilder(); 15 16 boolean first = true; 17 for(CharSequence cs : elements) { 18 if (!first) { 19 sb.append(delimiter); 20 } 21 sb.append(cs); 22 first = false; 23 } 24 return sb.toString(); 25 } 26 27 /** 28 * Encode a byte array into a Base64 byte array. 29 * @param data The byte array to encode 30 * @return The byte array encoded as Byte64 31 */ base64Encode(byte[] data)32 public static byte[] base64Encode(byte[] data) { 33 return stringUtilsBase64Encode(data); 34 } 35 36 /** 37 * Decode a Base64 byte array into a non-Base64 byte array. 38 * @param data The byte array to decode. 39 * @return Byte array decoded from Base64. 40 */ base64Decode(byte[] data)41 public static byte[] base64Decode(byte[] data) { 42 return stringUtilsBase64Decode(data); 43 } 44 stringUtilsBase64Encode(byte[] data_to_encode)45 private static native byte[] stringUtilsBase64Encode(byte[] data_to_encode); stringUtilsBase64Decode(byte[] data_to_decode)46 private static native byte[] stringUtilsBase64Decode(byte[] data_to_decode); 47 } 48