Package net.i2p.data

Class SimpleDataStructure

  • All Implemented Interfaces:
    DataStructure
    Direct Known Subclasses:
    EncryptedBuildRecord, Hash, Hash384, Hash512, NodeInfo, PrivateKey, PublicKey, SessionKey, SHA1Hash, Signature, SigningPrivateKey, SigningPublicKey

    public abstract class SimpleDataStructure
    extends Object
    implements DataStructure
    A SimpleDataStructure contains only a single fixed-length byte array. Implemented in 0.8.2 and retrofitted over several of the classes in this package. As of 0.8.3, SDS objects may be cached. An SDS may be instantiated with null data, and setData(null) is also OK. However, once non-null data is set, the data reference is immutable; subsequent attempts to set the data via setData(), readBytes(), fromByteArray(), or fromBase64() will throw a RuntimeException. As of 0.9.48, no longer extends DataStrucureImpl to save space
    Since:
    0.8.2
    Author:
    zzz
    • Field Detail

      • _data

        protected byte[] _data
    • Constructor Detail

      • SimpleDataStructure

        public SimpleDataStructure()
        A new instance with the data set to null. Call readBytes(), setData(), or fromByteArray() after this to set the data
      • SimpleDataStructure

        public SimpleDataStructure​(byte[] data)
        Throws:
        IllegalArgumentException - if data is not the legal number of bytes (but null is ok)
    • Method Detail

      • length

        public abstract int length()
        The legal length of the byte array in this data structure
        Since:
        0.8.2
      • getData

        public byte[] getData()
        Get the data reference (not a copy)
        Returns:
        the byte array, or null if unset
      • setData

        public void setData​(byte[] data)
        Sets the data.
        Parameters:
        data - of correct length, or null
        Throws:
        IllegalArgumentException - if data is not the legal number of bytes (but null is ok)
        RuntimeException - if data already set.
      • read

        protected int read​(InputStream in,
                           byte[] target)
                    throws IOException
        Repeated reads until the buffer is full or IOException is thrown
        Returns:
        number of bytes read (should always equal target.length)
        Throws:
        IOException
        Since:
        0.9.48, copied from former superclass DataStructureImpl
      • toBase64

        public String toBase64()
        Description copied from interface: DataStructure
        render the structure into modified base 64 notation
        Specified by:
        toBase64 in interface DataStructure
        Returns:
        null on error
      • calculateHash

        public Hash calculateHash()
        Description copied from interface: DataStructure
        Calculate the SHA256 value of this object (useful for a few scenarios)
        Specified by:
        calculateHash in interface DataStructure
        Returns:
        the SHA256 hash of the byte array, or null if the data is null
      • toByteArray

        public byte[] toByteArray()
        Specified by:
        toByteArray in interface DataStructure
        Returns:
        same thing as getData()
      • hashCode

        public int hashCode()
        We assume the data has enough randomness in it, so use the first 4 bytes for speed. If this is not the case, override in the extending class.
        Overrides:
        hashCode in class Object
      • equals

        public boolean equals​(Object obj)
        Warning - this returns true for two different classes with the same size and same data, e.g. SessionKey and SessionTag, but you wouldn't put them in the same Set, would you?
        Overrides:
        equals in class Object