/// <summary> /// Adiciona um item na fonte. /// </summary> /// <param name="item">Item contendo os dados.</param> public void Add(object item) { if (item == null) { throw new ArgumentNullException("item"); } if (item.GetType() != baseType) { throw new InvalidOperationException("Invalid item type."); } long pos = source.Position; source.Write(new byte[] { 0, 0, 0, 0 }, 0, sizeof(int)); BFormatter.SerializeBase(source, coreSupports, memberAllowNullCount, 0, item); int size = (int)(source.Position - sizeof(int) - pos); long endPos = source.Position; source.Seek(pos, SeekOrigin.Begin); source.Write(BitConverter.GetBytes(size), 0, sizeof(int)); source.Seek(endPos, SeekOrigin.Begin); _count++; }
/// <summary> /// Recupera o item. /// </summary> /// <returns></returns> public object GetItem() { if (currentPosition < 0) { throw new InvalidOperationException("Item not ready."); } navStream.Seek(currentStreamPosition, SeekOrigin.Begin); return(BFormatter.DeserializeBase(navStream, baseType, coreSupports, memberAllowNullCount, 0, null)); }
/// <summary> /// Inicializa a instância. /// </summary> private void Initialize() { navStream.Seek(beginStreamPosition, SeekOrigin.Begin); currentPosition = -1; currentStreamPosition = 0; sizeCurrentItem = 0; coreSupports = BFormatter.LoadTypeInformation(baseType, out memberAllowNullCount); if (navStream.Length > 0) { _count = BFormatter.ReadArrayLenght(navStream, int.MaxValue); currentStreamPosition = navStream.Position; } else { _count = 0; } }
private void Initialize() { coreSupports = BFormatter.LoadTypeInformation(baseType, out memberAllowNullCount); sourceBeginPosition = source.Position; source.Write(BitConverter.GetBytes(_count), 0, sizeof(int)); }
/// <summary> /// Lê os dados da stream e salva no objeto /// </summary> /// <param name="stream"></param> /// <param name="type">Tipo de dado a ser lido.</param> /// <return>Valor lido.</return> private static object ReadData(Stream stream, Type type, int maxLenght) { if (type.Name == "Nullable`1") { type = Nullable.GetUnderlyingType(type); } Type typeEnum = null; if (type.IsEnum) { typeEnum = type; type = Enum.GetUnderlyingType(type); } else if (IsStruct(type) && !type.IsAssignableFrom(typeof(DateTime)) && !type.IsAssignableFrom(typeof(decimal))) { object obj = Activator.CreateInstance(type); DeserializeBase(stream, type, null, 0, 0, obj); return(obj); } int size = 0; if (type.IsAssignableFrom(typeof(string))) { if (maxLenght > 0) { if (maxLenght < byte.MaxValue) { size = 1; } else if (maxLenght < ushort.MaxValue) { size = sizeof(ushort); } else if (maxLenght < int.MaxValue) { size = sizeof(uint); } } else { size = sizeof(ushort); } byte[] bufferAux = new byte[size]; stream.Read(bufferAux, 0, size); if (maxLenght > 0) { if (maxLenght < byte.MaxValue) { size = (int)bufferAux[0]; } else if (maxLenght < ushort.MaxValue) { size = BitConverter.ToInt16(bufferAux, 0); } else if (maxLenght < int.MaxValue) { size = BitConverter.ToInt32(bufferAux, 0); } } else { size = BitConverter.ToUInt16(bufferAux, 0); } } else if (type.IsAssignableFrom(typeof(bool))) { size = sizeof(bool); } else if (type.IsAssignableFrom(typeof(DateTime))) { size = sizeof(long); } else if (type.IsAssignableFrom(typeof(decimal))) { size = sizeof(decimal); } else if (type.IsAssignableFrom(typeof(char))) { size = sizeof(char); } else if (type.IsAssignableFrom(typeof(byte))) { size = sizeof(byte); } else if (type.IsAssignableFrom(typeof(byte[]))) { if (maxLenght > 0) { if (maxLenght < byte.MaxValue) { size = 1; } else if (maxLenght < ushort.MaxValue) { size = sizeof(ushort); } else if (maxLenght < int.MaxValue) { size = sizeof(uint); } } else { size = sizeof(ushort); } byte[] bufferAux = new byte[size]; stream.Read(bufferAux, 0, size); if (maxLenght > 0) { if (maxLenght < byte.MaxValue) { size = (int)bufferAux[0]; } else if (maxLenght < ushort.MaxValue) { size = BitConverter.ToInt16(bufferAux, 0); } else if (maxLenght < int.MaxValue) { size = BitConverter.ToInt32(bufferAux, 0); } } else { size = BitConverter.ToUInt16(bufferAux, 0); } bufferAux = new byte[size]; stream.Read(bufferAux, 0, size); return(bufferAux); } else if (!type.IsArray && !Array.Exists(coreTypes, delegate(Type tc1) { return(tc1 == type); })) { return(DeserializeBase(stream, type, null, 0, maxLenght, null)); } else { try { size = System.Runtime.InteropServices.Marshal.SizeOf(type); } catch (ArgumentException) { byte[] buffer = new byte[sizeof(int)]; stream.Read(buffer, 0, sizeof(int)); size = BitConverter.ToInt16(buffer, 0); buffer = new byte[size]; stream.Read(buffer, 0, size); return(BFormatter.Deserialize(buffer, type)); } } if (type.IsArray) { return(DeserializeBase(stream, type, null, 0, maxLenght, null)); } else { byte[] buffer = new byte[size]; stream.Read(buffer, 0, size); if (type.IsAssignableFrom(typeof(string))) { return(Encoding.Default.GetString(buffer, 0, size)); } else if (type.IsAssignableFrom(typeof(byte[]))) { return(buffer); } else if (type.IsAssignableFrom(typeof(byte))) { return(buffer[0]); } else if (type.IsAssignableFrom(typeof(DateTime))) { return(new DateTime(BitConverter.ToInt64(buffer, 0))); } else if (type.IsAssignableFrom(typeof(decimal))) { return(BytesToDecimal(buffer)); } else { MethodInfo m = typeof(BitConverter).GetMethod("To" + type.Name); if (typeEnum != null) { return(Enum.ToObject(typeEnum, m.Invoke(null, new object[] { buffer, 0 }))); } else { return(m.Invoke(null, new object[] { buffer, 0 })); } } } }
/// <summary> /// Escreve o valor do objeto na stream. /// </summary> /// <param name="stream">Stream aonde serão salvos os dados do objeto.</param> /// <param name="o">Objeto contendo os dados.</param> /// <param name="type">Tipo do valor a ser serializado.</param> /// <param name="maxLenght">Tamanho máximo a ser serializado.</param> private static void WriteData(Stream stream, object o, Type type, int maxLenght) { if (type.IsAssignableFrom(typeof(string)) && o == null) { o = ""; } if (type.Name == "Nullable`1") { type = Nullable.GetUnderlyingType(type); if (o == null) { o = Activator.CreateInstance(type); } } if (type.IsEnum) { type = Enum.GetUnderlyingType(type); switch (type.Name) { case "Int16": o = (short)o; break; case "UInt16": o = (ushort)o; break; case "Int32": o = (int)o; break; case "UInt32": o = (uint)o; break; case "Byte": o = (byte)o; break; default: o = (int)o; break; } } else if (IsStruct(type) && !type.IsAssignableFrom(typeof(DateTime)) && !type.IsAssignableFrom(typeof(decimal))) { SerializeBase(stream, null, 0, 0, o); return; } if (type.IsAssignableFrom(typeof(string))) { string s = (string)o; int size = 0; int lenght = s.Length; if (maxLenght > 0) { lenght = (lenght > maxLenght ? maxLenght : lenght); if (maxLenght < byte.MaxValue) { size = 1; } else if (maxLenght < ushort.MaxValue) { size = sizeof(ushort); } else if (maxLenght < int.MaxValue) { size = sizeof(uint); } } else { size = sizeof(ushort); lenght = s.Length; } stream.Write(BitConverter.GetBytes(lenght), 0, size); stream.Write(Encoding.Default.GetBytes(s), 0, lenght); } else if (type.IsAssignableFrom(typeof(DateTime))) { stream.Write(BitConverter.GetBytes(((DateTime)o).Ticks), 0, sizeof(long)); } else if (type.IsAssignableFrom(typeof(decimal))) { stream.Write(DecimalToBytes(Convert.ToDecimal(o)), 0, sizeof(decimal)); } else if (type.IsAssignableFrom(typeof(byte))) { stream.WriteByte((byte)o); } else if (type.IsAssignableFrom(typeof(byte[]))) { byte[] buffer = (byte[])o; int size = 0; int lenght = buffer.Length; if (maxLenght > 0) { lenght = (lenght > maxLenght ? maxLenght : lenght); if (maxLenght < byte.MaxValue) { size = 1; } else if (maxLenght < ushort.MaxValue) { size = sizeof(ushort); } else if (maxLenght < int.MaxValue) { size = sizeof(uint); } } else { size = sizeof(ushort); lenght = buffer.Length; } stream.Write(BitConverter.GetBytes(lenght), 0, size); stream.Write(buffer, 0, lenght); } else if (type.IsArray) { SerializeBase(stream, null, 0, maxLenght, o); } else { int size = 0; if (type.IsAssignableFrom(typeof(bool))) { size = sizeof(bool); } else if (type.IsAssignableFrom(typeof(char))) { size = sizeof(char); } else if (!Array.Exists(coreTypes, delegate(Type tc1) { return(tc1 == type); })) { SerializeBase(stream, null, 0, 0, o); return; } else { try { size = System.Runtime.InteropServices.Marshal.SizeOf(type); } catch (ArgumentException) { var buffer = BFormatter.Serialize(o); stream.Write(BitConverter.GetBytes(buffer.Length), 0, sizeof(int)); stream.Write(buffer, 0, buffer.Length); return; } } MethodInfo m = typeof(BitConverter).GetMethod("GetBytes", BindingFlags.Public | BindingFlags.Static, null, new Type[] { type }, null); stream.Write((byte[])m.Invoke(null, BindingFlags.Default, null, new object[] { o }, CultureInfo.CurrentCulture), 0, size); } }