ingo@79: package de.intevation.artifactdatabase; ingo@79: ingo@79: import java.io.UnsupportedEncodingException; ingo@79: ingo@79: import java.util.UUID; ingo@79: ingo@80: import org.apache.commons.codec.DecoderException; ingo@80: ingo@80: import org.apache.commons.codec.binary.Hex; ingo@80: ingo@79: import org.apache.log4j.Logger; ingo@79: ingo@79: /** sascha@87: * Commonly used string functions. sascha@87: * ingo@79: * @author Sascha L. Teichmann ingo@79: */ ingo@79: public final class StringUtils ingo@79: { ingo@79: private static Logger logger = Logger.getLogger(StringUtils.class); ingo@79: sascha@87: /** sascha@87: * Generated a random UUIDv4 in form of a string. sascha@87: * @return the UUID sascha@87: */ ingo@79: public static final String newUUID() { ingo@79: return UUID.randomUUID().toString(); ingo@79: } ingo@79: sascha@87: /** sascha@87: * Checks if a given string is a valid UUID. sascha@87: * @param uuid The string to test. sascha@87: * @return true if the string is a valid UUID else false. sascha@87: */ ingo@79: public static final boolean checkUUID(String uuid) { ingo@79: try { ingo@79: UUID.fromString(uuid); ingo@79: } ingo@79: catch (IllegalArgumentException iae) { ingo@79: logger.warn(iae.getLocalizedMessage()); ingo@79: return false; ingo@79: } ingo@79: return true; ingo@79: } ingo@79: sascha@87: /** sascha@87: * Returns the UTF-8 byte array representation of a given string. sascha@87: * @param s The string to be transformed. sascha@87: * @return The byte array representation. sascha@87: */ ingo@79: public static final byte [] getUTF8Bytes(String s) { ingo@79: try { ingo@79: return s.getBytes("UTF-8"); ingo@79: } ingo@79: catch (UnsupportedEncodingException usee) { ingo@79: logger.error(usee.getLocalizedMessage(), usee); ingo@79: return s.getBytes(); ingo@79: } ingo@79: } ingo@80: sascha@87: /** sascha@87: * Tries to convert a Base64 encoded string into the sascha@87: * corresponing byte array. sascha@87: * @param s The Base64 encoded string sascha@87: * @return The byte array representation or null if sascha@87: * an decoding error occurs. sascha@87: */ ingo@80: public static final byte [] decodeHex(String s) { ingo@80: try { ingo@80: return Hex.decodeHex(s.toCharArray()); ingo@80: } ingo@80: catch (DecoderException de) { ingo@80: return null; ingo@80: } ingo@80: } ingo@79: } ingo@79: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :