I want to get stream of byte array but I came to know that Arrays
does not have method to get stream of byte array.
byte[] byteArr = new byte[100];
Arrays.stream(byteArr);//Compile time error
My questions,
NOTE : I know I can use Byte[]
instead of byte[]
but that does not answer my question.
There are only 3 types of primitive streams: IntStream, LongStream and DoubleStream.
So, the closest you can have is an IntStream, where each byte in your array is promoted to an int.
AFAIK, the simplest way to build one from a byte array is
IntStream.Builder builder = IntStream.builder();
for (byte b : byteArray) {
builder.accept(b);
}
IntStream stream = builder.build();
EDIT: assylias suggests another, shorter way:
IntStream stream = IntStream.range(0, byteArr.length)
.map(i -> byteArray[i]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With