FileUpload to FileStream

Since FileUpload.PostedFile.InputStream gives me Stream, I used the following code to convert it to byte array public static byte[] ReadFully(Stream input) { byte[] buffer = new byte[input.Length]; //byte[] buffer = new byte[16 * 1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); … Read more

Zip Stream in PHP

If your web server is running Linux, then you can do it streaming without a temp file being generated. Under Win32, you may need to use Cygwin or something similar. If you use – as the zip file name, it will compress to STDOUT. That can be redirected straight to the requester using passthru(). The … Read more

Compute a hash from a stream of unknown length in C#

MD5, like other hash functions, does not require two passes. To start: HashAlgorithm hasher = ..; hasher.Initialize(); As each block of data arrives: byte[] buffer = ..; int bytesReceived = ..; hasher.TransformBlock(buffer, 0, bytesReceived, null, 0); To finish and retrieve the hash: hasher.TransformFinalBlock(new byte[0], 0, 0); byte[] hash = hasher.Hash; This pattern works for any … Read more

When or if to Dispose HttpResponseMessage when calling ReadAsStreamAsync?

So it seems like the calling code needs to know about and take ownership of the response message as well as the stream, or I leave the response message undisposed and let the finalizer deal with it. Neither option feels right. In this specific case, there are no finalizers. Neither HttpResponseMessage or HttpRequestMessage implement a … Read more

ReadAllLines for a Stream object?

You can write a method which reads line by line, like this: public IEnumerable<string> ReadLines(Func<Stream> streamProvider, Encoding encoding) { using (var stream = streamProvider()) using (var reader = new StreamReader(stream, encoding)) { string line; while ((line = reader.ReadLine()) != null) { yield return line; } } } Then call it as: var lines = ReadLines(() … Read more

std::ostringstream printing the address of the c-string instead of its content

The expressionstd::ostringstream() creates a temporary, and operator<< which takes const char* as argument is a free function, but this free function cannot be called on a temporary, as the type of the first parameter of the function is std::ostream& which cannot be bound to temporary object. Having said that, <<std::ostringstream() << “some data” resolves to … Read more

How to pass variables as stdin into command line from PHP

I’m not sure about what you’re trying to achieve. You can read stdin with the URL php://stdin. But that’s the stdin from the PHP command line, not the one from pdftk (through exec). But I’ll give a +1 for proc_open() <?php $cmd = sprintf(‘pdftk %s fill_form %s output -‘,’blank_form.pdf’, raw2xfdf($_POST)); $descriptorspec = array( 0 => … Read more