52

Byte streams contain, well, bytes. Broken down into what it is actually, it is 8 bits composed of 1s and 0s. If it were representing a number, it would be any number from 0 to 255 (which, I may add, is no coincidence why the 4 numbers in an IP address always range from 0 to 255). Byte streams are usually sophisticated interfaces meant to hide the ...


46

Streams provide much better abstraction for composition of different operations you want to do on top of collections or streams of data coming in. Especially when you need to map elements, filter and convert them. Your example is not very practical. Consider the following code from Oracle site. List<Transaction> groceryTransactions = new Arraylist<...


18

A byte is simply a unit of information - it can be anything. A byte by itself doesn't mean anything, you have to attach some sort of meaning to it. So, to expand on that - Does it contain bytes (hex data) or binary data or english letters only? Hex data is the same as binary data. It's just a different way of displaying the data. For example, 0x41 = ...


16

Another advantage of using the functional streaming API is, that it hides implementation details. It only describes what should be done, not how. This advantage becomes obvious when looking at the change that needs to be done, to change from single threaded to parallel code execution. Just change the .stream() to .parallelStream().


16

The short answer is that ungetc allows you to peek at the next character without consuming it. Let's say you're reading a packetized data format. It contains, among other things, a frame sync pattern. Frame sync patterns allow you to align data by marking the beginning of a data frame in an otherwise unsynchronized data stream. To facilitate the ...


14

If myPrivateThingies is mutable, you've created a hidden dependency between your private state and the stream results. If it's possible for the client to indirectly cause myPrivateThingies to change state, then he's going to get a different result when calling collect than the one you originally intended to give out. If myPrivateThingies is immutable, then ...


13

If anything, it is harder to read and understand. That is highly subjective. I find the second version much easier to read and understand. It matches how other languages (e.g. Ruby, Smalltalk, Clojure, Io, Ioke, Seph) do it, it requires fewer concepts to understand (it's just a normal method call like any other, whereas the first example is specialized ...


12

Each time a process produces output, it has to call a function that actually does the work. In most cases, that function is ultimately write(2). On a multitasking operating system, the call to write() will trap into the kernel, which has to stop the process, handle the I/O, do other things while any blockages are cleared, put it on the ready queue and get ...


11

Streams and collections are not alternatives, they serve completely different purposes. java.util.Collection is an interface for representing a "collection of things", its primary purpose is to keep things, like an array in JavaScript. java.util.Stream is an interface for processing things in multiple stages. In most cases you will actually have both, a ...


10

You can use grepcode.com to search through the Java standard library code (and some other libraries). Unfortunately, the stream implementation code is rather abstract. A good starting point is the internal java.util.stream.SortedOps class which transforms a stream into a sorted stream. The current implementation (used for streams of standard library ...


8

Put things in one end and receive it from the other end in the same order This would be a reasonable definition of a stream, and after all these years I still sometimes find myself falling into this mental trap for a moment. But that's not how Streams work in .NET. Instead, think of an I/O driver: you can write to it, you can read from it, you can seek. ...


7

Edit: apologies for blatantly wrong info. Acording to this Wikipedia article, it will be a bit larger: Very roughly, the final size of Base64-encoded binary data is equal to 1.37 times the original data size + 814 bytes (for headers). You can use ContentLength property of the request to determine what the size is in bytes, although if you are uploading ...


7

A streaming app is an app that consumes a stream of data. A stream of data is transmitted data formatted in a way that can be useful even when incomplete. Since partial stream data does not require complete transmission this allows consumers to join and leave at any time. It also allows for transmission to be continuous, though it may start and stop on ...


6

Your interpretation applies to a queue, not a stream. A queue offers methods to add at one end (Enqueue) and take from the other end (Dequeue), in a FIFO manner. Stream in a software sense expresses the serial, continuous character, not so much the transporting character.


5

Kafka/Kinesis is modelled as a stream. A stream has different properties than messages. Streams have context to them. They have order. You can apply window functions on streams. Although each item in a stream is meaningful, it may be more meaningful with the context around it Because streams have order, you can use that to make certain statements about the ...


5

Kafka deals in ordered logs of atomic messages. You can view it sort of like the pub/sub mode of message brokers, but with strict ordering and the ability to replay or seek around the stream of messages at any point in the past that's still being retained on disk (which could be forever). Kafka's flavor of streaming stands opposed to remote procedure call ...


5

According to the documentation, the sorted() method is a stateful intermediate operation. You can read more about what that means here: Intermediate operations return a new stream. They are always lazy; executing an intermediate operation such as filter() does not actually perform any filtering, but instead creates a new stream that, when traversed, ...


5

The short and simple answer is that using std::endl can and will slow output by a huge margin. In fact, I'm reasonably convinced that std::endl is responsible for most of the notion that C++ iostreams are substantially slower than C-style I/O. For example, consider a program like this: #include <iostream> #include <string> #include <sstream&...


4

The most important thing to consider: Streams can only be iterated once, whereas you have more flexibility over a Collection: you can continue to create more Streams or even Iterators to do additional, repetitive processing on the results. So if you are not sure whether callers of the method are going to use the results once and only once, it's better to ...


4

You can calculate the file size (in bytes) using below formula: x = (n * (3/4)) - y Where: 1. x is the size of a file in bytes 2. n is the length of the Base64 String 3. y will be 2 if Base64 ends with '==' and 1 if Base64 ends with '='. You can read the algorithm here Base 64 wiki


4

The answer to all these questions is: "You don't know!". Please bear in mind that this is a good thing. The entire point of a stream class is to abstract from the tedious detail of how much to read when, and where to allocate the buffer to store stuff that you've already been given by the controller but not yet delivered to your caller. If you wanted to ...


4

I hope it's not poor form to answer my own question, but upon trying to implement a LogicProgramAnswersetIterator, I ran into some problems that lead me to think either I would have to go to "heroic" measures to pretend that my objects are iterable, or else admit that iterator is not a good match for this use case. I think that the reasons why that is the ...


4

All you have to do is buffer your structured events such that you can look them up by affected path (source and destination, probably). The actual mechanism for combining any given pair of events will depend on what those events are, so you just need to manually code the optimisations you want. It's much simpler to do this pairwise than to somehow ...


3

should one load only the needed amount as shown above ? You should load into the buffer all that you can feasibly expect to process with the code that follows. In the DataReader documentation example, they read the entire stream into the buffer, because they are going to process it all immediately. The reason for the buffer is that IO is slow (usually). So ...


3

it misuses java streaming api to handle something that usually would be covered by simple for and if statements This is a vacuous argument. Everything the streaming API does except for parallelization can be handled by for and if statements. Whether they're simple is a matter of taste; IMO your first example is already not simple. (You also managed to put a ...


3

I'm not familiar with the specific site you're integrating with, so this answer is about the general approach to this kind of problem. The best thing to do in this situation is ask the administrators of the site in question how they want you to do it. Of course, this isn't always possible, so if you can't do this, then the following approaches are in rough ...


3

In my view, no. The things you can do with streams are a strict superset of the things you can do with collections, and often they can be made more efficient, so there is no reason not to use them except unfamiliarity. "Lambda expressions are the gateway drug to Java 8, but Streams are the real addiction." (Venkat Subramaniam, Functional Programming in Java)


Only top voted, non community-wiki answers of a minimum length are eligible