Click here to Skip to main content
15,895,740 members
Articles / Programming Languages / C#

Named Binary Tag serialization

Rate me:
Please Sign up or sign in to vote.
4.93/5 (23 votes)
17 Feb 2015CC (ASA 3U)6 min read 67.5K   2.1K   51  
This article describes the file format NBT and shows how can be implemented in a real application to store data.
using System;
using System.IO;
using NBT.Exceptions;

namespace NBT.Tags
{
    public sealed class TagSByte : Tag, IEquatable<TagSByte>
    {
        public SByte value;

        public TagSByte() : this((sbyte)0)
        {
        }

        public TagSByte(sbyte value)
        {
            this.value = value;
        }

        internal TagSByte(Stream stream) : this((sbyte)0)
        {
            if (stream == null)
            {
                throw new NBT_InvalidArgumentNullException();
            }
            this.readTag(stream);
        }

        public override object Value
        {
            get
            {
                return this.value;
            }
            set
            {
                if (value == null)
                {
                    throw new NBT_InvalidArgumentNullException();
                }
                if (value.GetType() != typeof(sbyte))
                {
                    throw new NBT_InvalidArgumentException();
                }
                this.value = (sbyte)value;
            }
        }

        public override byte tagID
        {
            get 
            {
                return TagTypes.TagSByte; 
            }
        }

        public override string toString()
        {
            return this.value.ToString();
        }

        internal override void readTag(Stream stream)
        {
            if (stream == null)
            {
                throw new NBT_InvalidArgumentNullException();
            }
            this.value = TagSByte.ReadSByte(stream);
        }

        internal override void writeTag(Stream stream)
        {
            if (stream == null)
            {
                throw new NBT_InvalidArgumentNullException();
            }
            TagSByte.WriteSByte(stream, this.value);
        }

        internal static sbyte ReadSByte(Stream stream)
        {
            if (stream == null)
            {
                throw new NBT_InvalidArgumentNullException();
            }
            int num = stream.ReadByte();
            if (num == -1)
            {
                throw new NBT_EndOfStreamException();
            }
            return (sbyte)num;
        }

        internal static void WriteSByte(Stream stream, sbyte value)
        {
            if (stream == null)
            {
                throw new NBT_InvalidArgumentNullException();
            }
            stream.WriteByte((byte)value);
        }

        public override object Clone()
        {
            return new TagSByte(this.value);
        }

        public static explicit operator TagSByte(sbyte value)
        {
            return new TagSByte(value);
        }

        public override Type getType()
        {
            return typeof(TagSByte);
        }

        public bool Equals(TagSByte other)
        {
            bool bResult = false;
            try
            {
                bResult = this.value.Equals(other.value);
            }
            catch (ArgumentNullException nullEx)
            {
                throw new NBT_InvalidArgumentNullException(nullEx.Message, nullEx.InnerException);
            }
            catch (Exception ex)
            {
                throw new NBT_InvalidArgumentException(ex.Message, ex.InnerException);
            }
            return bResult;
        }

        public override bool Equals(Tag other)
        {
            bool bResult = true;

            if (typeof(TagSByte) != other.getType())
            {
                bResult = false;
            }
            else
            {
                bResult = this.Equals((TagSByte)other);
            }

            return bResult;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License


Written By
Software Developer
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions