package com.evilco.mc.nbt.stream;

import com.evilco.mc.nbt.tag.ITag;
import com.evilco.mc.nbt.tag.TagType;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;

public class NbtInputStream extends DataInputStream {
   public NbtInputStream(InputStream in) {
      super(in);
   }

   public ITag readTag() throws IOException {
      byte type = this.readByte();
      TagType tagType = TagType.valueOf(type);
      if (tagType == null) {
         throw new IOException("Invalid NBT tag: Found unknown tag type " + type + ".");
      } else {
         return tagType == TagType.END ? null : this.readTag(tagType, false);
      }
   }

   public ITag readTag(TagType type, boolean anonymous) throws IOException {
      Constructor constructor = null;

      try {
         constructor = type.tagType.getConstructor(NbtInputStream.class, Boolean.TYPE);
      } catch (NoSuchMethodException var6) {
         throw new IOException("Invalid NBT implementation state: Type " + type.tagType.getName() + " has no de-serialization constructor.");
      }

      try {
         return (ITag)constructor.newInstance(this, anonymous);
      } catch (Exception var5) {
         throw new IOException("Invalid NBT implementation state: Type " + type.tagType.getName() + " in (" + this.getClass().getName() + ") has no valid constructor: " + var5.getMessage(), var5);
      }
   }
}
