I would like to ask for opinions/advices regarding a a part of my algorithm.
ByteBuffer bb = ByteBuffer.allocate(8);
bb.putLong(rs.getLong(index));//retrieve long from db (unsigned INT)
byte[] tmp = new byte[4];
bb.position(4);
bb.get(tmp);
(Inet4Address) InetAddress.getByAddress(tmp);
vs.
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt((int) rs.getLong(index));//retrieve long from db (unsigned INT)
bb.flip();
byte[] tmp = new byte[4];
bb.get(tmp);
(Inet4Address) InetAddress.getByAddress(tmp);
Basically I would like to know whether there is a performance difference in casting or is it better to use bigger ByteBuffer.
Thanks, regards,
Marek
Casting is “cheap” especially compared to allocating new
ByteBuffers and calling a few methods.I’m not entirely sure what you’re trying to do, but perhaps a simple shift-right would do the trick? For instance this snippet of code: