/** * Generate a {@link Guid} for an array of Strings. * @param strings array of Strings. * @return a single {@link Guid} for the array. * @throws IOException */ public static Guid fromStrings(String... strings) throws IOException { if (strings == null || strings.length == 0) { throw new IOException("Attempting to compute guid for an empty array."); } return new Guid(StringUtils.join(strings).getBytes(Charsets.UTF_8)); }
/** * Generate a {@link Guid} for an array of byte arrays. * @param byteArrays array of byte arrays. * @return a single {@link Guid} for the array. * @throws IOException */ public static Guid fromByteArrays(byte[]... byteArrays) throws IOException { if (byteArrays == null || byteArrays.length == 0) { throw new IOException("Attempting to compute guid for an empty array."); } if (byteArrays.length == 1) { return new Guid(byteArrays[0]); } byte[] tmp = new byte[0]; for (byte[] arr : byteArrays) { tmp = ArrayUtils.addAll(tmp, arr); } return new Guid(tmp); }
/** * Reverse of {@link #toString}. Deserializes a {@link Guid} from a previously serialized one. * @param str Serialized {@link Guid}. * @return deserialized {@link Guid}. * @throws IOException */ public static Guid deserialize(String str) throws IOException { if (str.length() != 2 * GUID_LENGTH) { throw new IOException("String is not an encoded guid."); } try { return new Guid(Hex.decodeHex(str.toCharArray()), true); } catch (DecoderException de) { throw new IOException(de); } }