Download large file in python with requests

Requests is a really nice library. I’d like to use it for downloading big files (>1GB). The problem is it’s not possible to keep whole file in memory; I need to read it in chunks. And this is a problem with the following code: import requests def DownloadFile(url) local_filename = url.split(“https://stackoverflow.com/”)[-1] r = requests.get(url) f … Read more

How do I generate a stream from a string?

I need to write a unit test for a method that takes a stream which comes from a text file. I would like to do do something like this: Stream s = GenerateStreamFromString(“a,b \n c,d”); 12 s 12 public static Stream GenerateStreamFromString(string s) { var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(s); … Read more

hat is the purpose of flush() in Java streams?

From the docs of the flush method: Flushes the output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination. … Read more

Get an OutputStream into a String

I would use a ByteArrayOutputStream. And on finish you can call: new String( baos.toByteArray(), codepage ); or better: baos.toString( codepage ); For the String constructor, the codepage can be a String or an instance of java.nio.charset.Charset. A possible value is java.nio.charset.StandardCharsets.UTF_8. The method toString() accepts only a String as a codepage parameter (stand Java 8).