How to reset a BehaviorSubject

I assume you want to clear the BehaviorSubject (because otherwise don’t call onComplete on it). That is not supported but you can achieve a similar effect by having a current value that is ignored by consumers: public static final Object EMPTY = new Object(); BehaviorSubject<Object> subject = BehaviorSubject.createDefault(EMPTY); Observable<YourType> obs = subject.filter(v -> v != … Read more

Is it possible to pipe to console.log?

console.log is just a function that pipes the process stream to an output. Note that the following is example code console.log = function(d) { process.stdout.write(d + ‘\n’); }; Piping to process.stdout does exactly the same thing. http.get(url, function(response) { response.pipe(process.stdout); response.on(‘end’, function() { console.log(‘finished’); }); }); Note you can also do process.stdout.write(response);

Closing a Java FileInputStream

For Java 7 and above try-with-resources should be used: try (InputStream in = new FileInputStream(file)) { // TODO: work } catch (IOException e) { // TODO: handle error } If you’re stuck on Java 6 or below… This pattern avoids mucking around with null: try { InputStream in = new FileInputStream(file); try { // TODO: … Read more

Concatenate two (or n) streams

This can be done with vanilla Node.js import { PassThrough } from ‘stream’ const merge = (…streams) => { let pass = new PassThrough() for (let stream of streams) { const end = stream == streams.at(-1); pass = stream.pipe(pass, { end }) } return pass } Use streams.slice(-1)[0] if you don’t have .at() in your … 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

How do I concatenate two System.IO.Stream instances into one?

class ConcatenatedStream : Stream { Queue<Stream> streams; public ConcatenatedStream(IEnumerable<Stream> streams) { this.streams = new Queue<Stream>(streams); } public override bool CanRead { get { return true; } } public override int Read(byte[] buffer, int offset, int count) { int totalBytesRead = 0; while (count > 0 && streams.Count > 0) { int bytesRead = streams.Peek().Read(buffer, offset, … Read more

Add mime type to HTML link

Time to answer my own question. This is a really old question and it probably wasn’t possible at the time but lots has changed since then. The HTML5 spec added the download attribute: <a href=”https://stackoverflow.com/questions/2110269/hugepdf.pdf” download>Download file</a> This will do exactly what I need, tell the browser to download the file instead of opening it. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)