3

I have ip address string like "192.168.1.1". Is it possible to parse string using java 8 streams and get primitive byte array as a result. Currently i have code

Arrays.stream(address.split("\\."))
 .map(UnsignedBytes::parseUnsignedByte)
 .toArray(Byte[]::new);

where is UnsignedBytes is guava class which return byte, but this code return Byte[] not byte[]

Update: Yes, i read about why ByteStream class is absent. My question is about, is it possible to get byte array using java-8 streams, without overhead such as create intermediate list.

user140547
8,2304 gold badges36 silver badges85 bronze badges
asked Jul 3, 2016 at 21:06
5
  • The only way I could possibly conceive of doing that would be to write a custom collector. That could work, but it might be tricky. Commented Jul 3, 2016 at 21:08
  • You might collect to a list and use guava's Bytes.toArray to get a primitive array. Commented Jul 3, 2016 at 21:27
  • 3
    Possible duplicate of In Java 8, is there a ByteStream class? Commented Jul 3, 2016 at 21:27
  • see the final collect in this answer: stackoverflow.com/a/36613808/3487617 Commented Jul 3, 2016 at 21:38
  • 1
    Frankly, this is an interesting intellectual effort, but this is the kind of situation where a plain old simple for loop is much simpler and readable. And you won't parallelize that stream, so it doesn't even have that advantage. Commented Jul 3, 2016 at 21:53

2 Answers 2

3

You could just use Java's InetAddress class.

 InetAddress i = InetAddress.getByName("192.168.1.1");
 byte[] bytes = i.getAddress();
answered Jul 3, 2016 at 22:13

3 Comments

I like this answer, but the javadoc says The result is in network byte order: the highest order byte of the address is in getAddress()[0]. I think you need to reverse the byte array to suit OP's need.
@Bohemian Not sure what order the OP wants. The code above has 192 in bytes[0], 168 in bytes[1], and so on. Which makes sense to me.
I must have been thinking bits (where 0 is the "last" bit). This is fine!
1

You can use Pattern.splitAsStream(CharSequence) use a ByteArrayOutputStream in a collector:

byte[] bytes = Pattern.compile("\\.")
 .splitAsStream(address)
 .mapToInt(Integer::parseInt)
 .collect(
 ByteArrayOutputStream::new,
 ByteArrayOutputStream::write,
 (baos1, baos2) -> baos1.write(baos2.toByteArray(), 0, baos2.size())
 )
 .toByteArray();

Collect code adapted from https://stackoverflow.com/a/32470838/3255152.

answered Jul 5, 2016 at 17:41

1 Comment

I like this answer better because it fits in the theme of the question, and would be useful outside of just parsing IP addresses.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.