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
{
///
/// Gets an array of bytes from any primitive object type.
///
/// Object to retrieve bytes from
/// The object type passed in .
/// Returns an array of bytes.
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 + ".");
}
///
/// Turns an array of bytes into the specified object type.
///
/// Array of bytes containing the object data.
/// The type of object to convert byte array to.
/// Returns an object of the type specified in .
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)));
}
///
/// Writes a byte to the serialization stream.
///
public static void Write(this Stream s, byte val)
{
s.WriteByte(val);
}
///
/// Writes a signed byte to the serialization stream.
///
public static void Write(this Stream s, sbyte val)
{
s.WriteByte((byte)val);
}
///
/// Writes an array of bytes to the serialization stream.
///
/// Array of bytes to write.
/// Offset to begin writing from.
/// Number of bytes to write.
public static void Write(this Stream s, byte[] bytes, int offset, int count)
{
s.Write(bytes, offset, count);
}
///
/// Writes an array of bytes to the serialization stream.
///
/// Array of bytes to write.
public static void Write(this Stream s, byte[] bytes)
{
s.Write(bytes, 0, bytes.Length);
}
///
/// Writes a signed 32-bit value to the serialization stream.
///
public static void Write(this Stream s, Int32 value)
{
s.Write(BitConverter.GetBytes(value), 0, 4);
}
///
/// Writes an unsigned 32-bit value to the serialization stream.
///
public static void Write(this Stream s, UInt32 value)
{
s.Write(BitConverter.GetBytes(value), 0, 4);
}
///
/// Writes an unsigned 64-bit value to the serialization stream.
///
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);
}
///
/// Writes a DateTime to the serialization stream.
///
public static void Write(this Stream s, DateTime value)
{
long binary = value.Ticks;
Write(s, binary);
}
///
/// Writes a TimeSpan to the serialization stream.
///
public static void Write(this Stream s, TimeSpan value)
{
long binary = value.Ticks;
Write(s, binary);
}
///
/// Writes a Guid to the serialization stream.
///
public static void Write(this Stream s, Guid value)
{
Write(s, value.ToByteArray());
}
///
/// Writes a Decimal to the serialization stream.
///
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]);
}
///
/// Writes a double-precision floating point value to the serialization stream.
///
public static void Write(this Stream s, double value)
{
s.Write(BitConverter.GetBytes(value), 0, 8);
}
///
/// Writes a single-precision floating point value to the serialization stream.
///
public static void Write(this Stream s, float value)
{
s.Write(BitConverter.GetBytes(value), 0, 4);
}
///
/// Writes a Char to the serialization stream.
///
public static void Write(this Stream s, char value)
{
s.Write(BitConverter.GetBytes(value), 0, 2);
}
///
/// Writes a CultureInfo structure to the serialization stream.
///
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;
}
///
/// Reads a Byte from the serialization stream.
///
public static int ReadByte(this Stream s)
{
return s.ReadByte();
}
///
/// Reads a signed byte from the serialization stream.
///
public static sbyte ReadSByte(this Stream s)
{
return (sbyte)s.ReadByte();
}
///
/// Reads an array of bytes from the serialization stream.
///
/// Number of bytes to read.
/// Returns an array of bytes read from the deserialization stream.
public static byte[] ReadBytes(this Stream s, int count)
{
byte[] ret = new byte[count];
ReadBytes(s, ret, 0, count);
return ret;
}
///
/// Reads an array of bytes from the serialization stream.
///
/// Byte array to read into.
/// Starting offset of bytes .
/// Number of bytes to read.
public static void ReadBytes(this Stream s, byte[] bytes, int offset, int count)
{
s.Read(bytes, offset, count);
}
///
/// Reads culture info from the serialization stream.
///
public static System.Globalization.CultureInfo ReadCultureInfo()
{
return System.Globalization.CultureInfo.InvariantCulture;
}
}
}