本文整理汇总了C#中System.Stream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.Read方法的具体用法?C# Stream.Read怎么用?C# Stream.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Stream
的用法示例。
在下文中一共展示了Stream.Read方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadBytes
public static void ReadBytes(Stream s, byte[] b, int offset, int length)
{
if ((offset == 0) && (length == 0))
length = b.Length;
while (length > 0)
{
int count = s.Read(b, offset, length);
if (count <= 0)
throw new Exception("IOException: EOF");
offset += count;
length -= count;
}
}
示例2: Store
// Copies all source file into storage file
private void Store(ref ZipFileEntry _zfe, Stream _source)
{
byte[] buffer = new byte[16384];
int bytesRead;
uint totalRead = 0;
Stream outStream;
long posStart = this.ZipFileStream.Position;
long sourceStart = _source.Position;
if (_zfe.Method == Compression.Store)
outStream = this.ZipFileStream;
else
outStream = new DeflateStream(this.ZipFileStream, CompressionMode.Compress, true);
_zfe.Crc32 = 0 ^ 0xffffffff;
do
{
bytesRead = _source.Read(buffer, 0, buffer.Length);
totalRead += (uint)bytesRead;
if (bytesRead > 0)
{
outStream.Write(buffer, 0, bytesRead);
for (uint i = 0; i < bytesRead; i++)
{
_zfe.Crc32 = ZipStorer.CrcTable[(_zfe.Crc32 ^ buffer[i]) & 0xFF] ^ (_zfe.Crc32 >> 8);
}
}
} while (bytesRead == buffer.Length);
outStream.Flush();
if (_zfe.Method == Compression.Deflate)
outStream.Dispose();
_zfe.Crc32 ^= 0xffffffff;
_zfe.FileSize = totalRead;
_zfe.CompressedSize = (uint)(this.ZipFileStream.Position - posStart);
// Verify for real compression
if (_zfe.Method == Compression.Deflate && !this.ForceDeflating && _source.CanSeek && _zfe.CompressedSize > _zfe.FileSize)
{
// Start operation again with Store algorithm
_zfe.Method = Compression.Store;
this.ZipFileStream.Position = posStart;
this.ZipFileStream.SetLength(posStart);
_source.Position = sourceStart;
this.Store(ref _zfe, _source);
}
}
示例3: CopyToMemoryStream
private static Stream CopyToMemoryStream(Stream s) {
var size = 100000; // large heap is more efficient
var copyBuff = new byte[size];
int len;
var r = new MemoryStream();
while((len = s.Read(copyBuff, 0, size)) > 0)
r.Write(copyBuff, 0, len);
r.Seek(0, SeekOrigin.Begin);
s.Close();
return r;
}
示例4: HtmlStream
public HtmlStream(Stream stm, Encoding defaultEncoding) {
if(defaultEncoding == null) {
defaultEncoding = Encoding.UTF8; // default is UTF8
}
if(!stm.CanSeek) {
// Need to be able to seek to sniff correctly.
stm = CopyToMemoryStream(stm);
}
this.stm = stm;
rawBuffer = new Byte[BUFSIZE];
rawUsed = stm.Read(rawBuffer, 0, 4); // maximum byte order mark
this.m_buffer = new char[BUFSIZE];
// Check byte order marks
this.m_decoder = AutoDetectEncoding(rawBuffer, ref rawPos, rawUsed);
var bom = rawPos;
if(this.m_decoder == null) {
this.m_decoder = defaultEncoding.GetDecoder();
rawUsed += stm.Read(rawBuffer, 4, BUFSIZE - 4);
DecodeBlock();
// Now sniff to see if there is an XML declaration or HTML <META> tag.
var sd = SniffEncoding();
if(sd != null) {
this.m_decoder = sd;
}
}
// Reset to get ready for Read()
this.stm.Seek(0, SeekOrigin.Begin);
this.pos = this.used = 0;
// skip bom
if(bom > 0) {
stm.Read(this.rawBuffer, 0, bom);
}
this.rawPos = this.rawUsed = 0;
}