package com.evilco.mc.nbt.tag;

import com.evilco.mc.nbt.stream.NbtInputStream;
import com.evilco.mc.nbt.stream.NbtOutputStream;
import java.io.IOException;
import javax.annotation.Nonnull;

public class TagString extends AbstractTag {
   protected String value;

   public TagString(@Nonnull String name, @Nonnull String value) {
      super(name);
      this.setValue(value);
   }

   public TagString(@Nonnull NbtInputStream inputStream, boolean anonymous) throws IOException {
      super(inputStream, anonymous);
      int size = inputStream.readShort();
      byte[] data = new byte[size];
      inputStream.readFully(data);
      this.setValue(new String(data, ITag.STRING_CHARSET));
   }

   public byte getTagID() {
      return TagType.STRING.typeID;
   }

   public String getValue() {
      return this.value;
   }

   public void setValue(@Nonnull String s) {
      this.value = s;
   }

   public void write(NbtOutputStream outputStream, boolean anonymous) throws IOException {
      super.write(outputStream, anonymous);
      byte[] outputBytes = this.value.getBytes(ITag.STRING_CHARSET);
      outputStream.writeShort(outputBytes.length);
      outputStream.write(outputBytes);
   }
}
