convert Object[] from stream to byte[]

Martin Frank :

I try to filter a byte[], I want to remove line breaks from the stream

byte[] data = "y\neahbabydoit\nsothisistheway\ntodoit".getBytes();
Object[] tmp = IntStream.range(0, data.length).mapToObj(idx -> Integer.valueOf(data[idx]).byteValue()).filter(i -> i != 10).toArray();
System.out.println("src:"+Arrays.toString(data));
System.out.println("dst:"+Arrays.toString(tmp));

//works not!!
byte[] dest = (Byte[]) tmp; //help me here

The result (as expected) works (so far) but I'm not able to convert the result (Object[]) in an easy way back (to byte[])...

src:[121, 10, 101, 97, 104, 98, ...
dst:[121, 101, 97, 104, 98, 97, ...

I know there are ways to solve this problem (see How to convert int[] to byte[]) but I want to do it in an easy (stream-like) way...

any news from java 8 or later?

RP- :

As @Mureinik suggested, it is better to deal with characters rather than bytes, to answer your question, you can certainly use something like

Byte[] tmp = IntStream.range(0, data.length)
        .mapToObj(idx -> Integer.valueOf(data[idx]).byteValue())
        .filter(i -> i != 10)
        .toArray(Byte[]::new);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=76691&siteId=1