public void Int64ReadWrite() { Int64 value; Int64 result; value = 20; ResetPosition(); _writer.WriteInt64(value); ResetPosition(); result = _reader.ReadInt64(); Assert.AreEqual(value, result); //Multiple bytes value = 300000000; ResetPosition(); _writer.WriteInt64(value); ResetPosition(); result = _reader.ReadInt64(); Assert.AreEqual(value, result); //Negative Values value = -30; ResetPosition(); _writer.WriteInt64(value); ResetPosition(); result = _reader.ReadInt64(); Assert.AreEqual(value, result); //Int 32 Max Values + 1 value = 2147483648; ResetPosition(); _writer.WriteInt64(value); ResetPosition(); result = _reader.ReadInt64(); Assert.AreEqual(value, result); //Int 32 Min Values - 1 value = -2147483649; ResetPosition(); _writer.WriteInt64(value); ResetPosition(); result = _reader.ReadInt64(); Assert.AreEqual(value, result); }
public List<TableRow> DeserializeUTFChunk(byte[] chunk) { m_chunk = chunk; if (!Binary.AsciiEqual (m_chunk, 0, "@UTF")) throw new InvalidFormatException(); var chunk_length = BigEndian.ToInt32 (m_chunk, 4); using (var mem = new MemoryStream (m_chunk, 8, chunk_length)) using (var input = new BigEndianReader (mem)) { int rows_offset = input.ReadInt32(); int strings_offset = input.ReadInt32() + 8; int data_offset = input.ReadInt32() + 8; input.Skip (4); int column_count = input.ReadInt16(); int row_length = input.ReadInt16(); int row_count = input.ReadInt32(); var columns = new List<Column> (column_count); for (int i = 0; i < column_count; ++i) { byte flags = input.ReadByte(); if (0 == flags) { input.Skip (3); flags = input.ReadByte(); } int name_offset = strings_offset + input.ReadInt32(); var column = new Column { Flags = (TableFlags)flags, Name = ReadString (name_offset), }; columns.Add (column); } var table = new List<TableRow> (row_count); int next_offset = rows_offset; for (int i = 0; i < row_count; ++i) { input.Position = next_offset; next_offset += row_length; var row = new TableRow (column_count); table.Add (row); foreach (var column in columns) { var storage = column.Flags & TableFlags.StorageMask; if (TableFlags.StorageNone == storage || TableFlags.StorageZero == storage || TableFlags.StorageConstant == storage) continue; switch (column.Flags & TableFlags.TypeMask) { case TableFlags.TypeByte: row[column.Name] = (int)input.ReadByte(); break; case TableFlags.TypeSByte: row[column.Name] = (int)input.ReadSByte(); break; case TableFlags.TypeUInt16: row[column.Name] = (int)input.ReadUInt16(); break; case TableFlags.TypeInt16: row[column.Name] = (int)input.ReadInt16(); break; case TableFlags.TypeUInt32: case TableFlags.TypeInt32: row[column.Name] = input.ReadInt32(); break; case TableFlags.TypeUInt64: case TableFlags.TypeInt64: row[column.Name] = input.ReadInt64(); break; case TableFlags.TypeFloat32: row[column.Name] = input.ReadSingle(); break; case TableFlags.TypeString: { int offset = strings_offset + input.ReadInt32(); row[column.Name] = ReadString (offset); break; } case TableFlags.TypeData: { int offset = data_offset + input.ReadInt32(); int length = input.ReadInt32(); row[column.Name] = m_chunk.Skip (offset).Take (length).ToArray(); break; } default: throw new NotSupportedException(); } } } return table; } }
public NbtTag Read(NbtTagType type) { int length; switch (type) { case NbtTagType.End: return(null); case NbtTagType.Byte: return(NbtTag.Create(_reader.ReadByte())); case NbtTagType.Short: return(NbtTag.Create(_reader.ReadInt16())); case NbtTagType.Int: return(NbtTag.Create(_reader.ReadInt32())); case NbtTagType.Long: return(NbtTag.Create(_reader.ReadInt64())); case NbtTagType.Float: return(NbtTag.Create(_reader.ReadSingle())); case NbtTagType.Double: return(NbtTag.Create(_reader.ReadDouble())); case NbtTagType.ByteArray: length = _reader.ReadInt32(); return(NbtTag.Create(_reader.ReadBytes(length))); case NbtTagType.String: length = _reader.ReadInt16(); byte[] array = _reader.ReadBytes(length); return(NbtTag.Create(Encoding.UTF8.GetString(array))); case NbtTagType.List: NbtTagType listType = (NbtTagType)_reader.ReadByte(); if (listType < NbtTagType.End || listType > NbtTagType.Compound) { throw new FormatException("'" + (int)type + "' is not a valid ListType."); } int count = _reader.ReadInt32(); NbtTag list = NbtTag.CreateList(listType); for (int i = 0; i < count; i++) { list.Add(Read(listType)); } return(list); case NbtTagType.Compound: NbtTag compound = NbtTag.CreateCompound(); while (true) { string name; NbtTag item = Read(out name); if (item == null) { return(compound); } compound.Add(name, item); } case NbtTagType.IntArray: length = _reader.ReadInt32(); int[] intArray = new int[length]; for (int i = 0; i < length; i++) { intArray[i] = _reader.ReadInt32(); } return(NbtTag.Create(intArray)); default: throw new FormatException("'" + (int)type + "' is not a valid TagType."); } }
public IEnumerable GetContents() { var chars = TypeTag.ToArray(); var activeArrays = new Stack <ArrayList>(); using (var reader = new BigEndianReader(GetContentStream())) { for (int i = 1; i < chars.Length; i++) { object content; switch (chars[i]) { case Osc.TypeTag.Int32: content = reader.ReadInt32(); break; case Osc.TypeTag.Float: content = reader.ReadSingle(); break; case Osc.TypeTag.String: case Osc.TypeTag.Alternate: content = MessageParser.ReadString(reader); break; case Osc.TypeTag.Blob: content = MessageParser.ReadBlob(reader); break; case Osc.TypeTag.Int64: content = reader.ReadInt64(); break; case Osc.TypeTag.TimeTag: content = MessageParser.ReadTimeTag(reader); break; case Osc.TypeTag.Double: content = reader.ReadDouble(); break; case Osc.TypeTag.Char: content = MessageParser.ReadChar(reader); break; case Osc.TypeTag.True: content = true; break; case Osc.TypeTag.False: content = false; break; case Osc.TypeTag.Infinitum: content = float.PositiveInfinity; break; case Osc.TypeTag.ArrayBegin: activeArrays.Push(new ArrayList()); continue; case Osc.TypeTag.ArrayEnd: var array = activeArrays.Pop(); content = array.ToArray(); break; default: case Osc.TypeTag.Nil: content = null; break; } if (activeArrays.Count > 0) { var array = activeArrays.Peek(); array.Add(content); } else { yield return(content); } } } }