/*
 * Decompiled with CFR 0.152.
 */
package com.evilco.mc.nbt.tag;

import com.evilco.mc.nbt.stream.NbtInputStream;
import com.evilco.mc.nbt.stream.NbtOutputStream;
import com.evilco.mc.nbt.tag.AbstractTag;
import com.evilco.mc.nbt.tag.ITag;
import com.evilco.mc.nbt.tag.TagType;
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);
        short size = inputStream.readShort();
        byte[] data = new byte[size];
        inputStream.readFully(data);
        this.setValue(new String(data, ITag.STRING_CHARSET));
    }

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

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

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

    @Override
    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);
    }
}
