/** * Will encode byte array using Base64 encoding. * * @param input bytes to encode * @return encoded string */ public static String toBase64(byte[] input) { return Base64.getEncoder().encodeToString(input); }
/** * Will decode Base64-encoded string back into byte array. * * @param input Base64-encoded string. * @return byte array decoded from string. */ public static byte[] fromBase64(String input) { return Base64.getDecoder().decode(input); }
/** * {@inheritDoc} */ public String name(int value) { if (value < LOOKUP_TABLE_SIZE) { return lookupTable()[value]; } return super.name(value); }
/** * Converts a camel case to underscore and then pluralizes. * * Example: "GrayDuck" is converted to "gray_ducks". * * @param camelCase any CamelCase phrase. * @return pluralized version of underscored CamelCase. */ public static String tableize(String camelCase) { return pluralize(underscore(camelCase)); }
/** * Reads contents of the input stream fully and returns it as String. Sets UTF-8 encoding internally. * * @param in InputStream to read from. * @return contents of the input stream fully as String. * @throws IOException in case of IO error */ public static String read(InputStream in) throws IOException { return read(in, "UTF-8"); }
/** * @param source instance of String * @return null if source is empty or contains only whitespaces, source otherwise */ @Override public Object convert(String source) { return blank(source) ? null : source; } }
/** * Generates a camel case version of a phrase from underscore. * * @param underscore underscore version of a word to converted to camel case. * @return camel case version of underscore. */ public static String camelize(String underscore){ return camelize(underscore, true); }
/** * Create a list from values. * * @param values values to create a list from. * @return list with values. */ public static <T> List<T> li(T... values) { return list(values); }
/** * Convert Java object to a JSON string. * * @param val Java object * @return JSON string. */ public static String toJsonString(Object val) { return toJsonString(val, false); }
/** * This method is used in one-off operations, where it is OK to load a template every time. * * Example: * <code> * String result = Templator.mergeFromPath(readResource("/message_template.txt", valuesMap)); * </code> * * @param templatePath template to merge * @param values values to merge into a template * @return result of merging */ public static String mergeFromPath(String templatePath, Map<String, ?> values) { return mergeFromTemplate(readResource(templatePath), values); }
/** * Escapes control characters in a string. * * @param value string to escape * @return escaped version * @see #sanitize(String) */ public static String escapeControlChars(String value) { return sanitize(value, false); }
/** * Escapes XML appending to StringBuilder. * * @param sb StringBuilder to append to * @param xml input */ public static void xml(StringBuilder sb, String xml) { html(sb, xml); }
/** * Reads contents of resource fully into a string. Sets UTF-8 encoding internally. * * @param resourceName resource name. * @return entire contents of resource as string. */ public static String readResource(String resourceName) { return readResource(resourceName, "UTF-8"); }
/** * Default constructor. */ public XmlEntities(){ map = new XmlEntities.LookupEntityMap(); }
@Override public void write(int b) throws IOException { byte[] buf = new byte[1]; buf[0] = (byte)(b & 0xff); write(buf, 0, 1); }
/** * <p> * Creates an entity lookup table of LOOKUP_TABLE_SIZE elements, initialized with entity names. * </p> */ private void createLookupTable() { lookupTable = new String[LOOKUP_TABLE_SIZE]; for (int i = 0; i < LOOKUP_TABLE_SIZE; ++i) { lookupTable[i] = super.name(i); } } }
/** * {@inheritDoc} */ public String name(int value) { int index = binarySearch(value); if (index < 0) { return null; } return names[index]; } }
/** * This method is used in repeated operations, since it will load a resource once. * * @param values values to merge into a template * @return result of merging */ public String merge(Map<String, ?> values){ return mergeFromTemplate(template, values); }
/** * Clean control characters in a string. * * @param value string to escape * @return escaped version */ public static String cleanControlChars(String value) { return sanitize(value, true); }
/** * Escapes control characters in a string. * * @param value string to escape * @return escaped version * @see #escapeControlChars(String) */ public static String sanitize(String value) { return sanitize(value, false); }