Click here to Skip to main content
15,896,727 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 TagULong : Tag, IEquatable<TagULong>
    {
        public ulong value;

        public TagULong() : this((ulong)0)
        {
        }

        public TagULong(ulong value)
        {
            this.value = value;
        }

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

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

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

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

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

        internal static ulong ReadULong(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.ToUInt64(buffer, 0);
        }

        internal static void WriteULong(Stream stream, ulong 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 TagULong(this.value);
        }

        public static explicit operator TagULong(ulong value)
        {
            return new TagULong(value);
        }

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

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