package iotcloud; import java.util.Arrays; /** * IoTString is wraps the underlying byte string. We don't use the * standard String class as we have bytes and not chars. * @author Brian Demsky * @version 1.0 */ final public class IoTString { byte[] array; int hashcode; private IoTString() { } /** * Builds an IoTString object around the byte array. This * constructor makes a copy, so the caller is free to modify the byte array. */ public IoTString(byte[] _array) { array=(byte[]) _array.clone(); hashcode=Arrays.hashCode(array); } /** * Converts the String object to a byte representation and stores it * into the IoTString object. */ public IoTString(String str) { array=str.getBytes(); hashcode=Arrays.hashCode(array); } /** * Internal methods to build an IoTString using the byte[] passed * in. Caller is responsible for ensuring the byte[] is never * modified. */ static IoTString shallow(byte[] _array) { IoTString i=new IoTString(); i.array = _array; i.hashcode = Arrays.hashCode(_array); return i; } /** * Internal method to grab a reference to our byte array. Caller * must not modify it. */ byte[] internalBytes() { return array; } /** * Returns the hashCode as computed by Arrays.hashcode(byte[]). */ public int hashCode() { return hashcode; } /** * Returns a String representation of the IoTString. */ public String toString() { return new String(array); } /** * Returns a copy of the underlying byte string. */ public byte[] getBytes() { return (byte[]) array.clone(); } /** * Returns true if two byte strings have the same content. */ public boolean equals(Object o) { if (o instanceof IoTString) { IoTString i=(IoTString)o; return Arrays.equals(array, i.array); } return false; } /** * Returns the length in bytes of the IoTString. */ public int length() { return array.length; } }