private static T[] InternalToArray <T>(PacketReader reader, PacketConverter converter) { var readerList = reader.GetList(); var count = readerList.Count; if (count < 1) { return(new T[0]); } try { var result = new T[count]; var generic = (PacketConverter <T>)converter; for (var i = 0; i < count; i++) { result[i] = generic.GetValue(readerList[i].block); } return(result); } catch (Exception ex) when(PacketException.ReThrowFilter(ex)) { throw PacketException.ConversionError(ex); } }
internal void ToDictionary <TK, TV>(PacketConverter indexConverter, PacketConverter elementConverter, DictionaryAbstract <TK, TV> dictionary) { if (this.Length == 0) { return; } var indexGeneric = (PacketConverter <TK>)indexConverter; var elementGeneric = (PacketConverter <TV>)elementConverter; var vernier = (Vernier)this; try { while (vernier.Any) { vernier.FlushExcept(indexGeneric.Length); var key = indexGeneric.GetValue(vernier.Buffer, vernier.Offset, vernier.Length); vernier.FlushExcept(elementGeneric.Length); var value = elementGeneric.GetValue(vernier.Buffer, vernier.Offset, vernier.Length); dictionary.Add(key, value); } } catch (Exception ex) when(PacketException.ReThrowFilter(ex)) { throw PacketException.ConversionError(ex); } }
internal static T GetValueChecked <T>(this PacketConverter <T> converter, byte[] buffer, int offset, int length, bool check = false) { try { if (check && converter.Length > length) { throw PacketException.Overflow(); } return(converter.GetValue(buffer, offset, length)); } catch (Exception ex) when(PacketException.ReThrowFilter(ex)) { throw PacketException.ConversionError(ex); } }
internal static byte[] GetBytesChecked <T>(this PacketConverter <T> converter, T value) { try { var buffer = converter.GetBytes(value); if (buffer == null) { buffer = Empty.Array <byte>(); } var define = converter.Length; if (define > 0 && define != buffer.Length) { throw PacketException.ConversionMismatch(define); } return(buffer); } catch (Exception ex) when(PacketException.ReThrowFilter(ex)) { throw PacketException.ConversionError(ex); } }
internal T[] ToArray <T>(PacketConverter converter) { if (this.Length < 1) { return(new T[0]); } if (typeof(T) == typeof(byte)) { return((T[])(object)UnmanagedArrayConverter <byte> .ToValue(this.Buffer, this.Offset, this.Length)); } else if (typeof(T) == typeof(sbyte)) { return((T[])(object)UnmanagedArrayConverter <sbyte> .ToValue(this.Buffer, this.Offset, this.Length)); } var define = converter.Length; var quotient = Math.DivRem(this.Length, define, out var remainder); if (remainder != 0) { throw PacketException.Overflow(); } try { var target = new T[quotient]; var generic = (PacketConverter <T>)converter; for (var i = 0; i < quotient; i++) { target[i] = generic.GetValue(this.Buffer, this.Offset + i * define, define); } return(target); } catch (Exception ex) when(PacketException.ReThrowFilter(ex)) { throw PacketException.ConversionError(ex); } }