extends BinaryReader : Binary Read Write « File Stream « C# / C Sharp






extends BinaryReader

     
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SharpMod.IO
{
    public class ModBinaryReader : BinaryReader
    {
        public ModBinaryReader(Stream baseStream)
            : base(baseStream)
        {

        }

        public void Seek(int offset, SeekOrigin origin)
        {
            BaseStream.Seek(offset, origin);
        }

        public virtual int Tell()
        {
            try
            {
                return (int)(BaseStream.Position);
            }
            catch (System.IO.IOException ioe1)
            {
                return -1;
            }
        }

        /*public int Read(byte[] buffer, int offset, int count)
        {
            return BaseStream.Read(buffer, offset, count);
        }*/

        /*public virtual sbyte ReadSByte()
        {
            try
            {
                return (sbyte)this.ReadByte();
            }
            catch (System.IO.IOException ioe1)
            {
                return -1;
            }
        }*/

        public virtual short ReadUByte()
        {
            try
            {
                return (short)this.ReadByte();
            }
            catch (System.IO.IOException ioe1)
            {
                throw ioe1;
            }
        }

        public virtual bool ReadUBytes(short[] buffer, int number)
        {
            int pos = 0; 
            while (number > 0)
            {
                buffer[pos++] = ReadUByte(); number--;
            } 
            return !isEOF();
        }

        public virtual int ReadMotorolaUWord()
        {
            int result = ((int)ReadUByte()) << 8;
            result = (int)((short)result | (short)ReadUByte());
            return result;
        }

        public virtual int ReadIntelUWord()/* _mm_read_I_UWORD*/
        {
            int result = ReadUByte();
            result |= ((int)ReadUByte()) << 8;
            return result;
        }


        public virtual short ReadMotorolaSWord()
        {
            short result = (short)(ReadUByte() << 8);
            result |= ReadUByte();
            return result;
        }

        public virtual bool ReadIntelUWords(int[] buffer, int number)
        {
            int pos = 0; while (number > 0)
            {
                buffer[pos++] = ReadIntelUWord(); number--;
            } return !isEOF();
        }

        

        public virtual short ReadIntelSWord()
        {
            short result = ReadUByte();
            result |= (short)(ReadUByte() << 8);
            return result;
        }

        public virtual int ReadMotorolaULong()
        {
            int result = (ReadMotorolaUWord()) << 16;
            result |= ReadMotorolaUWord();
            return result;
        }

        public virtual int ReadIntelULong()
        {
            int result = ReadIntelUWord();
            result |= ((int)ReadIntelUWord()) << 16;
            return result;
        }

        public virtual int ReadMotorolaSLong()
        {
            return ((int)ReadMotorolaULong());
        }

        public virtual int ReadIntelSLong()
        {
            return ((int)ReadIntelULong());
        }

        public string ReadString(int length)
        {
            try
            {
                byte[] tmpBuffer = new byte[length];
                this.Read(tmpBuffer, 0, length);

                return System.Text.UTF8Encoding.UTF8.GetString(tmpBuffer, 0, length).Trim(new char[] {'\0'});
            }
            catch (System.IO.IOException ioe1)
            {
                throw ioe1;
            }
        }

        public virtual bool ReadSBytes(sbyte[] buffer, int number)
        {
            int pos = 0; while (number > 0)
            {
                buffer[pos++] = ReadSByte(); number--;
            }

            return !isEOF();
        }

        public virtual bool readMotorolaSWords(short[] buffer, int number)
        {
            int pos = 0; while (number > 0)
            {
                buffer[pos++] = ReadMotorolaSWord(); number--;
            } return !isEOF();
        }

        public virtual bool readIntelSWords(short[] buffer, int number)
        {
            int pos = 0; while (number > 0)
            {
                buffer[pos++] = ReadIntelSWord(); number--;
            } return !isEOF();
        }

        // isEOF is basically a utility function to catch all the
        // IOExceptions from the dependandt functions.
        // It's also make the code look more like the original
        // C source because it corresponds to feof.
        public virtual bool isEOF()
        {
            try
            {
                return (BaseStream.Position >= BaseStream.Length);
            }
            catch (System.IO.IOException ioe1)
            {
                return true;
            }
        }

        public void Rewind()
        {
            Seek(0, SeekOrigin.Begin);
        }

    }
}

   
    
    
    
    
  








Related examples in the same category

1.Binary Writer Reader
2.new BinaryReader(stream)
3.StreamWriter and BinaryWriter
4.new BinaryWriter(stream) and Write method
5.Creating a sequential-access file
6.Reading a sequential-access fileReading a sequential-access file
7.BinaryWriter and BinaryReader classes for the writing and reading of primitive typesBinaryWriter and BinaryReader classes for the writing and reading of primitive types
8.Working with the BinaryWriter Class
9.Working with the BinaryReader ClassWorking with the BinaryReader Class
10.File pointer move and read binary file
11.Write and then read back binary dataWrite and then read back binary data
12.Use BinaryReader and BinaryWriter to implement a simple inventory programUse BinaryReader and BinaryWriter to implement 
   a simple inventory program
13.Read the data from a file and desiralize it
14.Read and Write a Binary File
15.Read Struct out of a file with BinaryReader
16.Write a value at a given position. Used to write a size of data in an earlier located header.