本文整理汇总了C#中System.IO.Stream.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.Dispose方法的具体用法?C# Stream.Dispose怎么用?C# Stream.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Stream
的用法示例。
在下文中一共展示了Stream.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CopyStreamsAsyncParallel
public static async Task<long> CopyStreamsAsyncParallel(Stream src, Stream dst)
{
long numCopied = 0;
byte[][] buffer = new byte[2][];
buffer[0] = new byte[0x1000];
buffer[1] = new byte[0x1000];
var index = 0;
int numRead = await src.ReadAsync(buffer[index], 0, buffer[index].Length);
while (numRead > 0)
{
var writeAsync = dst.WriteAsync(buffer[index], 0, numRead);
index = index ^= 1;
var readAsync = src.ReadAsync(buffer[index], 0, buffer[index].Length);
Task.WaitAll(writeAsync, readAsync);
numRead = readAsync.Result;
numCopied += numRead;
}
await dst.FlushAsync();
src.Dispose();
dst.Dispose();
return numCopied;
}
示例2: DisposeObject
private static void DisposeObject(ref HttpWebRequest request, ref HttpWebResponse response,
ref Stream responseStream, ref StreamReader reader)
{
if (request != null)
{
request = null;
}
if (response != null)
{
response.Close();
response = null;
}
if (responseStream != null)
{
responseStream.Close();
responseStream.Dispose();
responseStream = null;
}
if (reader != null)
{
reader.Close();
reader.Dispose();
reader = null;
}
}
示例3: MemoryImage
/// <exception cref="InvalidImageException">
/// ストリームから読みだされる画像データが不正な場合にスローされる
/// </exception>
protected MemoryImage(Stream stream)
{
try
{
this.Image = Image.FromStream(stream);
}
catch (ArgumentException e)
{
stream.Dispose();
throw new InvalidImageException("Invalid image", e);
}
catch (OutOfMemoryException e)
{
// GDI+ がサポートしない画像形式で OutOfMemoryException がスローされる場合があるらしい
stream.Dispose();
throw new InvalidImageException("Invalid image?", e);
}
catch (ExternalException e)
{
// 「GDI+ で汎用エラーが発生しました」という大雑把な例外がスローされる場合があるらしい
stream.Dispose();
throw new InvalidImageException("Invalid image?", e);
}
catch (Exception)
{
stream.Dispose();
throw;
}
this.Stream = stream;
}
示例4: saveOptions
public void saveOptions(Options options)
{
XmlSerializer formatter = new XmlSerializer(typeof(Options));
try
{
stream = File.Open(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "options.xml", FileMode.OpenOrCreate);
formatter.Serialize(stream, options);
stream.Dispose();
}
catch
{
stream.Dispose();
}
}
示例5: PaletteTable
public PaletteTable(Stream stream, bool leaveOpen = true)
{
ReadPaletteEntries(stream);
if(!leaveOpen)
stream.Dispose();
}
示例6: GetFileExt
/// <summary>
/// 返回文件真实后缀
/// </summary>
/// <param name="stream"></param>
/// <param name="closeStream"></param>
/// <returns></returns>
public static FileExt GetFileExt(Stream stream, bool closeStream = false)
{
System.IO.BinaryReader br = new System.IO.BinaryReader(stream);
StringBuilder sb = new StringBuilder();
byte buffer;
try
{
buffer = br.ReadByte();
sb.Append(buffer.ToString());
buffer = br.ReadByte();
sb.Append(buffer.ToString());
return (FileExt)Convert.ToInt32(sb.ToString());
}
catch
{
return FileExt.None;
}
finally
{
if (closeStream)
{
stream.Close();
stream.Dispose();
}
else
{
stream.Position = 0;
}
}
}
示例7: WriteBinaryMap
public Boolean WriteBinaryMap(String FileName)
{
try
{
serialzed_stream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
binary_formatter = new BinaryFormatter();
xmap = new FileFormat();
WriteMapData();
}
catch (Exception e)
{
#if(DEBUG)
MessageBox.Show(e.Message, "Save Map Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#else // RELEASE
MessageBox.Show("Error while saving map, the file could be in use by another program, or is write protected", "Save Map Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
return false;
}
finally
{
serialzed_stream.Close();
serialzed_stream.Dispose();
}
// Write to file Ok
return true;
}
示例8: WriteToStream
async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
{
try
{
var buffer = new byte[_chunkSize];
using (var inputStream = OpenInputStream())
{
var length = (int)inputStream.Length;
var bytesRead = 1;
while (length > 0 && bytesRead > 0)
{
bytesRead = inputStream.Read(buffer, 0, Math.Min(length, buffer.Length));
await outputStream.WriteAsync(buffer, 0, bytesRead);
length -= bytesRead;
}
}
}
catch (HttpException)
{
}
finally
{
outputStream.Close();
outputStream.Dispose();
}
}
示例9: GetStream
public static Stream GetStream(Stream fileStream)
{
Stream objFileStream = null;
try
{
GZipStream gzip = new GZipStream(fileStream, CompressionMode.Decompress);
MemoryStream memStream = new MemoryStream();
byte[] bytes = new byte[1024];
int bytesRead = -1;
while (bytesRead != 0)
{
bytesRead = gzip.Read(bytes, 0, 1024);
memStream.Write(bytes, 0, bytesRead);
memStream.Flush();
}
memStream.Position = 0;
objFileStream = memStream;
gzip.Close();
gzip.Dispose();
fileStream.Dispose();
}
catch (Exception)
{
fileStream.Position = 0;
objFileStream = fileStream;
}
return objFileStream;
}
示例10: ReplaceAtomically
public void ReplaceAtomically(Stream newData, Stream newLog)
{
var newDataStream = ((FileStream)newData);
string dataTempName = newDataStream.Name;
newDataStream.Flush();
newDataStream.Dispose();
var newLogStream = ((FileStream)newLog);
string logTempName = newLogStream.Name;
newLogStream.Flush();
newLogStream.Dispose();
newData.Dispose();
newLog.Dispose();
log.Dispose();
data.Dispose();
string renamedDataFile = dataPath + ".rename_op";
string renamedLogFile = logPath + ".rename_op";
File.Move(dataPath, renamedDataFile);
File.Move(logPath, renamedLogFile);
File.Move(logTempName, logPath);
File.Move(dataTempName, dataPath);
File.Delete(renamedDataFile);
File.Delete(renamedLogFile);
OpenFiles();
}
示例11: WaveFileReader
private WaveFileReader(Stream inputStream, bool ownInput)
{
this.waveStream = inputStream;
var chunkReader = new WaveFileChunkReader();
try
{
chunkReader.ReadWaveHeader(inputStream);
this.waveFormat = chunkReader.WaveFormat;
this.dataPosition = chunkReader.DataChunkPosition;
this.dataChunkLength = chunkReader.DataChunkLength;
this.chunks = chunkReader.RiffChunks;
}
catch
{
if (ownInput)
{
inputStream.Dispose();
}
throw;
}
Position = 0;
this.ownInput = ownInput;
}
示例12: getSERI
/// <summary>
/// Reads a SERI from a XML file.
/// </summary>
/// <param name="xmlData">The Stream of the XML file</param>
/// <returns></returns>
public static SERI getSERI(Stream xmlData)
{
XmlSerializer deserializer = new XmlSerializer(typeof(SERI));
SERI output = (SERI)deserializer.Deserialize(xmlData);
xmlData.Dispose();
return output;
}
示例13: Art
public Art(Stream input)
{
ImageData=new byte[input.Length];
input.Read(ImageData, 0, ImageData.Length);
input.Close();
input.Dispose();
}
示例14: ODataRawInputContext
internal ODataRawInputContext(
ODataFormat format,
Stream messageStream,
Encoding encoding,
ODataMessageReaderSettings messageReaderSettings,
bool readingResponse,
bool synchronous,
IEdmModel model,
IODataUrlResolver urlResolver,
ODataPayloadKind readerPayloadKind)
: base(format, messageReaderSettings, readingResponse, synchronous, model, urlResolver)
{
Debug.Assert(messageStream != null, "stream != null");
Debug.Assert(readerPayloadKind != ODataPayloadKind.Unsupported, "readerPayloadKind != ODataPayloadKind.Unsupported");
ExceptionUtils.CheckArgumentNotNull(format, "format");
ExceptionUtils.CheckArgumentNotNull(messageReaderSettings, "messageReaderSettings");
try
{
this.stream = messageStream;
this.encoding = encoding;
this.readerPayloadKind = readerPayloadKind;
}
catch (Exception e)
{
// Dispose the message stream if we failed to create the input context.
if (ExceptionUtils.IsCatchableExceptionType(e) && messageStream != null)
{
messageStream.Dispose();
}
throw;
}
}
示例15: DynamicYaml
/// <summary>
/// Initializes a new instance of <see cref="DynamicYaml"/> from the specified stream.
/// </summary>
/// <param name="stream">A stream that contains a YAML content. The stream will be disposed</param>
/// <param name="disposeStream">Dispose the stream when reading the YAML content is done. <c>true</c> by default</param>
public DynamicYaml(Stream stream, bool disposeStream = true)
{
if (stream == null) throw new ArgumentNullException(nameof(stream));
// transform the stream into string.
string assetAsString;
try
{
using (var assetStreamReader = new StreamReader(stream, Encoding.UTF8))
{
assetAsString = assetStreamReader.ReadToEnd();
}
}
finally
{
if (disposeStream)
{
stream.Dispose();
}
}
// Load the asset as a YamlNode object
var input = new StringReader(assetAsString);
yamlStream = new YamlStream();
yamlStream.Load(input);
if (yamlStream.Documents.Count != 1 || !(yamlStream.Documents[0].RootNode is YamlMappingNode))
throw new YamlException("Unable to load the given stream");
}