System.arraycopy(...) equivalents for ByteBuffer/FileChannel

Jeff Hain jeffhain at rocketmail.com
Wed Oct 10 14:12:55 PDT 2012


Hello.

This is a request for treatments addition.

I am currently building something where I do various sorts
of byte copies between ByteBuffers, between FileChannels,
and between each other, possibly in a concurrent way.

Not to pollute high level code with low level considerations,
and deal similarly with ByteBuffers and FileChannels, I'm making
System.arraycopy(...) equivalents for these types of copies
(with ByteBuffer/limit()/int <-> FileChannel/size()/long).

I also would like these copies to be efficient, i.e. native,
but don't want to resort to reflection or Unsafe.

Unfortunately, JDK's treatments (*) don't allow for efficient
implementations for all possible combinations.
Especially, most of them make use of {src|dst}'s position
and update it, which does not allow for concurrents reads or
writes.

For example, if wanting to copy bytes from a concurrently-read
DirectByteBuffer (can't touch its position), to another ByteBuffer,
one can only do a byte-loop (unless using disk as temp).
 

If would be great to have such System.arraycopy(...)
equivalents available from some java.nio class(es).
 

(*) these:

- System.arraycopy(
      srcBB.array(),srcPos,
      dstBB.array(),dstPos,n)
  (concurrent)

- ByteBuffer.put(srcBB)
  (not concurrent, fast if dst is direct)

- ByteBuffer.put(srcBB.array(),srcPos,n)
  (concurrent for src, fast if dst is direct)

- ByteBuffer.get(dstBB.array(),dstPos,n)
  (concurrent for dst, fast if src is direct)

- FileChannel.read(dstBB)
  (not concurrent, faster if dst is direct,
  still fast otherwise)

- FileChannel.read(dstBB,srcPos)
  (concurrent for src, faster if dst is direct,
  still fast otherwise)

- FileChannel.write(srcBB)
  (not concurrent, faster if src is direct,
  still fast otherwise)

- FileChannel.write(srcBB,dstPos)
  (concurrent for dst, faster if src is direct,
  still fast otherwise)

- FileChannel.transferTo(srcPos,n,dstWBC)
  (concurrent for src, might use kernel's copy,
  else map/write, else tmp/read/write)

- FileChannel.transferFrom(srcRBC,dstPos,n)
  (concurrent for dst, uses map/write,
  else tmp/read/write)
 

-Jeff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20121010/53d35cb2/attachment.html 


More information about the nio-dev mailing list