bool SkipFrameTable(IBinaryStream file) { file.Position = 4; int table_count = file.ReadInt16(); file.Position = 8; for (int i = 0; i < table_count; ++i) { switch (file.ReadByte()) { case 0: break; case 1: file.Seek(8, SeekOrigin.Current); break; case 2: case 3: case 4: case 5: file.Seek(4, SeekOrigin.Current); break; default: return(false); } } int count = file.ReadUInt16(); file.Seek(count * 8, SeekOrigin.Current); return(true); }
public override ImageMetaData ReadMetaData(IBinaryStream stream) { stream.Seek(4, SeekOrigin.Current); int data_size = stream.ReadInt32(); stream.Seek(4, SeekOrigin.Current); using (var reader = new Reader(stream.AsStream, 0x22)) // BMP header { reader.Unpack(); var bmp = reader.Data; if (bmp[0] != 'B' || bmp[1] != 'M') { return(null); } int width = LittleEndian.ToInt32(bmp, 0x12); int height = LittleEndian.ToInt32(bmp, 0x16); int bpp = LittleEndian.ToInt16(bmp, 0x1c); return(new GrdMetaData { Width = (uint)width, Height = (uint)height, BPP = bpp, DataSize = data_size, }); } }
} // 'HG-2' public override ImageMetaData ReadMetaData(IBinaryStream stream) { stream.Position = 8; var info = new Hg2MetaData(); int type = stream.ReadInt32(); if (0x25 == type) { info.HeaderSize = 0x58; } else if (0x20 == type) { info.HeaderSize = 0x50; } else { return(null); } info.Width = stream.ReadUInt32(); info.Height = stream.ReadUInt32(); info.BPP = stream.ReadInt32(); stream.Seek(8, SeekOrigin.Current); info.DataPacked = stream.ReadInt32(); info.DataUnpacked = stream.ReadInt32(); info.CtlPacked = stream.ReadInt32(); info.CtlUnpacked = stream.ReadInt32(); stream.Seek(8, SeekOrigin.Current); info.CanvasWidth = stream.ReadUInt32(); info.CanvasHeight = stream.ReadUInt32(); info.OffsetX = stream.ReadInt32(); info.OffsetY = stream.ReadInt32(); return(info); }
public WadyInput(IBinaryStream input) : base(new MemoryStream()) { input.Seek(5, SeekOrigin.Begin); MulValue = input.ReadUInt8(); input.Seek(6, SeekOrigin.Current); int src_size = input.ReadInt32(); input.Seek(16, SeekOrigin.Current); var format = new WaveFormat(); format.FormatTag = input.ReadUInt16(); format.Channels = input.ReadUInt16(); format.SamplesPerSecond = input.ReadUInt32(); format.AverageBytesPerSecond = input.ReadUInt32(); format.BlockAlign = input.ReadUInt16(); format.BitsPerSample = input.ReadUInt16(); format.ExtraSize = 0; this.Format = format; int remaining = (int)(input.Length - input.Position); if (remaining == src_size) { (Source as MemoryStream).Capacity = src_size * 2; Decode(input, src_size, Source); } else { Decode2(input, Source); } Source.Position = 0; this.PcmSize = Source.Length; input.Dispose(); }
public override ImageMetaData ReadMetaData(IBinaryStream stream) { stream.Position = 4; if (stream.ReadByte() != '3') { return(null); } var rect = new Rectangle(0, 0, 0, 0); int count = stream.ReadInt32(); for (int i = 0; i < count; ++i) { stream.ReadInt32(); int name_length = stream.ReadByte(); stream.Seek(name_length, SeekOrigin.Current); int x = stream.ReadInt32(); int y = stream.ReadInt32(); int w = stream.ReadInt32() - x; int h = stream.ReadInt32() - y; if (name_length > 0) { var part_rect = new Rectangle(x, y, w, h); rect = Rectangle.Union(rect, part_rect); } stream.Seek(12, SeekOrigin.Current); } uint data_size = stream.ReadUInt32(); if (data_size > stream.Length - stream.Position) { return(null); } return(ReadCompressionMetaData(stream, rect)); }
/// <summary> /// Reads the schema, maintains stream position. /// </summary> public static int[] GetFieldIds(BinaryObjectHeader hdr, IIgniteInternal ignite, IBinaryStream stream, int objectPos) { Debug.Assert(stream != null); if (hdr.IsCompactFooter) { // Get schema from Java return(GetFieldIds(hdr, ignite)); } var pos = stream.Position; stream.Seek(objectPos + hdr.SchemaOffset, SeekOrigin.Begin); var count = hdr.SchemaFieldCount; var offsetSize = hdr.SchemaFieldOffsetSize; var res = new int[count]; for (var i = 0; i < count; i++) { res[i] = stream.ReadInt(); stream.Seek(offsetSize, SeekOrigin.Current); // Skip offsets. } stream.Seek(pos, SeekOrigin.Begin); return(res); }
void UnpackSv2(byte[] output, int dst, int channels) { if (channels != 1) { m_input.Seek((channels - 1) * 4, SeekOrigin.Current); } int step = channels * 2; for (int i = 0; i < channels; ++i) { ResetBits(); int pos = dst; short last_word = 0; while (pos < output.Length) { int word; if (GetNextBit() != 0) { if (GetNextBit() != 0) { word = GetBits(10) << 6; } else { int repeat; if (GetNextBit() != 0) { int bit_length = 0; do { ++bit_length; }while (GetNextBit() != 0); repeat = GetBits(bit_length) + 4; } else { repeat = GetBits(2); } word = 0; while (repeat-- > 0) { output[pos] = 0; output[pos + 1] = 0; pos += step; } } } else { int adjust = (short)(GetBits(5) << 11) >> 5; word = last_word + adjust; } LittleEndian.Pack((short)word, output, pos); last_word = (short)word; pos += step; } dst += 2; } }
public void Unpack() { m_input.Position = m_info.DataOffset; uint name_length = m_input.ReadUInt32(); m_input.Seek(name_length, SeekOrigin.Current); uint mask = m_input.ReadUInt32(); m_input.Seek(9, SeekOrigin.Current); int layer_count = m_input.ReadInt32(); if (layer_count < 1) { throw new InvalidFormatException(); } // XXX only first frame is interpreted. var frame = new Frame(layer_count); frame.Width = m_input.ReadInt32(); frame.Height = m_input.ReadInt32(); frame.BPP = m_input.ReadInt32(); if (frame.BPP <= 0) { throw new InvalidFormatException(); } if (frame.BPP <= 8) { frame.Palette = ImageFormat.ReadColorMap(m_input.AsStream, 1 << frame.BPP); } frame.SetStride(); m_frames.Add(frame); for (int i = 0; i < layer_count; ++i) { m_input.ReadInt32(); // left m_input.ReadInt32(); // top m_input.ReadByte(); // visibility m_input.ReadInt32(); // (-1) TransColor m_input.ReadInt32(); // (0xFF) alpha m_input.ReadByte(); // AlphaOn name_length = m_input.ReadUInt32(); m_input.Seek(name_length, SeekOrigin.Current); if (m_info.Version >= 107) { m_input.ReadByte(); // lock } var layer = new Layer(); int layer_size = m_input.ReadInt32(); layer.Pixels = UnpackLayer(frame, layer_size); int alpha_size = m_input.ReadInt32(); if (alpha_size != 0) { layer.Alpha = UnpackLayer(frame, alpha_size, true); } frame.Layers.Add(layer); } Flatten(0); }
public List <Entry> Deserialize() { m_index.Position = 8; int count = Binary.BigEndian(m_index.ReadInt32()); m_name_list = new string[count]; for (int i = 0; i < count; ++i) { int length = m_index.ReadUInt8(); m_name_list[i] = m_index.ReadCString(length, Encoding.UTF8); } count = Binary.BigEndian(m_index.ReadInt32()); m_dir = new List <Entry> (count); for (int i = 0; i < count; ++i) { m_index.ReadUInt16(); ushort flags = Binary.BigEndian(m_index.ReadUInt16()); uint offset = Binary.BigEndian(m_index.ReadUInt32()); uint size = Binary.BigEndian(m_index.ReadUInt32()); var entry = new SxEntry { Flags = flags, Offset = (long)offset << 4, Size = size, IsPacked = 0 != (flags & 0x03), }; if (!entry.CheckPlacement(m_max_offset)) { return(null); } m_dir.Add(entry); } count = Binary.BigEndian(m_index.ReadUInt16()); for (int i = 0; i < count; ++i) { m_index.ReadUInt32(); m_index.ReadUInt32(); m_index.ReadUInt32(); Binary.BigEndian(m_index.ReadUInt32()); // archive body length m_index.ReadUInt64(); m_index.Seek(16, SeekOrigin.Current); // MD5 sum } count = Binary.BigEndian(m_index.ReadUInt16()); if (count > 0) { m_index.Seek(count * 24, SeekOrigin.Current); } DeserializeTree(); return(m_dir); }
} // 'dcf ' public override ImageMetaData ReadMetaData(IBinaryStream stream) { stream.Seek(4, SeekOrigin.Current); uint header_size = stream.ReadUInt32(); long data_pos = stream.Position + header_size; if (stream.ReadInt32() != 1) { return(null); } uint width = stream.ReadUInt32(); uint height = stream.ReadUInt32(); int bpp = stream.ReadInt32(); int name_length = stream.ReadInt32(); if (name_length <= 0) { return(null); } int shift = (name_length % 7) + 1; var name_bits = stream.ReadBytes(name_length); for (int i = 0; i < name_length; ++i) { name_bits[i] = Binary.RotByteL(name_bits[i], shift); } return(new DcfMetaData { Width = width, Height = height, BPP = bpp, BaseName = Encodings.cp932.GetString(name_bits), DataOffset = data_pos, }); }
public void Unpack() { if (2 == m_type) { while (0 != m_input.ReadUInt8()) { int size = m_input.ReadInt32(); if (size < 4) { throw new InvalidFormatException(); } m_input.Seek(size - 4, SeekOrigin.Current); } } if (8 == m_bpp) { Format = PixelFormats.Indexed8; UnpackPalette(); UnpackIndexed(m_output); } else if (24 == m_bpp) { Format = PixelFormats.Bgr24; UnpackRGB(); } else { Format = PixelFormats.Bgra32; UnpackRGBA(); UnpackAlpha(); } }
public string LoadRioTypeCore(out uint signature) { signature = m_input.ReadUInt32(); if (!CoreSignatures.Contains(signature)) { throw new InvalidFormatException("[RIO] invalid signature"); } int version = ReadUInt16(); if (version >= 0x10 && version <= 0x3FFF) { Schema = version; if (version >= 0x11) { m_field_4C &= 0xFFFF; m_field_4C |= ReadUInt16() << 16; } } else { m_input.Seek(-2, SeekOrigin.Current); } if (EncryptedSignature == signature) { m_field_4C |= 0xC; } return(ReadClass()); }
public override ImageMetaData ReadMetaData(IBinaryStream file) { if (0xFF != file.ReadByte() || 0xD8 != file.ReadByte()) { return(null); } while (-1 != file.PeekByte()) { ushort marker = Binary.BigEndian(file.ReadUInt16()); if ((marker & 0xff00) != 0xff00) { break; } int length = Binary.BigEndian(file.ReadUInt16()); if ((marker & 0x00f0) == 0xc0 && marker != 0xffc4) { if (length < 8) { break; } int bits = file.ReadByte(); uint height = Binary.BigEndian(file.ReadUInt16()); uint width = Binary.BigEndian(file.ReadUInt16()); int components = file.ReadByte(); return(new ImageMetaData { Width = width, Height = height, BPP = bits * components, }); } file.Seek(length - 2, SeekOrigin.Current); } return(null); }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { var meta = (MfgMetaData)info; file.Position = 0x14; if ('_' != meta.Type) { for (uint i = 0; i < meta.Height; ++i) { uint n = file.ReadUInt32(); file.Seek(n * 8, SeekOrigin.Current); } } byte[] pixels = new byte[meta.Stride * info.Height]; if (pixels.Length != file.Read(pixels, 0, pixels.Length)) { throw new InvalidFormatException("Unexpected end of file"); } PixelFormat format; if (24 == meta.BPP) { format = PixelFormats.Bgr24; } else { format = PixelFormats.Bgra32; } return(ImageData.Create(info, format, null, pixels, meta.Stride)); }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { var meta = (GgdMetaData)info; file.Position = meta.HeaderSize + 4; var palette_data = new byte[0x400]; if (palette_data.Length != file.Read(palette_data, 0, palette_data.Length)) { throw new InvalidFormatException(); } var colors = new Color[256]; for (int i = 0; i < 256; ++i) { colors[i] = Color.FromRgb(palette_data[i * 4 + 2], palette_data[i * 4 + 1], palette_data[i * 4]); } file.Seek(4, SeekOrigin.Current); int input_size = (int)(file.Length - file.Position); using (var reader = new LzssReader(file.AsStream, input_size, (int)meta.BitmapSize)) { reader.Unpack(); var palette = new BitmapPalette(colors); int stride = ((int)meta.Width + 3) & ~3; return(ImageData.Create(info, PixelFormats.Indexed8, palette, reader.Data, stride)); } }
public override ImageMetaData ReadMetaData(IBinaryStream file) { if (file.Signature != CRioArchive.ObjectSignature) { return(null); } var rio = new CRioArchive(file); uint signature; var class_ref = rio.LoadRioTypeCore(out signature); uint object_pos = (uint)file.Position; if ("CS5i" != class_ref) { return(null); } file.Seek(8, SeekOrigin.Current); return(new S5iMetaData { Width = file.ReadUInt16(), Height = file.ReadUInt16(), BPP = 32, ObjectOffset = object_pos, Schema = rio.Schema, }); }
/// <summary> /// Reads an instance from stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="position">The position.</param> /// <returns>Instance of the header.</returns> public static unsafe BinaryObjectHeader Read(IBinaryStream stream, int position) { Debug.Assert(stream != null); Debug.Assert(position >= 0); stream.Seek(position, SeekOrigin.Begin); BinaryObjectHeader hdr; if (BitConverter.IsLittleEndian) { stream.Read((byte *)&hdr, Size); Debug.Assert(hdr.Version == BinaryUtils.ProtoVer); Debug.Assert(hdr.SchemaOffset <= hdr.Length); Debug.Assert(hdr.SchemaOffset >= Size || !hdr.HasSchema); } else { hdr = new BinaryObjectHeader(stream); } // Only one of the flags can be set var f = hdr.Flags; Debug.Assert((f & (Flag.OffsetOneByte | Flag.OffsetTwoBytes)) != (Flag.OffsetOneByte | Flag.OffsetTwoBytes)); return(hdr); }
} // 'OGV' public override SoundInput TryOpen(IBinaryStream file) { file.Position = 0xc; var header = new byte[8]; if (8 != file.Read(header, 0, 8)) { return(null); } if (!Binary.AsciiEqual(header, 0, "fmt ")) { return(null); } uint offset = LittleEndian.ToUInt32(header, 4); file.Seek(offset, SeekOrigin.Current); if (8 != file.Read(header, 0, 8)) { return(null); } if (!Binary.AsciiEqual(header, 0, "data")) { return(null); } var input = new StreamRegion(file.AsStream, file.Position); return(new OggInput(input)); // input is left undisposed in case of exception. }
} // 'GYBP' public override ImageMetaData ReadMetaData(IBinaryStream file) { var header = file.ReadHeader(0x14); file.Seek(-0x13, SeekOrigin.End); var key = file.ReadBytes(0x13); for (int i = 4; i < 0x14; i += 2) { header[i] ^= key[0x10]; header[i + 1] ^= key[0x11]; } for (int i = 0; i < 0x10; ++i) { header[i + 4] -= key[i]; } return(new GbpMetaData { Width = header.ToUInt16(0xE), Height = header.ToUInt16(0x10), BPP = header.ToUInt16(0x12), HeaderSize = header.ToInt32(4), DataOffset = header.ToInt32(8), Method = header.ToUInt16(0xC), }); // 0x14 -> 32-bit checksum after encryption }
List <Entry> ReadIndexV1(IBinaryStream file) { var max_offset = file.Length; file.Position = 8; var key_gen = new KeyGenerator(0xDEADCAFE); var dir = new List <Entry>(); while (file.PeekByte() != -1) { uint name_length = file.ReadUInt32() ^ key_gen.GetNext(); var name_bytes = file.ReadBytes((int)name_length); var name = DecryptName(name_bytes, key_gen); var entry = FormatCatalog.Instance.Create <RgssEntry> (name); entry.Size = file.ReadUInt32() ^ key_gen.GetNext(); entry.Offset = file.Position; entry.Key = key_gen.Current; if (!entry.CheckPlacement(max_offset)) { return(null); } dir.Add(entry); file.Seek(entry.Size, SeekOrigin.Current); } return(dir); }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { file.Seek(8, SeekOrigin.Current); using (var zstream = new ZLibStream(file.AsStream, CompressionMode.Decompress, true)) using (var input = new SeekableStream(zstream)) using (var bmp = new BinaryStream(input, file.Name)) return(base.Read(bmp, info)); }
public override ImageMetaData ReadMetaData(IBinaryStream stream) { int name_count = stream.ReadInt16(); if (name_count <= 0 || name_count > 1000) { return(null); } for (int i = 0; i < name_count; ++i) { int name_length = stream.ReadInt32(); if (name_length <= 0 || name_length > 260) { return(null); } stream.Seek(name_length, SeekOrigin.Current); } int tile_count = stream.ReadInt16(); if (tile_count <= 0 || tile_count > 1000) { return(null); } var rect = new Rectangle(0, 0, 0, 0); for (int i = 0; i < tile_count; ++i) { int name_length = stream.ReadInt32(); if (name_length <= 0 || name_length > 260) { return(null); } stream.Seek(name_length + 0xC, SeekOrigin.Current); int x = stream.ReadInt32(); int y = stream.ReadInt32(); int w = stream.ReadInt32() - x; int h = stream.ReadInt32() - y; var part_rect = new Rectangle(x, y, w, h); rect = Rectangle.Union(rect, part_rect); stream.Seek(0x28, SeekOrigin.Current); } return(ReadCompressionMetaData(stream, rect)); }
/** <inheritdoc /> */ int IBinaryEqualityComparer.GetHashCode(IBinaryStream stream, int startPos, int length, BinaryObjectSchemaHolder schema, int schemaId, Marshaller marshaller, IBinaryTypeDescriptor desc) { Debug.Assert(stream != null); Debug.Assert(startPos >= 0); Debug.Assert(length >= 0); Debug.Assert(schema != null); Debug.Assert(marshaller != null); Debug.Assert(desc != null); Validate(); stream.Flush(); // Preserve stream position. var pos = stream.Position; var reader = marshaller.StartUnmarshal(stream, BinaryMode.ForceBinary); var fields = schema.GetFullSchema(schemaId); int hash = 0; foreach (var fieldName in FieldNames) { int fieldId = BinaryUtils.FieldId(desc.TypeId, fieldName, desc.NameMapper, desc.IdMapper); int fieldHash = 0; // Null (missing) field hash code is 0. int fieldPos; if (fields.TryGetValue(fieldId, out fieldPos)) { stream.Seek(startPos + fieldPos - BinaryObjectHeader.Size, SeekOrigin.Begin); var fieldVal = reader.Deserialize <object>(); fieldHash = fieldVal != null?fieldVal.GetHashCode() : 0; } hash = 31 * hash + fieldHash; } // Restore stream position. stream.Seek(pos, SeekOrigin.Begin); return(hash); }
/// <summary> /// Unmarshals the value, throwing an exception for nulls. /// </summary> private T UnmarshalNotNull <T>(IBinaryStream stream) { var hdr = stream.ReadByte(); if (hdr == BinaryUtils.HdrNull) { throw GetKeyNotFoundException(); } stream.Seek(-1, SeekOrigin.Current); return(_marsh.Unmarshal <T>(stream, _keepBinary)); }
/// <summary> /// Gets the raw offset of this object in specified stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="position">The position.</param> /// <returns>Raw offset.</returns> public int GetRawOffset(IBinaryStream stream, int position) { Debug.Assert(stream != null); if (!HasRaw || !HasSchema) { return(SchemaOffset); } stream.Seek(position + Length - 4, SeekOrigin.Begin); return(stream.ReadInt()); }
/// <summary> /// Unmarshals the value, wrapping in a cache result. /// </summary> private CacheResult <T> UnmarshalCacheResult <T>(IBinaryStream stream) { var hdr = stream.ReadByte(); if (hdr == BinaryUtils.HdrNull) { return(new CacheResult <T>()); } stream.Seek(-1, SeekOrigin.Current); return(new CacheResult <T>(_marsh.Unmarshal <T>(stream, _keepBinary))); }
private void Unpack() { m_input.Position = 0; if (m_colors > 0) { Palette = ImageFormat.ReadPalette(m_input.AsStream, m_colors); } if (m_extra > 0) { m_input.Seek(10 * m_extra + 2, SeekOrigin.Current); } using (var lzss = new LzssStream(m_input.AsStream, LzssMode.Decompress, true)) lzss.Read(m_output, 0, m_output.Length); }
/// <summary> /// Gets the raw offset of this object in specified stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="position">The binary object position in the stream.</param> /// <returns>Raw offset.</returns> public int GetRawOffset(IBinaryStream stream, int position) { Debug.Assert(stream != null); // Either schema or raw is not present - offset is in the header. if (!HasRaw || !HasSchema) { return(SchemaOffset); } // Both schema and raw data are present: raw offset is in the last 4 bytes. stream.Seek(position + Length - 4, SeekOrigin.Begin); return(stream.ReadInt()); }
private ImageData Unpack() { m_input.Position = 0; if (m_colors > 0) { Palette = ImageFormat.ReadPalette(m_input.AsStream, m_colors); } if (m_extra > 0) { m_input.Seek(10 * m_extra + 2, SeekOrigin.Current); } using (var lzss = new LzssStream(m_input.AsStream, LzssMode.Decompress, true)) lzss.Read(m_output, 0, m_output.Length); return(ImageData.CreateFlipped(Info, Format, Palette, m_output, Stride)); }
protected byte[] ReadDataBytes() { uint data_pos = m_input.ReadUInt32(); long ctl_pos = m_input.Position; m_input.Seek(data_pos, SeekOrigin.Current); var data = m_input.ReadBytes((int)(m_input.Length - data_pos)); if (0 == data.Length) { throw new InvalidFormatException(); } m_input.Position = ctl_pos; return(data); }
/// <summary> /// Reads the schema according to this header data. /// </summary> /// <param name="stream">The stream.</param> /// <param name="position">The position.</param> /// <param name="hdr">The header.</param> /// <param name="fieldIdsFunc">The field ids function.</param> /// <returns> /// Schema. /// </returns> public static BinaryObjectSchemaField[] ReadSchema(IBinaryStream stream, int position, BinaryObjectHeader hdr, Func<int[]> fieldIdsFunc) { Debug.Assert(stream != null); Debug.Assert(fieldIdsFunc != null); var schemaSize = hdr.SchemaFieldCount; if (schemaSize == 0) return null; stream.Seek(position + hdr.SchemaOffset, SeekOrigin.Begin); var res = new BinaryObjectSchemaField[schemaSize]; var offsetSize = hdr.SchemaFieldOffsetSize; if (hdr.IsCompactFooter) { var fieldIds = fieldIdsFunc(); Debug.Assert(fieldIds.Length == schemaSize); if (offsetSize == 1) { for (var i = 0; i < schemaSize; i++) res[i] = new BinaryObjectSchemaField(fieldIds[i], stream.ReadByte()); } else if (offsetSize == 2) { for (var i = 0; i < schemaSize; i++) res[i] = new BinaryObjectSchemaField(fieldIds[i], stream.ReadShort()); } else { for (var i = 0; i < schemaSize; i++) res[i] = new BinaryObjectSchemaField(fieldIds[i], stream.ReadInt()); } } else { if (offsetSize == 1) { for (var i = 0; i < schemaSize; i++) res[i] = new BinaryObjectSchemaField(stream.ReadInt(), stream.ReadByte()); } else if (offsetSize == 2) { for (var i = 0; i < schemaSize; i++) res[i] = new BinaryObjectSchemaField(stream.ReadInt(), stream.ReadShort()); } else { for (var i = 0; i < schemaSize; i++) res[i] = new BinaryObjectSchemaField(stream.ReadInt(), stream.ReadInt()); } } return res; }
/// <summary> /// Invokes a store operation. /// </summary> /// <param name="stream">Input stream.</param> /// <param name="grid">Grid.</param> /// <returns>Invocation result.</returns> /// <exception cref="IgniteException">Invalid operation type: + opType</exception> public int Invoke(IBinaryStream stream, Ignite grid) { IBinaryReader reader = grid.Marshaller.StartUnmarshal(stream, _convertBinary ? BinaryMode.Deserialize : BinaryMode.ForceBinary); IBinaryRawReader rawReader = reader.GetRawReader(); int opType = rawReader.ReadByte(); // Setup cache session for this invocation. long sesId = rawReader.ReadLong(); CacheStoreSession ses = grid.HandleRegistry.Get<CacheStoreSession>(sesId, true); ses.CacheName = rawReader.ReadString(); _sesProxy.SetSession(ses); try { // Perform operation. switch (opType) { case OpLoadCache: { var args = rawReader.ReadArray<object>(); stream.Seek(0, SeekOrigin.Begin); int cnt = 0; stream.WriteInt(cnt); // Reserve space for count. var writer = grid.Marshaller.StartMarshal(stream); _store.LoadCache((k, v) => { lock (writer) // User-defined store can be multithreaded. { writer.WithDetach(w => { w.WriteObject(k); w.WriteObject(v); }); cnt++; } }, args); stream.WriteInt(0, cnt); grid.Marshaller.FinishMarshal(writer); break; } case OpLoad: { var val = _store.Load(rawReader.ReadObject<object>()); stream.Seek(0, SeekOrigin.Begin); var writer = grid.Marshaller.StartMarshal(stream); writer.WriteObject(val); grid.Marshaller.FinishMarshal(writer); break; } case OpLoadAll: { var keys = rawReader.ReadCollection(); var result = _store.LoadAll(keys); stream.Seek(0, SeekOrigin.Begin); stream.WriteInt(result.Count); var writer = grid.Marshaller.StartMarshal(stream); foreach (DictionaryEntry entry in result) { var entry0 = entry; // Copy modified closure. writer.WithDetach(w => { w.WriteObject(entry0.Key); w.WriteObject(entry0.Value); }); } grid.Marshaller.FinishMarshal(writer); break; } case OpPut: _store.Write(rawReader.ReadObject<object>(), rawReader.ReadObject<object>()); break; case OpPutAll: var size = rawReader.ReadInt(); var dict = new Hashtable(size); for (int i = 0; i < size; i++) dict[rawReader.ReadObject<object>()] = rawReader.ReadObject<object>(); _store.WriteAll(dict); break; case OpRmv: _store.Delete(rawReader.ReadObject<object>()); break; case OpRmvAll: _store.DeleteAll(rawReader.ReadCollection()); break; case OpSesEnd: grid.HandleRegistry.Release(sesId); _store.SessionEnd(rawReader.ReadBoolean()); break; default: throw new IgniteException("Invalid operation type: " + opType); } return 0; } finally { _sesProxy.ClearSession(); } }
/// <summary> /// Tests the stream. /// </summary> private static unsafe void TestStream(IBinaryStream stream, bool sameArr, Action flush) { Action seek = () => Assert.AreEqual(0, stream.Seek(0, SeekOrigin.Begin)); Action<Action, Func<object>, object> check = (write, read, expectedResult) => { seek(); write(); flush(); seek(); Assert.AreEqual(expectedResult, read()); }; // Arrays. Assert.AreEqual(sameArr, stream.IsSameArray(stream.GetArray())); Assert.IsFalse(stream.IsSameArray(new byte[1])); Assert.IsFalse(stream.IsSameArray(stream.GetArrayCopy())); // byte* byte* bytes = stackalloc byte[10]; *bytes = 1; *(bytes + 1) = 2; stream.Write(bytes, 2); Assert.AreEqual(2, stream.Position); flush(); seek(); Assert.AreEqual(sameArr ? 256 : 2, stream.Remaining); byte* bytes2 = stackalloc byte[2]; stream.Read(bytes2, 2); Assert.AreEqual(1, *bytes2); Assert.AreEqual(2, *(bytes2 + 1)); // char* seek(); char* chars = stackalloc char[10]; *chars = 'a'; *(chars + 1) = 'b'; Assert.AreEqual(2, stream.WriteString(chars, 2, 2, Encoding.ASCII)); flush(); seek(); stream.Read(bytes2, 2); Assert.AreEqual('a', *bytes2); Assert.AreEqual('b', *(bytes2 + 1)); // Others. check(() => stream.Write(new byte[] {3, 4, 5}, 1, 2), () => stream.ReadByteArray(2), new byte[] {4, 5}); check(() => stream.WriteBool(true), () => stream.ReadBool(), true); check(() => stream.WriteBoolArray(new[] {true, false}), () => stream.ReadBoolArray(2), new[] {true, false}); check(() => stream.WriteByte(4), () => stream.ReadByte(), 4); check(() => stream.WriteByteArray(new byte[] {4, 5, 6}), () => stream.ReadByteArray(3), new byte[] {4, 5, 6}); check(() => stream.WriteChar('x'), () => stream.ReadChar(), 'x'); check(() => stream.WriteCharArray(new[] {'a', 'b'}), () => stream.ReadCharArray(2), new[] {'a', 'b'}); check(() => stream.WriteDouble(4), () => stream.ReadDouble(), 4d); check(() => stream.WriteDoubleArray(new[] {4d}), () => stream.ReadDoubleArray(1), new[] {4d}); check(() => stream.WriteFloat(4), () => stream.ReadFloat(), 4f); check(() => stream.WriteFloatArray(new[] {4f}), () => stream.ReadFloatArray(1), new[] {4f}); check(() => stream.WriteInt(4), () => stream.ReadInt(), 4); check(() => stream.WriteInt(0, 4), () => stream.ReadInt(), 4); check(() => stream.WriteIntArray(new[] {4}), () => stream.ReadIntArray(1), new[] {4}); check(() => stream.WriteLong(4), () => stream.ReadLong(), 4L); check(() => stream.WriteLongArray(new[] {4L}), () => stream.ReadLongArray(1), new[] {4L}); check(() => stream.WriteShort(4), () => stream.ReadShort(), (short)4); check(() => stream.WriteShortArray(new short[] {4}), () => stream.ReadShortArray(1), new short[] {4}); }