Click here to Skip to main content
15,885,309 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.3K   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 TagLong : Tag, IEquatable<TagLong>
    {
        public long value;

        public TagLong() : this((long)0)
        {
        }

        public TagLong(long value)
        {
            this.value = value;
        }

        internal TagLong(Stream stream) : this((long)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(long))
                {
                    throw new NBT_InvalidArgumentException();
                }
                this.value = (long)value;
            }
        }

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

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

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

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

        internal static long ReadLong(Stream stream)
        {
            if (stream == null)
            {
                throw new NBT_InvalidArgumentNullException();
            }
            byte[] buffer = new byte[8];
            if (stream.Read(buffer, 0, buffer.Length) != buffer.Length)
            {
                throw new NBT_EndOfStreamException();
            }
            if (BitConverter.IsLittleEndian == true)
            { 
                Array.Reverse(buffer);            
            }
            return BitConverter.ToInt64(buffer, 0);
        }

        internal static void WriteLong(Stream stream, long value)
        {
            if (stream == null)
            {
                throw new NBT_InvalidArgumentNullException();
            }
            byte[] bytes = BitConverter.GetBytes(value);
            if (BitConverter.IsLittleEndian == true)
            { 
                Array.Reverse(bytes);            
            }
            stream.Write(bytes, 0, bytes.Length);
        }

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

        public static explicit operator TagLong(long value)
        {
            return new TagLong(value);
        }

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

        public bool Equals(TagLong 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(TagLong) != other.getType())
            {
                bResult = false;
            }
            else
            {
                bResult = this.Equals((TagLong)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