0

In Java 8, I am finding this a bit odd. I can't seem to create a byte stream out of a byte[] array. The Array.stream class seems to only work on int[], long[], double[], or T[].

public long update(final byte[] buffer) {
    crc = 0L;
    Arrays.stream(buffer).forEach(this::update); // expecting to go into a function update(byte b)
    return crc;
}

When I try alternatives like Stream.of(buffer) or Arrays.asList(buffer).stream().forEach(this::update); the stream just reflects itself and I get a byte[].

Am I missing something in the implementation here?

shmosel
  • 49,289
  • 6
  • 73
  • 138
E.S.
  • 2,733
  • 6
  • 36
  • 71
  • You would need to use `Array.stream()` if it existed. Byte streams were considered a corner case during the development of Java's stream API and not implemented. The assumption was that int streams would be good enough. – markspace Jan 03 '18 at 01:16
  • Hmm you'd think there would be an internal way to handle byte[] as if it were int[] internally? I.e., under the hood, could they have an Array.stream which converted the byte[] to int[]? – E.S. Jan 03 '18 at 01:17
  • For byte processing, the established practice is to use IO streams or NIO channels for which there's considerable amount of tooling in libraries like Commons or Guava (even if no actual IO takes place and all operations are done on buffers/byte arrays). That's probably the main underlying reason why bytes where omitted from the "functional" stream API. – oakad Jan 03 '18 at 01:21

0 Answers0