本文整理汇总了C#中System.IO.Stream类的典型用法代码示例。如果您正苦于以下问题:C# Stream类的具体用法?C# Stream怎么用?C# Stream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Stream类属于System.IO命名空间,在下文中一共展示了Stream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateText
public SourceText CreateText(Stream stream, Encoding defaultEncoding, CancellationToken cancellationToken = default(CancellationToken))
{
// this API is for a case where user wants us to figure out encoding from the given stream.
// if defaultEncoding is given, we will use it if we couldn't figure out encoding used in the stream ourselves.
Debug.Assert(stream != null);
Debug.Assert(stream.CanSeek);
Debug.Assert(stream.CanRead);
if (defaultEncoding == null)
{
// Try UTF-8
try
{
return CreateTextInternal(stream, s_throwingUtf8Encoding, cancellationToken);
}
catch (DecoderFallbackException)
{
// Try Encoding.Default
defaultEncoding = Encoding.Default;
}
}
try
{
return CreateTextInternal(stream, defaultEncoding, cancellationToken);
}
catch (DecoderFallbackException)
{
return null;
}
}
示例2: Metafile
// Usually called when cloning images that need to have
// not only the handle saved, but also the underlying stream
// (when using MS GDI+ and IStream we must ensure the stream stays alive for all the life of the Image)
internal Metafile (IntPtr ptr, Stream stream)
{
// under Win32 stream is owned by SD/GDI+ code
if (GDIPlus.RunningOnWindows ())
this.stream = stream;
nativeObject = ptr;
}
示例3: Save
public void Save(Stream output)
{
BinaryWriter writer = new BinaryWriter(output);
writer.Write(this.version);
writer.Write((int)0);
writer.Write((int)0);
// Double the string length since it's UTF16
writer.Write((byte)(this.partName.Length * 2));
MadScience.StreamHelpers.WriteStringUTF16(output, false, this.partName);
writer.Write(this.blendType);
this.blendTgi.Save(output);
writer.Write((uint)this.geomBoneEntries.Count);
for (int i = 0; i < this.geomBoneEntries.Count; i++)
{
this.geomBoneEntries[i].Save(output);
}
uint tgiOffset = (uint)output.Position - 8;
// Why is this +12? I dunno. :)
this.keytable.size = 8;
this.keytable.Save(output);
output.Seek(4, SeekOrigin.Begin);
writer.Write(tgiOffset);
writer.Write(this.keytable.size);
writer = null;
}
示例4: SetOutput
protected void SetOutput(Stream stream, bool ownsStream, Encoding encoding)
{
_stream = stream;
_ownsStream = ownsStream;
_offset = 0;
_encoding = encoding;
}
示例5: ComputeHash
/// <summary>
/// Compute a hash from the content of a stream and restore the position.
/// </summary>
/// <remarks>
/// Modified FNV Hash in C#
/// http://stackoverflow.com/a/468084
/// </remarks>
internal static int ComputeHash(Stream stream)
{
System.Diagnostics.Debug.Assert(stream.CanSeek);
unchecked
{
const int p = 16777619;
var hash = (int)2166136261;
var prevPosition = stream.Position;
stream.Position = 0;
var data = new byte[1024];
int length;
while((length = stream.Read(data, 0, data.Length)) != 0)
{
for (var i = 0; i < length; i++)
hash = (hash ^ data[i]) * p;
}
// Restore stream position.
stream.Position = prevPosition;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return hash;
}
}
示例6: ReadProp
public override void ReadProp(Stream input, bool array)
{
this.R = input.ReadF32();
this.G = input.ReadF32();
this.B = input.ReadF32();
this.A = input.ReadF32();
}
示例7: WriteToStream
public static void WriteToStream(Image img, Stream stream)
{
IntPtr point = img.gdImageStructPtr;
DLLImports.gdImageStruct gdImageStruct = Marshal.PtrToStructure<DLLImports.gdImageStruct>(point);
var wrapper = new gdStreamWrapper(stream);
DLLImports.gdImageJpegCtx(ref gdImageStruct, ref wrapper.IOCallbacks);
}
示例8: StreamToXml
public static XmlDocument StreamToXml(Stream stream)
{
var xmlDoc = new XmlDocument();
var reader = new StreamReader(stream);
xmlDoc.LoadXml(reader.ReadToEnd());
return xmlDoc;
}
示例9: StreamToMemory
static MemoryStream StreamToMemory(Stream input)
{
byte[] buffer = new byte[1024];
int count = 1024;
MemoryStream output;
// build a new stream
if (input.CanSeek)
{
output = new MemoryStream((int)input.Length);
}
else
{
output = new MemoryStream();
}
// iterate stream and transfer to memory stream
do
{
count = input.Read(buffer, 0, count);
if (count == 0)
break; // TODO: might not be correct. Was : Exit Do
output.Write(buffer, 0, count);
} while (true);
// rewind stream
output.Position = 0;
// pass back
return output;
}
示例10: LimitedInputStream
internal LimitedInputStream(
Stream inStream,
int limit)
{
this._in = inStream;
this._limit = limit;
}
示例11: Read
public void Read(Stream inputStream)
{
BinaryReader reader = new BinaryReader(inputStream, Encoding.UTF8, true);
int magicNumber = reader.ReadInt32();
int version = reader.ReadInt32(); // GZ 2, TPP 3
int endianess = reader.ReadInt32(); // LE, BE
int entryCount = reader.ReadInt32();
int valuesOffset = reader.ReadInt32();
int keysOffset = reader.ReadInt32();
inputStream.Position = valuesOffset;
Dictionary<int, LangEntry> offsetEntryDictionary = new Dictionary<int, LangEntry>();
for (int i = 0; i < entryCount; i++)
{
int valuePosition = (int)inputStream.Position - valuesOffset;
short valueConstant = reader.ReadInt16();
Debug.Assert(valueConstant == 1);
string value = reader.ReadNullTerminatedString();
offsetEntryDictionary.Add(valuePosition, new LangEntry
{
Value = value
});
}
inputStream.Position = keysOffset;
for (int i = 0; i < entryCount; i++)
{
uint key = reader.ReadUInt32();
int offset = reader.ReadInt32();
offsetEntryDictionary[offset].Key = key;
}
Entries = offsetEntryDictionary.Values.ToList();
}
示例12: AllowRead
public override ReadVetoResult AllowRead(string key, Stream data, RavenJObject metadata, ReadOperation operation)
{
RavenJToken value;
if (metadata.TryGetValue("Raven-Delete-Marker", out value))
return ReadVetoResult.Ignore;
return ReadVetoResult.Allowed;
}
示例13: BundleFetchConnection
public BundleFetchConnection(Transport transportBundle, Stream src)
{
transport = transportBundle;
bin = new BufferedStream(src, IndexPack.BUFFER_SIZE);
try
{
switch (readSignature())
{
case 2:
readBundleV2();
break;
default:
throw new TransportException(transport.Uri, "not a bundle");
}
}
catch (TransportException)
{
Close();
throw;
}
catch (IOException err)
{
Close();
throw new TransportException(transport.Uri, err.Message, err);
}
}
示例14: Load
public static AcmeRegistration Load(Stream s)
{
using (var r = new StreamReader(s))
{
return JsonConvert.DeserializeObject<AcmeRegistration>(r.ReadToEnd());
}
}
示例15: Save
public void Save(Stream s)
{
using (var w = new StreamWriter(s))
{
w.Write(JsonConvert.SerializeObject(this, Formatting.Indented));
}
}