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


C# BufferedStream.Dispose方法代码示例

本文整理汇总了C#中System.IO.BufferedStream.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# BufferedStream.Dispose方法的具体用法?C# BufferedStream.Dispose怎么用?C# BufferedStream.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.BufferedStream的用法示例。


在下文中一共展示了BufferedStream.Dispose方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SerializeUtf8

		public static void SerializeUtf8 (XmlSerializer serializer, Stream stream, object o)
		{
			if (serializer == null)
				throw new ArgumentNullException ("serializer");

			if (stream == null)
				throw new ArgumentNullException ("stream");

			if (o == null)
				throw new ArgumentNullException ("o");

			UnclosableStream unclosable_stream = new UnclosableStream (stream);
			BufferedStream buffered_stream = new BufferedStream (unclosable_stream, 8192);

			Stopwatch w = new Stopwatch ();
			w.Start ();
			XmlTextWriter xml_writer = new XmlTextWriter (buffered_stream, Encoding.UTF8);
			xml_writer.Formatting = Formatting.Indented;
			serializer.Serialize (xml_writer, o);

			// This will flush the stream and release the buffer.
			// Normally it also closes the underlying stream,
			// which is why it wraps an UnclosableStream.
			buffered_stream.Dispose ();
			w.Stop ();

			if (Debug)
				Log.Debug (">>> Serialization of {0}: {1}", o.GetType (), w);
		}
开发者ID:ArsenShnurkov,项目名称:beagle-1,代码行数:29,代码来源:XmlFu.cs

示例2: Read

        /// <summary>
        /// Reads all log messages from the supplied file.
        /// </summary>
        public static List<LogMessage> Read(string logFileName)
        {
            List<LogMessage> lst = new List<LogMessage>();
            FilePath.ValidatePathName(logFileName);
            using (var stream = new FileStream(logFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
            {
                try
                {
                    LogMessageSaveHelper helper;
                    int version = stream.ReadInt32();
                    switch (version)
                    {
                        case 282497:
                            helper = LogMessageSaveHelper.Create(); //VersionNumber: Compressed. With LogSaveHelper
                            break;
                        default:
                            throw new VersionNotFoundException();
                    }

                    using (var zipStream = new DeflateStream(stream, CompressionMode.Decompress, true))
                    using (var bs = new BufferedStream(zipStream))
                    {
                        while (bs.ReadBoolean())
                        {
                            var message = new LogMessage(bs, helper);
                            lst.Add(message);
                        }
                        bs.Dispose();
                    }
                }
                catch (EndOfStreamException)
                {

                }
                catch (ZlibException)
                {

                }

            }
            return lst;
        }
开发者ID:GridProtectionAlliance,项目名称:gsf,代码行数:45,代码来源:LogFileReader.cs

示例3: setMetaData

        public void setMetaData(string path, Dictionary<String, String> values)
        {
            Hashtable toReturn = new Hashtable();
            BufferedStream randomAccess = null;
            PdfReader pdfReader = null;
            Dictionary<String, String> keywords = new Dictionary<String, String>();
            JavaScriptSerializer j = new JavaScriptSerializer();
            try
            {
                randomAccess = new BufferedStream(this.WaitForFile(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Delete), 12288);
                pdfReader = new PdfReader(randomAccess, null);
                keywords = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(getValue(pdfReader.Info, "Keywords"));

                if (keywords == null)
                {
                    keywords = new Dictionary<String, String>();
                }
                MemoryStream mem = new MemoryStream();
                if (pdfReader != null)
                {
                    using (BufferedStream ms = new BufferedStream(mem, 12288))
                    {
                        using (PdfStamper stamper = new PdfStamper(pdfReader, ms))
                        {
                            Dictionary<String, String> info = pdfReader.Info;
                            foreach (KeyValuePair<string, string> entry in values)
                            {
                                if (keywords.ContainsKey(entry.Key))
                                {
                                    keywords[entry.Key] = entry.Value;
                                    if (entry.Key != IndividualRFileInfo.CURRENT_DIRECTORY)
                                    {
                                        FileInfo fInfo = new FileInfo(path);
                                        if (keywords.ContainsKey(IndividualRFileInfo.CURRENT_DIRECTORY))
                                        {
                                            keywords[IndividualRFileInfo.CURRENT_DIRECTORY] = fInfo.Directory.Name;
                                        }
                                        else
                                        {
                                            keywords.Add(IndividualRFileInfo.CURRENT_DIRECTORY, fInfo.Directory.Name);

                                        }
                                    }
                                }
                                else
                                {
                                    keywords.Add(entry.Key, entry.Value);
                                }
                            }
                            string allStr = j.Serialize(keywords);
                            info["Keywords"] = allStr;

                            stamper.MoreInfo = info;
                            stamper.Close();
                        }

                        //File.Delete(path);
                        File.WriteAllBytes(path, mem.ToArray());
                        ms.Close();
                        ms.Dispose();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                if (randomAccess != null)
                {
                    randomAccess.Close();

                }
                if (pdfReader != null)
                {
                    pdfReader.Close();
                    pdfReader.Dispose();
                }
            }
        }
开发者ID:wtang2006,项目名称:SpiderWeb,代码行数:81,代码来源:FileManager.cs

示例4: ExecuteAsync


//.........这里部分代码省略.........
                                    transferredBytes += bytesRead;

                                    int offset = 0;

                                    if (partStream.Position + bytesRead > PART_STREAM_HASH_SIZE)
                                    {
                                        var length = PART_STREAM_HASH_SIZE - (int)partStream.Position;
                                        partStream.Write(buffer, 0, length);
                                        offset = length;
                                    }
                                    else
                                    {
                                        partStream.Write(buffer, 0, bytesRead);
                                        offset = bytesRead;
                                    }

                                    if (partStream.Position == PART_STREAM_HASH_SIZE)
                                    {
                                        partStream.Position = 0;
                                        hashes.AddLast(TreeHashGenerator.CalculateTreeHash(partStream));
                                    }

                                    if (offset != bytesRead)
                                    {
                                        partStream.Write(buffer, offset, bytesRead - offset);
                                    }

                                    // Make callback on progress
                                    AWSSDKUtils.InvokeInBackground(
                                        this.options.StreamTransferProgress,
                                        new Runtime.StreamTransferProgressArgs(bytesRead, transferredBytes, contentLength),
                                        this.manager);
                                }

                                if (retryAttempts > 0)
                                    retryAttempts = 0; // Reset retry attempts back to 0 since we able to successfully write more data to disk.

                            } while (bytesReadFromGetJobOutput < totalBytesFromGetJobOutput);

                            // Compute hash of the last remaining bytes
                            if (partStream.Position != 0)
                            {
                                partStream.SetLength(partStream.Position);
                                partStream.Position = 0;
                                hashes.AddLast(TreeHashGenerator.CalculateTreeHash(partStream));
                            }

                            break;
                        }
                        finally
                        {
                            output.Dispose();
                            output = null;

                            try { if (input != null) input.Dispose(); }
                            catch (Exception) { }
                        }
                    }
                    catch (Exception e)
                    {
                        var age = e as AmazonGlacierException;
                        if (age != null && age.StatusCode == HttpStatusCode.NotFound)
                        {
                            throw;
                        }

                        fileMode = FileMode.Append;
                        rangeValue = string.Format(CultureInfo.InvariantCulture, "bytes={0}-", new FileInfo(filePath).Length);
                        retryAttempts++;

                        if (retryAttempts <= DownloadFileCommand.MAX_OPERATION_RETRY)
                        {
                            Console.WriteLine("Error and going to retry: {0}", e.Message);
                            Console.WriteLine(e.StackTrace);

                            Thread.Sleep(60 * 1000);
                        }
                        else
                            throw;
                    }
                }

                var computedCheckSum = TreeHashGenerator.CalculateTreeHash(hashes);
                if (!string.Equals(glacierProvidedCheckSum, computedCheckSum, StringComparison.OrdinalIgnoreCase))
                {
                    throw new AmazonGlacierException("Checksum of the downloaded file does not match the checksum reported by Amazon Glacier.");
                }
            }
            catch (IOException e)
            {
                throw new IOException("Unable to save archive to disk", e);
            }
            finally
            {
                try { if (input != null) input.Dispose(); }
                catch (Exception) { }
                try { if (output != null) output.Dispose(); }
                catch (Exception) { }
            }
        }
开发者ID:aws,项目名称:aws-sdk-net,代码行数:101,代码来源:DownloadJobCommand.async.cs

示例5: ReadFileAsEnumerableWithWrap

        // note: exceptions handled and ignored (logged only)
        public static IEnumerable<LineInfo> ReadFileAsEnumerableWithWrap(string path, int startLine)
        {
            FileStream fs = null;
            BufferedStream bs = null;
            StreamReader sr = null;
            try
            {
                fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite);
                bs = new BufferedStream(fs);
                sr = new StreamReader(bs);
            }
            catch (IOException e)
            {
                Logger.Error(e, "Cannot open file '" + path + "'");
                if (sr != null)
                    sr.Dispose();
                if (bs != null)
                    bs.Dispose();
                if (fs != null)
                    fs.Dispose();
                yield break;
            }

            string line;
            IList<string> wrappedLines = new List<string>(startLine - 1);
            int lineNum = 0;
            String startLineString = string.Empty;
            do
            {
                try
                {
                    line = sr.ReadLine();
                    lineNum++;
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Cannot read from file '" + path + "'");
                    sr.Dispose();
                    bs.Dispose();
                    fs.Dispose();
                    yield break;
                }
                if (line != null)
                {
                    if (lineNum >= startLine)
                    {
                        if (lineNum == startLine)
                        {
                            startLineString = line;
                        }
                        yield return new LineInfo(line, lineNum);
                    }
                    else
                    {
                        wrappedLines.Add(line);
                    }
                }
            }
            while (line != null);
            sr.Dispose();
            bs.Dispose();
            fs.Dispose();

            int i = 1;
            foreach (string wrappedLine in wrappedLines)
            {
                yield return new LineInfo(wrappedLine, i++);
            }
            yield return new LineInfo(startLineString, i);
        }
开发者ID:kmerlin,项目名称:PsISEProjectExplorer,代码行数:71,代码来源:FileReader.cs

示例6: GetImageUsingKDUExpand


//.........这里部分代码省略.........
                        {
                            tileheight = int.Parse(Math.Round(imageSize.Height * scale).ToString());
                            tilewidth = int.Parse(Math.Round(imageSize.Width * scale).ToString());
                        }

                        break;
                    default:
                        if (wScale < hScale)
                            scale = wScale;
                        else
                            scale = hScale;

                        scalex = scaley = scale;
                        break;
                }

                regions = p_state.Region.Split(new char[] { ',' });

                string region = "";
                if (regions.Length == 4)
                {
                    // for percentage regions
                    if (p_state.Region.StartsWith("%"))
                    {
                        startx = float.Parse(regions[0].Substring(1));
                        starty = float.Parse(regions[1]);

                        tilewidth = float.Parse(regions[2]);
                        tileheight = float.Parse(regions[3]);
                    }
                    else
                    {
                        startx = float.Parse(regions[0]) / imageSize.Width;
                        starty = float.Parse(regions[1]) / imageSize.Height;

                        tilewidth = float.Parse(regions[2]) / imageSize.Width;
                        tileheight = float.Parse(regions[3]) / imageSize.Height;
                    }

                    region = "{" + starty + "," + startx + "},{" + tileheight + "," + tilewidth + "}";
                }

                if (starty > 1 || startx > 1 || startx + tilewidth < 0 || starty + tileheight < 0)
                {
                    throw new IIIFException(getErrorXml(new Error { Code = "i3f_400", Message = "Invalid region specified" }));
                }

                // try to get the closest possible size to the one we want in order to reduce the tile size
                // when creating an image tile
                int reduce = Convert.ToInt32(1 / scale);
                if (reduce > 0)
                    reduce = Convert.ToInt32(Math.Floor(Math.Log(reduce, 2.5)));
                p_state.Reduce = reduce;

                string outputFile = ConfigurationManager.AppSettings["JP2CachePath"] + p_state.Target.Replace(".jpg", ".bmp");

                ProcessStartInfo start = new ProcessStartInfo();
                start.FileName = ConfigurationManager.AppSettings["ExpanderPath"];
                start.UseShellExecute = false;
                start.RedirectStandardOutput = false;
                start.RedirectStandardError = true;
                start.CreateNoWindow = true;

                start.Arguments = "-resilient -quiet -i \"" + fname + "\" -o \"" +
                    outputFile +
                    "\" -reduce " + reduce +
                    (string.IsNullOrEmpty(region) ? "" : " -region " + region);

                Process proc = Process.Start(start);
                proc.WaitForExit();

                BufferedStream bs = new BufferedStream(new FileStream(outputFile, FileMode.Open));

                int length = (int)bs.Length;
                byte[] imageBits = new byte[length];

                // read the digital bits of the image into the byte array
                bs.Read(imageBits, 0, length);

                Image img = Image.FromStream(bs);

                Bitmap bmp = new Bitmap(img);

                MemoryStream stream = new MemoryStream();
                bmp.Save(stream, ImageFormat.Jpeg);

                // remove temporary bmp file
                img.Dispose();
                bs.Close();
                bs.Dispose();

                File.Delete(outputFile);

                return stream;
            }
            catch (Exception)
            {
                throw;
            }
        }
开发者ID:britishlibrary,项目名称:SanddragonImageService,代码行数:101,代码来源:Extractor.cs

示例7: AsyncListen

        static void AsyncListen()
        {
            HttpListener listen = new HttpListener();
            listen.Prefixes.Add("http://*:8080/");
            listen.Start();
            while (true)
            {
                var context = listen.GetContext();
                Console.WriteLine("Request to: {0}", context.Request.Url);
                var proxyRequest = HttpWebRequest.Create(context.Request.RawUrl) as HttpWebRequest;

                IAsyncResult result = proxyRequest.BeginGetResponse(new AsyncCallback(new Action<IAsyncResult>((IAsyncResult r) =>
                    {
                        RequestPair pair = r.AsyncState as RequestPair;
                        HttpWebResponse endPointResponse = null;
                        try
                        {
                            endPointResponse = pair.Req.EndGetResponse(r) as HttpWebResponse;
                        }
                        catch (Exception)
                        {
                            Console.Error.WriteLine("Proxy Request Error");
                            using (StreamWriter sw = new StreamWriter(pair.Ctx.Response.OutputStream))
                            {
                                sw.WriteLine("Failure in proxy request");
                            }
                        }

                        pair.Ctx.Response.AddHeader("X-Evan-Async-Proxy", DateTime.Now.ToString());

                        BufferedStream reader = null;
                        BufferedStream writer = null;
                        try
                        {
                            reader = new BufferedStream(endPointResponse.GetResponseStream());
                            writer = new BufferedStream(pair.Ctx.Response.OutputStream);

                            var BUFSIZE = 2048;
                            byte[] buf = new byte[BUFSIZE];

                            for (int bytesRead = reader.Read(buf, 0, BUFSIZE); bytesRead != 0; bytesRead = reader.Read(buf, 0, BUFSIZE))
                            {
                                writer.Write(buf, 0, bytesRead);
                            }
                        }
                        catch (Exception)
                        {
                            Console.Error.WriteLine("Error writing content at URL: {0} to the client", pair.Req.RequestUri);
                        }
                        finally
                        {
                            if (reader != null) reader.Dispose();
                            if (writer != null)
                            {
                                try
                                {
                                    writer.Dispose();
                                }
                                catch (Exception)
                                {
                                }
                            }
                            else
                            {
                                pair.Ctx.Response.Close();
                            }
                        }
                    })), new RequestPair() { Ctx = context, Req = proxyRequest });
            }
        }
开发者ID:evanlong,项目名称:publicgit,代码行数:70,代码来源:Program.cs

示例8: ToSignerInfo

            internal SignerInfo ToSignerInfo(
                DerObjectIdentifier	contentType,
                CmsProcessable		content,
				SecureRandom		random)
            {
                AlgorithmIdentifier digAlgId = DigestAlgorithmID;
                string digestName = Helper.GetDigestAlgName(digestOID);
                IDigest dig = Helper.GetDigestInstance(digestName);

                string signatureName = digestName + "with" + Helper.GetEncryptionAlgName(encOID);
                ISigner sig = Helper.GetSignatureInstance(signatureName);

                // TODO Optimise the case where more than one signer with same digest
                if (content != null)
                {
                    content.Write(new DigOutputStream(dig));
                }

                byte[] hash = DigestUtilities.DoFinal(dig);
                outer._digests.Add(digestOID, hash.Clone());

                sig.Init(true, new ParametersWithRandom(key, random));
                #if NETCF_1_0 || NETCF_2_0 || SILVERLIGHT || NETFX_CORE
                Stream sigStr = new SigOutputStream(sig);
                #else
                Stream sigStr = new BufferedStream(new SigOutputStream(sig));
                #endif

                Asn1Set signedAttr = null;
                if (sAttr != null)
                {
                    IDictionary parameters = outer.GetBaseParameters(contentType, digAlgId, hash);

                //					Asn1.Cms.AttributeTable signed = sAttr.GetAttributes(Collections.unmodifiableMap(parameters));
                    Asn1.Cms.AttributeTable signed = sAttr.GetAttributes(parameters);

                    if (contentType == null) //counter signature
                    {
                        if (signed != null && signed[CmsAttributes.ContentType] != null)
                        {
                            IDictionary tmpSigned = signed.ToDictionary();
                            tmpSigned.Remove(CmsAttributes.ContentType);
                            signed = new Asn1.Cms.AttributeTable(tmpSigned);
                        }
                    }

                    // TODO Validate proposed signed attributes

                    signedAttr = outer.GetAttributeSet(signed);

                    // sig must be composed from the DER encoding.
                    new DerOutputStream(sigStr).WriteObject(signedAttr);
                }
                else if (content != null)
                {
                    // TODO Use raw signature of the hash value instead
                    content.Write(sigStr);
                }

                sigStr.Dispose();
                byte[] sigBytes = sig.GenerateSignature();

                Asn1Set unsignedAttr = null;
                if (unsAttr != null)
                {
                    IDictionary baseParameters = outer.GetBaseParameters(contentType, digAlgId, hash);
                    baseParameters[CmsAttributeTableParameter.Signature] = sigBytes.Clone();

                //					Asn1.Cms.AttributeTable unsigned = unsAttr.GetAttributes(Collections.unmodifiableMap(baseParameters));
                    Asn1.Cms.AttributeTable unsigned = unsAttr.GetAttributes(baseParameters);

                    // TODO Validate proposed unsigned attributes

                    unsignedAttr = outer.GetAttributeSet(unsigned);
                }

                // TODO[RSAPSS] Need the ability to specify non-default parameters
                Asn1Encodable sigX509Parameters = SignerUtilities.GetDefaultX509Parameters(signatureName);
                AlgorithmIdentifier encAlgId = CmsSignedGenerator.GetEncAlgorithmIdentifier(
                    new DerObjectIdentifier(encOID), sigX509Parameters);

                return new SignerInfo(signerIdentifier, digAlgId,
                    signedAttr, encAlgId, new DerOctetString(sigBytes), unsignedAttr);
            }
开发者ID:sanyaade-iot,项目名称:Schmoose-BouncyCastle,代码行数:84,代码来源:CMSSignedDataGenerator.cs

示例9: SaveToDisk

        /// <summary>
        /// Save the world to disk. Let the caller decide if this should be in a thread because in some situations it shouldnt (ie: when loading a newly generated world the file has to be saved first).
        /// This is only called by a standalone server or a server thread running in single player. In single player the user can also manually initiate a save in which case this will be called using a Task.
        /// </summary>
        internal static void SaveToDisk()
        {
            if (File.Exists(Settings.WorldFileTempPath)) File.Delete(Settings.WorldFileTempPath);

            var fstream = new FileStream(Settings.WorldFileTempPath, FileMode.Create);
            var gzstream = new GZipStream(fstream, CompressionMode.Compress);
            //GZipStream only applies compression during .Write, writing 2 bytes at a time ends up inflating it a lot. Adding this saves up to 99.3%
            var buffstream = new BufferedStream(gzstream, 65536);
            var chunkBytes = new byte[Chunk.SIZE_IN_BYTES];

            var worldSettings = WorldSettings.GetXmlByteArray();
            buffstream.Write(BitConverter.GetBytes(worldSettings.Length), 0, sizeof(int));
            buffstream.Write(worldSettings, 0, worldSettings.Length); //write the length of the world config xml

            for (var x = 0; x < SizeInChunksX; x++)
            {
                for (var z = 0; z < SizeInChunksZ; z++)
                {
                    Buffer.BlockCopy(Chunks[x,z].Blocks.Array, 0, chunkBytes, 0, chunkBytes.Length);
                    //Buffer.BlockCopy(Chunks[x,z].Blocks.DiffArray, 0, chunkBytes, 0, chunkBytes.Length); 'bm: this will save a diff instead, WIP
                    buffstream.Write(chunkBytes, 0, chunkBytes.Length);
                }
            }
            buffstream.Flush();
            buffstream.Close();
            gzstream.Close();
            fstream.Close();
            buffstream.Dispose();
            gzstream.Dispose();
            fstream.Dispose();

            File.Copy(Settings.WorldFileTempPath, Settings.WorldFilePath, true);
            File.Delete(Settings.WorldFileTempPath);
        }
开发者ID:MagistrAVSH,项目名称:voxelgame,代码行数:38,代码来源:WorldData.cs


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