当前位置: 首页>>代码示例>>C#>>正文


C# Stream.Dispose方法代码示例

本文整理汇总了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;

        }
开发者ID:rikace,项目名称:Experiments,代码行数:28,代码来源:Comp.cs

示例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;
     }
 }
开发者ID:superhappy123,项目名称:Test,代码行数:25,代码来源:WebUrlRead.cs

示例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;
        }
开发者ID:Mitsui,项目名称:OpenTween,代码行数:34,代码来源:MemoryImage.cs

示例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();
     }
 }
开发者ID:DeepDolphin,项目名称:CameraSorterCmd,代码行数:14,代码来源:Program.cs

示例5: PaletteTable

        public PaletteTable(Stream stream, bool leaveOpen = true)
        {
            ReadPaletteEntries(stream);

            if(!leaveOpen)
                stream.Dispose();
        }
开发者ID:jaggedsoft,项目名称:mithiamapeditor,代码行数:7,代码来源:PaletteTable.cs

示例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;
                }
            }


        }
开发者ID:daixinkai,项目名称:Dai.CommonLib,代码行数:38,代码来源:Tools_Other.cs

示例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;
        }
开发者ID:plhearn,项目名称:tileEditor,代码行数:29,代码来源:MapWriter.cs

示例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();
            }
        }
开发者ID:Robin--,项目名称:Warewolf,代码行数:27,代码来源:HttpPushContentStream.cs

示例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;
        }
开发者ID:AugustoRuiz,项目名称:WYZTracker,代码行数:32,代码来源:SongManager.cs

示例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();
        }
开发者ID:Rationalle,项目名称:ravendb,代码行数:32,代码来源:FileBasedPersistentSource.cs

示例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;
        }
开发者ID:SimonSimCity,项目名称:NAudio,代码行数:25,代码来源:WaveFileReader.cs

示例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;
 }
开发者ID:Quibilia,项目名称:Ohana3DS-Transfigured,代码行数:12,代码来源:Serialization.cs

示例13: Art

 public Art(Stream input)
 {
     ImageData=new byte[input.Length];
     input.Read(ImageData, 0, ImageData.Length);
     input.Close();
     input.Dispose();
 }
开发者ID:killwort,项目名称:musicdata,代码行数:7,代码来源:Art.cs

示例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;
            }
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:35,代码来源:ODataRawInputContext.cs

示例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");
        }
开发者ID:Kryptos-FR,项目名称:xenko-reloaded,代码行数:33,代码来源:DynamicYaml.cs


注:本文中的System.IO.Stream.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。