I need to convert a stream (Java 8 stream, not InputStream) into a byte array.
I have tried using toArray
method of Stream
, but it doesn't accept byte[]::new
, as byte
is a primitive type.
I also tried using toArray
method of IntStream
, but it doesn't accept arguments, it assumes you want to get int[]
back.
It's possible to convert a stream to a list or an array of boxed Byte
types, and then convert it to byte[]
, but I wonder if there is a better way.
Edit: Added code, if that helps understanding the question. This doesn't compile, it's just pseudo-code. If it helps, you can also consider IntStream
to be Stream<Byte>
, for my purposes it doesn't matter.
byte[] getArray(IntStream stream) {
return stream.toArray();
}