-1

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();
}
Konrad Borowski
  • 11,584
  • 3
  • 57
  • 71
  • How about writing the bytes into a `ByteArrayOutputStream` and returning that from the stream? – Henrik Aasted Sørensen Feb 23 '18 at 09:53
  • 4
    What type of objects does the `Stream` contain? – daniu Feb 23 '18 at 09:55
  • show us some code? Will help to get an idea about what you want to try. – ProgrammerBoy Feb 23 '18 at 09:56
  • Related: https://stackoverflow.com/questions/32459683/in-java-8-is-there-a-bytestream-class – Andy Turner Feb 23 '18 at 09:59
  • 1
    Why do you think you can convert the stream to a byte array? It would require serialization at the minimum. – Kayaman Feb 23 '18 at 10:03
  • @daniu `int` or `Integer`, but it doesn't really matter, whatever works for the solution (question of using `map` vs `mapToObj`). – Konrad Borowski Feb 23 '18 at 10:46
  • @Kayaman Because Java provides `toArray` methods for `IntStream`, `LongStream`, `DoubleStream` and `Stream`. It seems like there should be a way to convert to `byte[]`. – Konrad Borowski Feb 23 '18 at 10:54
  • Well it might seem like that to someone with no understanding about Java. You can't just convert an object to `byte[]`, let alone a stream of objects. What do you think you'd do with those bytes? – Kayaman Feb 23 '18 at 10:56
  • @Kayaman I know that they aren't represented the same way, so copying is needed, but that's fine. What I have is `Stream` (or `IntStream`). – Konrad Borowski Feb 23 '18 at 10:57
  • Why do you have a `Stream` and not an `InputStream`? Did someone use `Stream` because they didn't really understand Java 8 streams? Because that's the only reason I could think of. – Kayaman Feb 23 '18 at 11:00
  • @Kayaman Because it comes from a stream over a collection which did some other transformations on a value. – Konrad Borowski Feb 23 '18 at 11:02
  • Sounds like bad design. A `Stream` is not a byte stream. – Kayaman Feb 23 '18 at 12:36

1 Answers1

0

I guess you can make byte[] from the original stream's individual entries and then concatenate them in the end.

static byte[] <T> toByteArray(Stream<T> stream, Function<T, byte[]> transform) {
    Stream<byte[]> arrayStream = stream.map(transform);
    arrayStream.collect(concatArrays);
}

However, the only implementation I can think of for a concatArrays Collector would be:

  • maintain a byte[] containing the already collected data
  • for each entry of the Stream, expand it to accomodate the new entry
  • use System.arraycopy to combine the arrays

I'd expect you'd be better off with your "boxing to Byte and then convert" in almost all cases.

daniu
  • 14,137
  • 4
  • 32
  • 53