本文整理汇总了C#中Stream.ReadToMemoryStream方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.ReadToMemoryStream方法的具体用法?C# Stream.ReadToMemoryStream怎么用?C# Stream.ReadToMemoryStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stream
的用法示例。
在下文中一共展示了Stream.ReadToMemoryStream方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decompress
public static MemoryStream Decompress(Stream input)
{
var basePosition = input.Position;
if (input.ReadValueU32(false) != Magic) // CDRM
{
throw new FormatException();
}
var version = input.ReadValueU32(true);
if (version != 0 &&
version != 2 && version.Swap() != 2)
{
throw new FormatException();
}
bool littleEndian;
uint count;
uint padding;
if (version == 0)
{
count = input.ReadValueU32(true);
if (count > 0x7FFFFF)
{
count = count.Swap();
littleEndian = false;
}
else
{
littleEndian = true;
}
input.ReadValueU32(littleEndian);
padding = (uint)(basePosition + 16 + (count * 8));
padding = padding.Align(16) - padding;
}
else
{
littleEndian = version == 2;
count = input.ReadValueU32(littleEndian);
padding = input.ReadValueU32(littleEndian);
}
var startOfData = basePosition + 16 + (count * 8) + padding;
var blocks = new Block[count];
using (var buffer = input.ReadToMemoryStream((count * 8).Align(16)))
{
for (uint i = 0; i < count; i++)
{
var block = new Block();
var flags = buffer.ReadValueU32(littleEndian);
block.UncompressedSize = (flags >> 8) & 0xFFFFFF;
block.Type = (byte)(flags & 0xFF);
block.CompressedSize = buffer.ReadValueU32(littleEndian);
blocks[i] = block;
}
}
if (startOfData != input.Position)
{
throw new InvalidOperationException();
}
var output = new MemoryStream();
long offset = 0;
foreach (var block in blocks)
{
var nextPosition = input.Position + block.CompressedSize.Align(16);
using (var buffer = input.ReadToMemoryStream(block.CompressedSize))
{
if (block.Type == 1)
{
if (block.CompressedSize != block.UncompressedSize)
{
throw new InvalidOperationException();
}
output.Seek(offset, SeekOrigin.Begin);
output.WriteFromStream(buffer, block.CompressedSize);
offset += block.CompressedSize.Align(16);
}
else if (block.Type == 2)
{
var zlib = new InflaterInputStream(buffer);
output.Seek(offset, SeekOrigin.Begin);
output.WriteFromStream(zlib, block.UncompressedSize);
offset += block.UncompressedSize.Align(16);
}
else
{
throw new FormatException();
}
}
//.........这里部分代码省略.........
示例2: Deserialize
public void Deserialize(Stream input)
{
var magic = input.ReadValueU32(Endian.Little);
if (magic != 0x666D726D && // fmrm
magic.Swap() != 0x666D726D)
{
throw new FormatException();
}
var endian = magic == 0x666D726D ? Endian.Little : Endian.Big;
var version = input.ReadValueU32(endian);
if (version != 1)
{
throw new FormatException();
}
this.Version = version;
/*var maxKeyLength =*/ input.ReadValueS32(endian);
var maxValueLength = input.ReadValueS32(endian);
var stringTableSize = input.ReadValueU32(endian);
var huffmanSize = input.ReadValueU32(endian);
var indexSize = input.ReadValueU32(endian);
var dataSize = input.ReadValueU32(endian);
var strings = new List<KeyValuePair<uint, string>>();
using (var data = input.ReadToMemoryStream(stringTableSize))
{
var localStringTableSize = data.ReadValueU32(endian);
if (localStringTableSize != stringTableSize)
{
throw new FormatException();
}
var count = data.ReadValueU32(endian);
var offsets = new List<KeyValuePair<uint, uint>>();
for (uint i = 0; i < count; i++)
{
var hash = data.ReadValueU32(endian);
var offset = data.ReadValueU32(endian);
offsets.Add(new KeyValuePair<uint, uint>(hash, offset));
}
foreach (var kv in offsets)
{
var hash = kv.Key;
var offset = kv.Value;
data.Seek(8 + offset, SeekOrigin.Begin);
var length = data.ReadValueU16(endian);
var text = data.ReadString(length, Encoding.UTF8);
if (text.HashCrc32() != hash)
{
throw new InvalidOperationException();
}
strings.Add(new KeyValuePair<uint, string>(hash, text));
}
}
Huffman.Pair[] huffmanTree;
using (var data = input.ReadToMemoryStream(huffmanSize))
{
var count = data.ReadValueU16(endian);
huffmanTree = new Huffman.Pair[count];
for (ushort i = 0; i < count; i++)
{
var left = data.ReadValueS32(endian);
var right = data.ReadValueS32(endian);
huffmanTree[i] = new Huffman.Pair(left, right);
}
}
using (var index = input.ReadToMemoryStream(indexSize))
{
var totalBits = input.ReadValueS32(endian);
var data = input.ReadBytes(dataSize);
var bitArray = new BitArray(data) { Length = totalBits };
var files = new List<KeyValuePair<string, uint>>();
var fileCount = index.ReadValueU16(endian);
for (ushort i = 0; i < fileCount; i++)
{
var nameIndex = index.ReadValueU16(endian);
var name = strings[nameIndex].Value;
var offset = index.ReadValueU32(endian);
files.Add(new KeyValuePair<string, uint>(name, offset));
}
foreach (var fileInfo in files.OrderBy(f => f.Key))
{
var file = new Coalesced.File() { Name = fileInfo.Key };
index.Seek(fileInfo.Value, SeekOrigin.Begin);
var sectionCount = index.ReadValueU16(endian);
var sections = new List<KeyValuePair<string, uint>>();
for (ushort i = 0; i < sectionCount; i++)
{
//.........这里部分代码省略.........
示例3: Deserialize
public void Deserialize(Stream input)
{
if (input.ReadValueU32() != 0x39444350)
{
throw new FormatException();
}
this.Format = input.ReadValueEnum<PCD9.Format>();
var dataSize = input.ReadValueU32();
this.Unknown0C = input.ReadValueU32();
this.Width = input.ReadValueU16();
this.Height = input.ReadValueU16();
this.BPP = input.ReadValueU8();
var mipMapCount = 1 + input.ReadValueU8();
this.Unknown16 = input.ReadValueU16();
if ((this.Unknown16 & 0x8000) != 0)
{
throw new NotSupportedException();
this.unknownFlag = true;
}
this.Mipmaps.Clear();
using (var data = input.ReadToMemoryStream(dataSize))
{
var mipWidth = this.Width;
var mipHeight = this.Height;
for (int i = 0; i < mipMapCount; i++)
{
if (mipWidth == 0)
{
mipWidth = 1;
}
if (mipHeight == 0)
{
mipHeight = 1;
}
int size;
switch (this.Format)
{
case PCD9.Format.A8R8G8B8:
{
size = mipWidth * mipHeight * 4;
break;
}
case PCD9.Format.DXT1:
case PCD9.Format.DXT3:
case PCD9.Format.DXT5:
{
int blockCount = ((mipWidth + 3) / 4) * ((mipHeight + 3) / 4);
int blockSize = this.Format == PCD9.Format.DXT1 ? 8 : 16;
size = blockCount * blockSize;
break;
}
default:
{
throw new NotSupportedException();
}
}
var buffer = new byte[size];
if (data.Read(buffer, 0, buffer.Length) != buffer.Length)
{
throw new EndOfStreamException();
}
this.Mipmaps.Add(new PCD9.Mipmap()
{
Width = mipWidth,
Height = mipHeight,
Data = buffer,
});
mipWidth >>= 1;
mipHeight >>= 1;
}
if (data.Position != data.Length)
{
throw new InvalidOperationException();
}
}
}
示例4: Deserialize
public static SaveFile Deserialize(Stream input, DeserializeSettings settings)
{
if (input.Position + 20 > input.Length)
{
throw new SaveCorruptionException("not enough data for save header");
}
var check = input.ReadValueU32(Endian.Big);
if (check == 0x434F4E20)
{
throw new SaveFormatException("Xbox 360 save game loading is in the works");
}
input.Seek(-4, SeekOrigin.Current);
var readSha1Hash = input.ReadBytes(20);
using (var data = input.ReadToMemoryStream(input.Length - 20))
{
byte[] computedSha1Hash;
using (var sha1 = new System.Security.Cryptography.SHA1Managed())
{
computedSha1Hash = sha1.ComputeHash(data);
}
if ((settings & DeserializeSettings.IgnoreSha1Mismatch) == 0 &&
readSha1Hash.SequenceEqual(computedSha1Hash) == false)
{
throw new SaveCorruptionException("invalid SHA1 hash");
}
data.Position = 0;
var uncompressedSize = data.ReadValueU32(Endian.Big);
var uncompressedBytes = new byte[uncompressedSize];
if (uncompressedSize <= BlockSize)
{
var actualUncompressedSize = (int)uncompressedSize;
var compressedSize = (uint)(data.Length - 4);
var compressedBytes = data.ReadBytes(compressedSize);
var result = LZO.Decompress(compressedBytes,
0,
(int)compressedSize,
uncompressedBytes,
0,
ref actualUncompressedSize);
if (result != LZO.ErrorCode.Success)
{
throw new SaveCorruptionException(string.Format("LZO decompression failure ({0})", result));
}
if (actualUncompressedSize != (int)uncompressedSize)
{
throw new SaveCorruptionException("LZO decompression failure (uncompressed size mismatch)");
}
}
else
{
var blockCount = data.ReadValueU32(Endian.Big);
var blockInfos = new List<Tuple<uint, uint>>();
for (uint i = 0; i < blockCount; i++)
{
var blockCompressedSize = data.ReadValueU32(Endian.Big);
var blockUncompressedSize = data.ReadValueU32(Endian.Big);
blockInfos.Add(new Tuple<uint, uint>(blockCompressedSize, blockUncompressedSize));
}
int uncompressedOffset = 0;
int uncompressedSizeLeft = (int)uncompressedSize;
foreach (var blockInfo in blockInfos)
{
var blockUncompressedSize = Math.Min((int)blockInfo.Item2, uncompressedSizeLeft);
var actualUncompressedSize = blockUncompressedSize;
var compressedSize = (int)blockInfo.Item1;
var compressedBytes = data.ReadBytes(compressedSize);
var result = LZO.Decompress(compressedBytes,
0,
compressedSize,
uncompressedBytes,
uncompressedOffset,
ref actualUncompressedSize);
if (result != LZO.ErrorCode.Success)
{
throw new SaveCorruptionException(string.Format("LZO decompression failure ({0})", result));
}
if (actualUncompressedSize != blockUncompressedSize)
{
throw new SaveCorruptionException("LZO decompression failure (uncompressed size mismatch)");
}
uncompressedOffset += blockUncompressedSize;
uncompressedSizeLeft -= blockUncompressedSize;
}
if (uncompressedSizeLeft != 0)
{
throw new SaveCorruptionException("LZO decompression failure (uncompressed size left != 0)");
}
}
//.........这里部分代码省略.........
示例5: Deserialize
public void Deserialize(Stream input)
{
var magic = input.ReadValueU32(false);
input.Seek(-4, SeekOrigin.Current);
if (magic == CDRMFile.Magic)
{
input = CDRMFile.Decompress(input);
}
if (input.Position + 32 > input.Length)
{
throw new FormatException("not enough data for header");
}
var version = input.ReadValueU32();
if (version != 19 && version.Swap() != 19 &&
version != 21 && version.Swap() != 21)
{
throw new FormatException();
}
this.LittleEndian =
version == 19 ||
version == 21;
this.Version = this.LittleEndian == true ? version : version.Swap();
if (this.Version == 19)
{
throw new NotSupportedException();
}
var unknown04_Size = input.ReadValueU32(this.LittleEndian);
var unknown08_Size = input.ReadValueU32(this.LittleEndian);
var unknown0C = input.ReadValueU32(this.LittleEndian); // extra data after first block?
var unknown10 = input.ReadValueU32(this.LittleEndian);
this.Flags = input.ReadValueU32(this.LittleEndian);
var sectionCount = input.ReadValueU32(this.LittleEndian);
var unknown1C_Count = input.ReadValueU32(this.LittleEndian);
if (unknown0C != 0)
{
throw new FormatException();
if ((this.Flags & 1) != 0)
{
input.Seek(input.Position.Align(16), SeekOrigin.Begin);
}
}
var sectionHeaders = new DRM.SectionHeader[sectionCount];
for (uint i = 0; i < sectionCount; i++)
{
sectionHeaders[i] = new DRM.SectionHeader();
sectionHeaders[i].Deserialize(input, this.LittleEndian);
}
this.Unknown08s.Clear();
using (var unknown08_Data = input.ReadToMemoryStream(unknown08_Size))
{
while (unknown08_Data.Position < unknown08_Data.Length)
{
this.Unknown08s.Add(unknown08_Data.ReadStringZ(Encoding.ASCII));
}
}
this.Unknown04s.Clear();
using (var unknown04_Data = input.ReadToMemoryStream(unknown04_Size))
{
while (unknown04_Data.Position < unknown04_Data.Length)
{
this.Unknown04s.Add(unknown04_Data.ReadStringZ(Encoding.ASCII));
}
}
if ((this.Flags & 1) != 0)
{
input.Seek(input.Position.Align(16), SeekOrigin.Begin);
}
var sections = new DRM.Section[sectionCount];
for (int i = 0; i < sectionCount; i++)
{
var sectionHeader = sectionHeaders[i];
var section = new DRM.Section();
section.Id = sectionHeader.Id;
section.Type = sectionHeader.Type;
section.Flags = (byte)(sectionHeader.Flags & 0xFF);
section.Unknown05 = sectionHeader.Unknown05;
section.Unknown06 = sectionHeader.Unknown06;
section.Unknown10 = sectionHeader.Unknown10;
if ((sectionHeader.Unknown05 & 1) != 0)
{
throw new NotImplementedException();
}
if (sectionHeader.HeaderSize > 0)
{
//.........这里部分代码省略.........
示例6: Deserialize
//.........这里部分代码省略.........
if (unknown1C != 0xFFFFFFFF)
{
throw new InvalidOperationException();
}
this.Flags = (flags & 0x1FFFFF0F) >> 0;
this.Encryption = (EncryptionScheme)((flags & 0x000000F0) >> 4);
this.Compression = (CompressionScheme)((flags & 0xE0000000) >> 29);
if (this.Flags != 0 && this.Flags != 1)
{
throw new FormatException("unknown flags value");
}
this.ContentId = contentId;
this.PasswordDigest = passwordDigest;
this.Entries.Clear();
for (uint i = 0; i < fileCount; i++)
{
var entry = new Entry();
entry.Name = input.ReadString(64, true, Encoding.Unicode);
entry.CalculateHashes();
entry.Offset = input.ReadValueU32();
entry.CompressedSize = input.ReadValueU32();
entry.UncompressedSize = input.ReadValueU32();
this.Entries.Add(entry);
}
}
else if (version1 == 0x4500520046002000 &&
version2 == 0x560033002E003000) // ERF V3.0
{
input.Seek(basePosition + 16, SeekOrigin.Begin);
this.Version = 3;
var stringTableSize = input.ReadValueU32();
var fileCount = input.ReadValueU32();
var flags = input.ReadValueU32();
var contentId = input.ReadValueU32();
var passwordDigest = new byte[16];
input.Read(passwordDigest, 0, passwordDigest.Length);
this.Flags = (flags & 0x1FFFFF0F) >> 0;
this.Encryption = (EncryptionScheme)((flags & 0x000000F0) >> 4);
this.Compression = (CompressionScheme)((flags & 0xE0000000) >> 29);
if (this.Flags != 0 && this.Flags != 1)
{
throw new FormatException("unknown flags value");
}
this.ContentId = contentId;
this.PasswordDigest = passwordDigest;
MemoryStream stringTable = stringTableSize == 0 ?
null : input.ReadToMemoryStream(stringTableSize);
this.Entries.Clear();
for (uint i = 0; i < fileCount; i++)
{
var entry = new Entry();
uint nameOffset = input.ReadValueU32();
entry.NameHash = input.ReadValueU64();
if (nameOffset != 0xFFFFFFFF)
{
if (nameOffset + 1 > stringTable.Length)
{
throw new FormatException("file name exceeds string table bounds");
}
stringTable.Position = nameOffset;
entry.Name = stringTable.ReadStringZ(Encoding.ASCII);
if (entry.Name.HashFNV64() != entry.NameHash)
{
throw new InvalidOperationException("hash mismatch");
}
}
else
{
entry.Name = null;
}
entry.TypeHash = input.ReadValueU32();
entry.Offset = input.ReadValueU32();
entry.CompressedSize = input.ReadValueU32();
entry.UncompressedSize = input.ReadValueU32();
this.Entries.Add(entry);
}
}
else
{
throw new FormatException("unsupported / unknown ERF format");
}
}
示例7: Deserialize
//.........这里部分代码省略.........
{
throw new FormatException();
}
var infos = new Sprite.FrameInfo[header.FrameCount];
for (int i = 0; i < infos.Length; i++)
{
infos[i] = input.ReadStructure<Sprite.FrameInfo>();
}
var compressionFlags = (uint)header.CompressionFlags;
if ((compressionFlags & ~0x1FFu) != 0)
{
throw new FormatException("unknown compression flags");
}
if (header.Unknown20 != 0 &&
header.Unknown20 != 3)
{
// WHAT DOES THIS VALUE MEAN AUGH
throw new NotSupportedException();
}
if ((header.CompressionFlags &
Sprite.CompressionFlags.NoCompression) != 0)
{
if ((header.CompressionFlags &
~(Sprite.CompressionFlags.NoPixels | Sprite.CompressionFlags.NoCompression)) != 0)
{
throw new FormatException("other compression flags set with NoCompression flag");
}
}
using (var data = input.ReadToMemoryStream(header.DataSize))
{
this.Frames = new Sprite.Frame[header.FrameCount];
for (int i = 0; i < header.FrameCount; i++)
{
var info = infos[i];
data.Seek(info.Offset, SeekOrigin.Begin);
var frame = this.Frames[i] = new Sprite.Frame();
frame.X = info.X;
frame.Y = info.Y;
frame.Width = Math.Abs(info.Width);
frame.Height = Math.Abs(info.Height);
frame.Pixels = new byte[frame.Width * frame.Height];
if ((header.CompressionFlags &
Sprite.CompressionFlags.NoCompression) != 0)
{
// uncompressed data
data.Read(frame.Pixels, 0, frame.Pixels.Length);
}
else
{
// compressed data
var lengths = new int[frame.Height];
var max = 0;
for (int y = 0; y < frame.Height; y++)
{
int length = data.ReadValueU8();
if (length == 0xFF)
{
示例8: Deserialize
public void Deserialize(Stream input)
{
input.Seek(0, SeekOrigin.Begin);
var magic = input.ReadValueU32(false);
if (magic != 0x47464620)
{
throw new FormatException();
}
var version = input.ReadValueU32(false);
if (version != 0x56342E30 && // 4.0
version != 0x56342E31) // 4.1
{
throw new FormatException("unsupported version");
}
this.FileVersion = (byte)(version - 0x56342E30);
this.FilePlatform = input.ReadValueEnum<GFF.FilePlatform>(false);
this.FormatType = input.ReadValueEnum<GFF.FormatType>(false);
this.FormatVersion = input.ReadValueU32(false);
var littleEndian = this.FilePlatform == GFF.FilePlatform.PC;
var structCount = input.ReadValueU32(littleEndian);
var stringCount = this.FileVersion < 1 ? 0 : input.ReadValueU32(littleEndian);
var stringOffset = this.FileVersion < 1 ? 0 : input.ReadValueU32(littleEndian);
var dataOffset = input.ReadValueU32(littleEndian);
if (this.FileVersion < 1)
{
stringOffset = dataOffset;
}
else
{
if (dataOffset < stringOffset)
{
throw new FormatException();
}
}
this.Structures.Clear();
for (uint i = 0; i < structCount; i++)
{
var structDef = new GFF.StructureDefinition();
//structDef.Id = input.ReadValueU32(littleEndian);
structDef.Id = input.ReadValueU32(false);
var fieldCount = input.ReadValueU32(littleEndian);
var fieldOffset = input.ReadValueU32(littleEndian);
structDef.DataSize = input.ReadValueU32(littleEndian);
long nextOffset = input.Position;
structDef.Fields.Clear();
input.Seek(fieldOffset, SeekOrigin.Begin);
for (uint j = 0; j < fieldCount; j++)
{
var fieldDef = new GFF.FieldDefinition();
fieldDef.Id = input.ReadValueS32(littleEndian);
var rawFlags = input.ReadValueU32(littleEndian);
fieldDef.Offset = input.ReadValueU32(littleEndian);
var type = (ushort)(rawFlags & 0xFFFF);
var flags = (GFF.FieldFlags)((rawFlags >> 16) & 0xFFFF);
if ((flags & GFF.FieldFlags.IsStructure) != 0)
{
flags &= ~GFF.FieldFlags.IsStructure;
fieldDef.Type = GFF.FieldType.Structure;
fieldDef.StructureId = type;
}
else
{
fieldDef.Type = (GFF.FieldType)type;
}
fieldDef.Flags = flags;
structDef.Fields.Add(fieldDef);
}
this.Structures.Add(structDef);
input.Seek(nextOffset, SeekOrigin.Begin);
}
if (this.FileVersion >= 1)
{
input.Seek(stringOffset, SeekOrigin.Begin);
this.StringTable = new List<string>();
for (uint i = 0; i < stringCount; i++)
{
this.StringTable.Add(input.ReadStringZ(Encoding.UTF8));
}
}
input.Seek(dataOffset, SeekOrigin.Begin);
this.Data = input.ReadToMemoryStream(input.Length - dataOffset);
}
示例9: Deserialize
public void Deserialize(Stream input)
{
var magic = input.ReadUInt32();
if (magic != 0x666D726D && magic.Swap() != 0x666D726D)
{
throw new FormatException();
}
var endian = magic == 0x666D726D ? ByteOrder.LittleEndian : ByteOrder.BigEndian;
var version = input.ReadUInt32(endian);
if (version != 1)
{
throw new FormatException();
}
Version = version;
input.ReadInt32(endian);
var maxValueLength = input.ReadInt32(endian);
var stringTableSize = input.ReadUInt32(endian);
var huffmanSize = input.ReadUInt32(endian);
var indexSize = input.ReadUInt32(endian);
var dataSize = input.ReadUInt32(endian);
var strings = new List<KeyValuePair<uint, string>>();
using (var data = input.ReadToMemoryStream(stringTableSize))
{
var localStringTableSize = data.ReadUInt32(endian);
if (localStringTableSize != stringTableSize)
{
throw new FormatException();
}
var count = data.ReadUInt32(endian);
var offsets = new List<KeyValuePair<uint, uint>>();
for (uint i = 0; i < count; i++)
{
var hash = data.ReadUInt32(endian);
var offset = data.ReadUInt32(endian);
offsets.Add(new KeyValuePair<uint, uint>(hash, offset));
}
foreach (var kv in offsets)
{
var hash = kv.Key;
var offset = kv.Value;
data.Seek(8 + offset, SeekOrigin.Begin);
var length = data.ReadUInt16(endian);
var text = data.ReadString(length, Encoding.UTF8);
if (text.HashCrc32() != hash)
{
throw new InvalidOperationException();
}
strings.Add(new KeyValuePair<uint, string>(hash, text));
}
}
Pair[] huffmanTree;
using (var data = input.ReadToMemoryStream(huffmanSize))
{
var count = data.ReadUInt16(endian);
huffmanTree = new Pair[count];
for (ushort i = 0; i < count; i++)
{
var left = data.ReadInt32(endian);
var right = data.ReadInt32(endian);
huffmanTree[i] = new Pair(left, right);
}
}
using (var index = input.ReadToMemoryStream(indexSize))
{
var totalBits = input.ReadInt32(endian);
var data = input.ReadBytes(dataSize);
var bitArray = new BitArray(data)
{
Length = totalBits
};
var files = new List<KeyValuePair<string, uint>>();
var fileCount = index.ReadUInt16(endian);
for (ushort i = 0; i < fileCount; i++)
{
var nameIndex = index.ReadUInt16(endian);
var name = strings[nameIndex].Value;
var offset = index.ReadUInt32(endian);
//.........这里部分代码省略.........