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); }
public void avi_close() { if (fd != null) { avi_end(width, height, targetfps); writeindexs(); fd.Close(); fd.Dispose(); } fd = null; }
/// <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; }
private string CalculateHashFromStream(Stream stream) { using (var readerSource = new System.IO.BufferedStream(stream, 1200000)) { using (var md51 = new System.Security.Cryptography.MD5CryptoServiceProvider()) { md51.ComputeHash(readerSource); //Dispose after using readerSource.Dispose(); stream.Dispose(); //Because very big memory consuming, we need to force Garbage collection do its job immediately GC.Collect(); // return(Convert.ToBase64String(md51.Hash)); } } }
public static Stream Open() { var innerStream = File.Open(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileOptions.DeleteOnClose); try { var outerStream = new BufferedStream(innerStream, BufferSize); try { return(new NotFlushingBufferOnDisposeStream(outerStream, innerStream)); } catch { outerStream.Dispose(); throw; } } catch { innerStream?.Dispose(); throw; } }
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(); } } }
internal async Task ExecuteAsync() { long contentLength = -1; string glacierProvidedCheckSum = null; string rangeValue = null; Stream input = null; Stream output = null; try { // Make sure the directory exists to write too. FileInfo fi = new FileInfo(filePath); Directory.CreateDirectory(fi.DirectoryName); FileMode fileMode = FileMode.Create; int retryAttempts = 0; byte[] buffer = new byte[1024 * 1024 * 5]; long transferredBytes = 0; MemoryStream partStream = new MemoryStream(new byte[PART_STREAM_HASH_SIZE]); LinkedList<string> hashes = new LinkedList<string>(); while (true) { try { output = File.Open(filePath, fileMode, FileAccess.Write, FileShare.None); try { GetJobOutputRequest getJobOutputRequest = new GetJobOutputRequest() { AccountId = this.options.AccountId, VaultName = this.vaultName, JobId = jobId, Range = rangeValue }; ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)getJobOutputRequest).AddBeforeRequestHandler(new ArchiveTransferManager.UserAgentPostFix("DownloadArchive").UserAgentRequestEventHandlerSync); GetJobOutputResponse jobOutputResponse = await this.manager.GlacierClient.GetJobOutputAsync(getJobOutputRequest).ConfigureAwait(false); if (contentLength < 0) { contentLength = jobOutputResponse.ContentLength; glacierProvidedCheckSum = jobOutputResponse.Checksum; } #if CORECLR input = jobOutputResponse.Body; #else input = new BufferedStream(jobOutputResponse.Body); #endif long totalBytesFromGetJobOutput = jobOutputResponse.ContentLength; long bytesReadFromGetJobOutput = 0; int bytesRead = 0; do { bytesRead = input.Read(buffer, 0, buffer.Length); if (bytesRead > 0) { bytesReadFromGetJobOutput += bytesRead; output.Write(buffer, 0, bytesRead); 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) { } } }
// 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); }
private Stream GetImageUsingKDUExpand(State p_state) { try { string fname = ConfigurationManager.AppSettings["JP2CachePath"] + p_state.File; float starty = 0; float startx = 0; double tileheight = 1; double tilewidth = 1; string[] regions = null; float scale = 0; float scalex = 0; float scaley = 0; Metadata imageSize = GetJP2Metadata(fname); regions = p_state.Region.Split(new char[] { ',' }); if (regions.Length == 4) { if (!regions[0].StartsWith("%")) { tilewidth = int.Parse(regions[2]); tileheight = int.Parse(regions[3]); } else { tilewidth = double.Parse(regions[2]) * imageSize.Width; tileheight = double.Parse(regions[3]) * imageSize.Height; } } // assume all region returned else { startx = 0; starty = 0; } float wScale = (float)p_state.Width / imageSize.Width; float hScale = (float)p_state.Height / imageSize.Height; scale = scalex = scaley = 1; startx = 0; starty = 0; switch (p_state.SizeType) { case "resize": if (p_state.Region.Equals("all")) { scalex = wScale; scaley = hScale; if (p_state.SizeType.Equals("best")) { if (scalex < scaley) { scale = scalex; } else { scale = scaley; } } else { if (scalex > scaley) { scale = scalex; } else { scale = scaley; } } tilewidth = Convert.ToInt32(imageSize.Width * scale); tileheight = Convert.ToInt32(imageSize.Height * scale); } else if (p_state.Width != 0 && p_state.Height != 0) { if (tilewidth > 0) scalex = (float)(p_state.Width / tilewidth); else scalex = wScale; if (tileheight > 0) scaley = (float)(p_state.Height / tileheight); else scaley = hScale; if (p_state.Width == p_state.Height) { scale = scalex; // both scales should be the same so shouldn't matter } else if (p_state.Width > p_state.Height) { scale = scalex; } else { scale = scaley; } } else if (p_state.Width > p_state.Height) { if (tilewidth > 0) scale = (float)(p_state.Width / tilewidth); else scale = wScale; scaley = scalex = scale; } else { if (tileheight > 0) scale = (float)(p_state.Height / tileheight); else scale = hScale; } // get all if (!p_state.SizeType.Equals("resize") && p_state.Region.Equals("all")) { if (p_state.Width != 0) tilewidth = p_state.Width; if (p_state.Height != 0) tileheight = p_state.Height; } break; case "best": if (tilewidth > 0) scalex = (float)(p_state.Width / tilewidth); else scalex = wScale; if (tileheight > 0) scaley = (float)(p_state.Height / tileheight); else scaley = hScale; if (scalex < scaley) scale = scalex; else scale = scaley; // get all if (p_state.Region.Equals("all")) { tileheight = int.Parse(Math.Round(imageSize.Height * scale).ToString()); tilewidth = int.Parse(Math.Round(imageSize.Width * scale).ToString()); } break; case "proportion": //scale = scalex = scaley = p_state.Size; scale = p_state.Size; // get all if (p_state.Region.Equals("all")) { 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; } }
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 }); } }
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); }
/// <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); }