Get bytes and read bytes : Byte Array « File Stream « C# / C Sharp






Get bytes and read bytes

       
using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization;
using System.Diagnostics;

namespace AltSerialize
{
    public static class Util
    {
        /// <summary>
        /// Gets an array of bytes from any primitive object type.
        /// </summary>
        /// <param name="obj">Object to retrieve bytes from</param>
        /// <param name="objectType">The object type passed in <paramref name="obj"/>.</param>
        /// <returns>Returns an array of bytes.</returns>
        public static byte[] GetBytes(object obj, Type objectType)
        {
            if (objectType == typeof(int)) return BitConverter.GetBytes((int)obj);
            else if (objectType == typeof(long)) return BitConverter.GetBytes((long)obj);
            else if (objectType == typeof(bool)) return new byte[] { (bool)obj == true ? (byte)1 : (byte)0 };
            else if (objectType == typeof(double)) return BitConverter.GetBytes((double)obj);
            else if (objectType == typeof(byte)) return new byte[] { (byte)obj };
            else if (objectType == typeof(sbyte)) return new byte[] { (byte)((sbyte)obj) };
            else if (objectType == typeof(short)) return BitConverter.GetBytes((short)obj);
            else if (objectType == typeof(ushort)) return BitConverter.GetBytes((ushort)obj);
            else if (objectType == typeof(uint)) return BitConverter.GetBytes((uint)obj);
            else if (objectType == typeof(ulong)) return BitConverter.GetBytes((ulong)obj);
            else if (objectType == typeof(float)) return BitConverter.GetBytes((float)obj);
            else if (objectType == typeof(char)) return BitConverter.GetBytes((char)obj);
            else if (objectType == typeof(IntPtr)) throw new Exception("IntPtr type is not supported.");
            else if (objectType == typeof(UIntPtr)) throw new Exception("UIntPtr type is not supported.");
            else
                throw new Exception("Could not retrieve bytes from the object type " + objectType.FullName + ".");
        }

        /// <summary>
        /// Turns an array of bytes into the specified object type.
        /// </summary>
        /// <param name="bytes">Array of bytes containing the object data.</param>
        /// <param name="objectType">The type of object to convert byte array to.</param>
        /// <returns>Returns an object of the type specified in <paramref name="objectType"/>.</returns>
        public static object ReadBytes(byte[] bytes, Type objectType)
        {
            if (objectType == typeof(bool)) return bytes[0] == (byte)1 ? true : false;
            else if (objectType == typeof(byte)) return bytes[0];
            else if (objectType == typeof(int)) return BitConverter.ToInt32(bytes, 0);
            else if (objectType == typeof(long)) return BitConverter.ToInt64(bytes, 0);
            else if (objectType == typeof(double)) return BitConverter.ToDouble(bytes, 0);
            else if (objectType == typeof(sbyte)) return (sbyte)bytes[0];
            else if (objectType == typeof(short)) return BitConverter.ToInt16(bytes, 0);
            else if (objectType == typeof(ushort)) return BitConverter.ToUInt16(bytes, 0);
            else if (objectType == typeof(uint)) return BitConverter.ToUInt32(bytes, 0);
            else if (objectType == typeof(ulong)) return BitConverter.ToUInt64(bytes, 0);
            else if (objectType == typeof(float)) return BitConverter.ToSingle(bytes, 0);
            else if (objectType == typeof(char)) return BitConverter.ToChar(bytes, 0);
            else if (objectType == typeof(IntPtr))
                throw new Exception("IntPtr type is not supported.");

            throw new Exception("Could not retrieve bytes from the object type " + objectType.FullName + ".");
        }

        // Write 24-bit unsigned int
        public static void WriteUInt24(this Stream s, int value)
        {
            s.WriteByte((byte)(value & 0xFF));
            s.WriteByte((byte)(((value >> 8) & 0xFF)));
            s.WriteByte((byte)(((value >> 16) & 0xFF)));
        }

        public static void WriteUInt16(this Stream s, int value)
        {
            s.WriteByte((byte)(value & 0xFF));
            s.WriteByte((byte)(((value >> 8) & 0xFF)));
        }

        /// <summary>
        /// Writes a byte to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, byte val)
        {
            s.WriteByte(val);
        }

        /// <summary>
        /// Writes a signed byte to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, sbyte val)
        {
            s.WriteByte((byte)val);
        }

        /// <summary>
        /// Writes an array of bytes to the serialization stream.
        /// </summary>
        /// <param name="bytes">Array of bytes to write.</param>
        /// <param name="offset">Offset to begin writing from.</param>
        /// <param name="count">Number of bytes to write.</param>
        public static void Write(this Stream s, byte[] bytes, int offset, int count)
        {
            s.Write(bytes, offset, count);
        }

        /// <summary>
        /// Writes an array of bytes to the serialization stream.
        /// </summary>
        /// <param name="bytes">Array of bytes to write.</param>
        public static void Write(this Stream s, byte[] bytes)
        {
            s.Write(bytes, 0, bytes.Length);
        }

        /// <summary>
        /// Writes a signed 32-bit value to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, Int32 value)
        {
            s.Write(BitConverter.GetBytes(value), 0, 4);
        }

        /// <summary>
        /// Writes an unsigned 32-bit value to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, UInt32 value)
        {
            s.Write(BitConverter.GetBytes(value), 0, 4);
        }

        /// <summary>
        /// Writes an unsigned 64-bit value to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, UInt64 value)
        {
            s.Write(BitConverter.GetBytes(value), 0, 8);
        }

        public static void Write(this Stream s, long value)
        {
            s.Write(BitConverter.GetBytes(value), 0, 8);
        }

        /// <summary>
        /// Writes a DateTime to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, DateTime value)
        {
            long binary = value.Ticks;
            Write(s, binary);
        }

        /// <summary>
        /// Writes a TimeSpan to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, TimeSpan value)
        {
            long binary = value.Ticks;
            Write(s, binary);
        }

        /// <summary>
        /// Writes a Guid to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, Guid value)
        {
            Write(s, value.ToByteArray());
        }

        /// <summary>
        /// Writes a Decimal to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, Decimal value)
        {
            int[] bits = Decimal.GetBits(value);
            Write(s, bits[0]);
            Write(s, bits[1]);
            Write(s, bits[2]);
            Write(s, bits[3]);
        }

        /// <summary>
        /// Writes a double-precision floating point value to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, double value)
        {
            s.Write(BitConverter.GetBytes(value), 0, 8);
        }

        /// <summary>
        /// Writes a single-precision floating point value to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, float value)
        {
            s.Write(BitConverter.GetBytes(value), 0, 4);
        }

        /// <summary>
        /// Writes a Char to the serialization stream.
        /// </summary>
        public static void Write(this Stream s, char value)
        {
            s.Write(BitConverter.GetBytes(value), 0, 2);
        }

        /// <summary>
        /// Writes a CultureInfo structure to the serialization stream.
        /// </summary>
        public static void WriteCultureInfo(System.Globalization.CultureInfo info)
        {
        }

        // Read 24-bit unsigned int
        private static int ReadUInt24(this Stream s)
        {
            // Read first two bytes.
            int newValue;
            newValue = s.ReadByte();
            newValue += (s.ReadByte() << 8);
            newValue += (s.ReadByte() << 16);
            return newValue;
        }

        /// <summary>
        /// Reads a Byte from the serialization stream.
        /// </summary>
        public static int ReadByte(this Stream s)
        {
            return s.ReadByte();
        }

        /// <summary>
        /// Reads a signed byte from the serialization stream.
        /// </summary>
        public static sbyte ReadSByte(this Stream s)
        {
            return (sbyte)s.ReadByte();
        }

        /// <summary>
        /// Reads an array of bytes from the serialization stream.
        /// </summary>
        /// <param name="count">Number of bytes to read.</param>
        /// <returns>Returns an array of bytes read from the deserialization stream.</returns>
        public static byte[] ReadBytes(this Stream s, int count)
        {
            byte[] ret = new byte[count];
            ReadBytes(s, ret, 0, count);
            return ret;
        }

        /// <summary>
        /// Reads an array of bytes from the serialization stream.
        /// </summary>
        /// <param name="bytes">Byte array to read into.</param>
        /// <param name="offset">Starting offset of <paramref>bytes</paramref>.</param>
        /// <param name="count">Number of bytes to read.</param>
        public static void ReadBytes(this Stream s, byte[] bytes, int offset, int count)
        {
            s.Read(bytes, offset, count);
        }

        /// <summary>
        /// Reads culture info from the serialization stream.
        /// </summary>
        public static System.Globalization.CultureInfo ReadCultureInfo()
        {
            return System.Globalization.CultureInfo.InvariantCulture;
        }
    }
}

   
    
    
    
    
    
    
  








Related examples in the same category

1.Convert a byte array to string using default encoding
2.Get the byte array from a string using default encoding
3.Write a byte array to a file
4.Read byte array from file
5.Read Stream to Byte array
6.Save Byte Array To File
7.Reinitializes a byte array to the given value in an optimized way: byteArraySet
8.Bit Array To Byte Array
9.Bit Array To Byte Array (2)
10.Checks if the input byte array contains only safe values, the data does not need to be encoded for use with LDIF
11.Convert byte array into a string
12.Byte Array To String
13.Bytes To Formatted String
14.Get string from byte array
15.Reads a file into a byte array
16.Read entire stream into a byte array
17.Read Stream to fill the byte array
18.Convert to easy-to-read byte value
19.Conversions between complex types and byte arrays.
20.Search a byte array for a sub byte array
21.Append various data types to byte array
22.Convert a byte array to an Object.
23.Compares the values of two byte arrays, and returns true only if every array member is identical
24.Convert an object to a byte array.
25.Bytes Equal
26.Compare Bytes
27.Pad bytes.
28.Bytes To Comma Separated List
29.Compare Byte Array Elements
30.Byte Arrays Are Equal
31.Converts a number of bytes into a more easily read form.
32.Int to byte array converter
33.Compares up to n elements of byte array a[] against byte array b[].
34.Utility method to compare two byte arrays for equality
35.Swap Byte Order
36.Copy as much data as possible for a read operation.