Class TStream<T>

java.lang.Object
com.saicone.rtag.stream.TStream<T>
Type Parameters:
T - Object type to write and read.

public class TStream<T> extends Object
Tag stream class to handle CompoundTag with writeable and readable objects.
The TStream instance provide easy methods handle objects has bytes.

Write

Object -> CompoundTag -> Bytes
With bytes you can write to file or convert into Base64 String.

Read

Bytes -> CompoundTag -> Object
You can read bytes from file or Base64.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final TStream<Object>
    Tag stream instance to save and get compounds.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    build(Object compound)
    Build object type using an CompoundTag.
    clone(T object)
    Clone provided object by extract CompoundTag and use it to build new object.
    extract(T object)
    Extract object information into CompoundTag.
    void
    fromBase(Object nbt, Consumer<T> consumer)
    Consume new objects if it can be created from Tag object.
    Only compatible with ByteArrayTag, ListTag and CompoundTag.
    T[]
    Get array of objects by read Base64 string.
    void
    fromBase64(String base64, Consumer<T> consumer)
    Consume new objects if it can be created from Base64 string.
    void
    fromBukkitObject(byte[] bytes, Consumer<T> consumer)
    Consume new objects if it can be created from byte array read by BukkitObjectInputStream.
    fromBytes(byte[] bytes)
    Get object from bytes.
    This method first read the bytes with ByteArrayInputStream to get an CompoundTag and convert it into object.
    Bytes -> CompoundTag -> Object
    void
    fromBytes(byte[] bytes, Consumer<T> consumer)
    Consume new objects if it can be created from byte array.
    This method detect any CompoundTag inside provided bytes and convert into current instance object using the detected compression format, it is also compatible with any saved object inside ByteArrayTag, ListTag and BukkitObjectInputStream.
    fromCompound(Object compound)
    Create new object from CompoundTag.
    void
    fromCompound(Object compound, Consumer<T> consumer)
    Consume new object if it can be created from CompoundTag.
    fromFile(File file)
    Get object by read provided file.
    void
    fromFile(File file, Consumer<T> consumer)
    Consume new objects if it can be created from file.
    void
    fromList(Object list, Consumer<T> consumer)
    Consume new objects if it can be created from ListTag object.
    Only compatible with list types of ByteArrayTag, ListTag and CompoundTag.
    Get object by read provided Map of objects.
    Get object by read provided NBT String.
    Get list of objects by read Base64 string.
    listFromBytes(byte[] bytes)
    Get list of objects by read byte array.
    listToBase64(List<T> objects)
    Convert list of objects into Base64.
    final String
    toBase64(T... object)
    Convert objects into Base64.
    byte[]
    toBytes(T object)
    Convert object into bytes.
    This method first convert provided object into CompoundTag, then write compound into ByteArrayOutputStream.
    Object -> CompoundTag -> Bytes
    toCompound(T object)
    Convert object into CompoundTag.
    toFile(T object, File file)
    Write provided object into file.
    Provided file must be exist.
    This method first convert provided object into CompoundTag, then write compound into FileOutputStream.
    toMap(T object)
    Convert object into Map of objects.
    toString(T object)
    Convert object into NBT String.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • COMPOUND

      public static final TStream<Object> COMPOUND
      Tag stream instance to save and get compounds.
  • Constructor Details

    • TStream

      public TStream()
  • Method Details

    • clone

      public T clone(T object)
      Clone provided object by extract CompoundTag and use it to build new object.
      Parameters:
      object - Object to copy.
      Returns:
      A copy of provided object.
    • extract

      public Object extract(T object)
      Extract object information into CompoundTag.
      Parameters:
      object - Object to extract.
      Returns:
      a compound tag with object information.
    • build

      public T build(Object compound)
      Build object type using an CompoundTag.
      Parameters:
      compound - CompoundTag with object information.
      Returns:
      A new object with CompoundTag parameters.
    • toCompound

      public Object toCompound(T object)
      Convert object into CompoundTag.
      Parameters:
      object - Object to convert.
      Returns:
      a compound tag representing the object.
    • fromBase

      public void fromBase(Object nbt, Consumer<T> consumer)
      Consume new objects if it can be created from Tag object.
      Only compatible with ByteArrayTag, ListTag and CompoundTag.
      Parameters:
      nbt - Tag instance.
      consumer - The consumer that accept non-null objects.
    • fromCompound

      public T fromCompound(Object compound)
      Create new object from CompoundTag.
      Parameters:
      compound - CompoundTag instance.
      Returns:
      A new object with CompoundTag parameters, null otherwise.
    • fromCompound

      public void fromCompound(Object compound, Consumer<T> consumer)
      Consume new object if it can be created from CompoundTag.
      Parameters:
      compound - CompoundTag instance.
      consumer - The consumer that accept non-null objects.
    • fromList

      public void fromList(Object list, Consumer<T> consumer)
      Consume new objects if it can be created from ListTag object.
      Only compatible with list types of ByteArrayTag, ListTag and CompoundTag.
      Parameters:
      list - ListTag instance.
      consumer - The consumer that accept non-null objects.
    • toMap

      public Map<String,Object> toMap(T object)
      Convert object into Map of objects.
      Parameters:
      object - Object to convert.
      Returns:
      A map that represent the provided object compound.
    • toString

      public String toString(T object)
      Convert object into NBT String.
      Parameters:
      object - Object to convert.
      Returns:
      The object compound as String.
    • toFile

      public File toFile(T object, File file)
      Write provided object into file.
      Provided file must be exist.
      This method first convert provided object into CompoundTag, then write compound into FileOutputStream.
      Parameters:
      object - Object to write.
      file - File to write inside.
      Returns:
      Provided file.
    • toBase64

      @SafeVarargs public final String toBase64(T... object)
      Convert objects into Base64.
      Parameters:
      object - Objects to convert.
      Returns:
      A Base64 String that represent provided objects.
    • listToBase64

      public String listToBase64(List<T> objects)
      Convert list of objects into Base64.
      Parameters:
      objects - Objects to convert.
      Returns:
      A Base64 String that represent provided list.
    • toBytes

      public byte[] toBytes(T object)
      Convert object into bytes.
      This method first convert provided object into CompoundTag, then write compound into ByteArrayOutputStream.
      Object -> CompoundTag -> Bytes
      Parameters:
      object - Object to convert.
      Returns:
      A byte array that represent the object.
    • fromMap

      public T fromMap(Map<String,Object> map)
      Get object by read provided Map of objects.
      Parameters:
      map - Map that represent the object.
      Returns:
      An object representation using Map as compound.
    • fromString

      public T fromString(String snbt)
      Get object by read provided NBT String.
      Parameters:
      snbt - a compound tag string.
      Returns:
      An object representation using NBT String as compound.
    • fromFile

      public T fromFile(File file)
      Get object by read provided file.
      Parameters:
      file - File to read.
      Returns:
      A object if provided file contains it, null if not.
    • fromFile

      public void fromFile(File file, Consumer<T> consumer)
      Consume new objects if it can be created from file.
      Parameters:
      file - File to read.
      consumer - The consumer that accept non-null objects.
    • fromBase64

      public T[] fromBase64(String base64)
      Get array of objects by read Base64 string.
      Parameters:
      base64 - Base64 that represent the objects.
      Returns:
      Array of objects.
    • fromBase64

      public void fromBase64(String base64, Consumer<T> consumer)
      Consume new objects if it can be created from Base64 string.
      Parameters:
      base64 - Base64 that represent the objects.
      consumer - The consumer that accept non-null objects.
    • listFromBase64

      public List<T> listFromBase64(String base64)
      Get list of objects by read Base64 string.
      Parameters:
      base64 - Base64 that represent the list.
      Returns:
      List of objects.
    • listFromBytes

      public List<T> listFromBytes(byte[] bytes)
      Get list of objects by read byte array.
      Parameters:
      bytes - Bytes to read,
      Returns:
      A list of converted objects from any saved compound.
    • fromBytes

      public T fromBytes(byte[] bytes)
      Get object from bytes.
      This method first read the bytes with ByteArrayInputStream to get an CompoundTag and convert it into object.
      Bytes -> CompoundTag -> Object
      Parameters:
      bytes - Bytes to read.
      Returns:
      a compound tag converted to object.
    • fromBytes

      public void fromBytes(byte[] bytes, Consumer<T> consumer)
      Consume new objects if it can be created from byte array.
      This method detect any CompoundTag inside provided bytes and convert into current instance object using the detected compression format, it is also compatible with any saved object inside ByteArrayTag, ListTag and BukkitObjectInputStream.
      Parameters:
      bytes - Byte array to read.
      consumer - The consumer that accept non-null objects.
    • fromBukkitObject

      public void fromBukkitObject(byte[] bytes, Consumer<T> consumer)
      Consume new objects if it can be created from byte array read by BukkitObjectInputStream.
      Parameters:
      bytes - Byte array to read.
      consumer - The consumer that accept non-null objects.