3

我想就我的算法的一部分征求意见/建议。

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);

对比

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);

基本上我想知道转换是否存在性能差异,或者使用更大的 ByteBuffer 是否更好。

感谢和问候,

马立克

4

1 回答 1

2

基本上我想知道转换是否存在性能差异,或者使用更大的 ByteBuffer 是否更好。

铸造是“便宜的”,特别是与分配 newByteBuffer和调用一些方法相比。

我不完全确定您要做什么,但也许一个简单的右移就可以了?例如这段代码:

long l = rs.getLong(index);
InetAddress.getByAddress(new byte[] { (byte) ((l & 0xFF000000) >> 24),
                                      (byte) ((l & 0x00FF0000) >> 16),
                                      (byte) ((l & 0x0000FF00) >>  8),
                                      (byte) ((l & 0x000000FF) >>  0)});
于 2011-02-10T14:00:34.687 回答