将字节从一个ByteBuffer传输到另一个ByteBuffer

时间:2009-02-20 16:12:24

标签: java javadoc nio

将ByteBuffer bbuf_src中尽可能多的字节放入另一个ByteBuffer bbuf_dest(以及知道传输了多少字节)的最有效方法是什么?我正在尝试bbuf_dest.put(bbuf_src)但似乎想要抛出一个BufferOverflowException,当我需要它时,我现在无法从Sun获得javadoc(网络问题)。 > :( argh。


编辑:darnit,@ Richard的方法(在bbuf_src的支持数组中使用put())如果bbuf_src是ReadOnly缓冲区将无效,因为您无法访问该数组。在那种情况下我该怎么办?

3 个答案:

答案 0 :(得分:9)

正如您所发现的那样,获取后备阵列并不总是有效(对于只读缓冲区,直接缓冲区和内存映射文件缓冲区而言,它会失败)。更好的选择是复制源缓冲区并为要传输的数据量设置新的限制:

int maxTransfer = Math.min(bbuf_dest.remaining(), bbuf_src.remaining());
// use a duplicated buffer so we don't disrupt the limit of the original buffer
ByteBuffer bbuf_tmp = bbuf_src.duplicate ();
bbuf_tmp.limit (bbuf_tmp.position() + maxTransfer);
bbuf_dest.put (bbuf_tmp);

// now discard the data we've copied from the original source (optional)
bbuf_src.position(bbuf_src.position() + maxTransfer);

答案 1 :(得分:3)

好的,我已经改编了@ Richard的回答:

public static int transferAsMuchAsPossible(
                     ByteBuffer bbuf_dest, ByteBuffer bbuf_src)
{
  int nTransfer = Math.min(bbuf_dest.remaining(), bbuf_src.remaining());
  if (nTransfer > 0)
  {
    bbuf_dest.put(bbuf_src.array(), 
                  bbuf_src.arrayOffset()+bbuf_src.position(), 
                  nTransfer);
    bbuf_src.position(bbuf_src.position()+nTransfer);
  }
  return nTransfer;
}

并进行测试以确保其有效:

public static boolean transferTest()
{
    ByteBuffer bb1 = ByteBuffer.allocate(256);
    ByteBuffer bb2 = ByteBuffer.allocate(50);
    for (int i = 0; i < 100; ++i)
    {
        bb1.put((byte)i);
    }
    bb1.flip();
    bb1.position(5);
    ByteBuffer bb1a = bb1.slice();
    bb1a.position(2);
    // bb3 includes the 5-100 range
    bb2.put((byte)77);
    // something to see this works when bb2 isn't empty
    int n = transferAsMuchAsPossible(bb2, bb1a);
    boolean itWorked = (n == 49);

    if (bb1a.position() != 51)
        itWorked = false;
    if (bb2.position() != 50)
        itWorked = false;
    bb2.rewind();
    if (bb2.get() != 77)
        itWorked = false;
    for (int i = 0; i < 49; ++i)
    {
        if (bb2.get() != i+7)
        {
            itWorked = false;
            break;
        }
    }
    return itWorked;
}

答案 2 :(得分:1)

你得到BufferOverflowException,因为你的bbuf_dest不够大。

您需要使用bbuf_dest.remaining()来查找可以从bbuf_src传输的最大字节数:

int maxTransfer = Math.min(bbuf_dest.remaining(), bbuf_src.remaining());
bbuf_dest.put(bbuf_src.array(), 0, maxTransfer);