Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ public ByteBuffer[] toDirectByteBuffers(long offset, long size) {
int index = 0;
while (pos < limit) {
long blockLength = Math.min(limit - pos, blockSize);
result[index++] = UnsafeUtil.newDirectByteBuffer(address() + pos, (int) blockLength).order(ByteOrder.nativeOrder());
result[index++] = UnsafeUtil.newDirectByteBuffer(address() + pos, (int) blockLength, this)
.order(ByteOrder.nativeOrder());
pos += blockLength;
}
return result;
Expand All @@ -408,7 +409,7 @@ public ByteBuffer[] toDirectByteBuffers(long offset, long size) {
* @return
*/
public ByteBuffer toDirectByteBuffer(long offset, int size) {
return UnsafeUtil.newDirectByteBuffer(address() + offset, size);
return UnsafeUtil.newDirectByteBuffer(address() + offset, size, this);
}

protected long offset() {
Expand Down
16 changes: 8 additions & 8 deletions larray-buffer/src/main/java/xerial/larray/buffer/UnsafeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ public static Unsafe getUnsafe() {

private static Constructor<?> findDirectByteBufferConstructor() {
try {
return Class.forName("java.nio.DirectByteBuffer").getDeclaredConstructor(Long.TYPE, Integer.TYPE);
}
catch(ClassNotFoundException e) {
return Class.forName("java.nio.DirectByteBuffer").getDeclaredConstructor(Long.TYPE, Integer.TYPE, Object.class);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(String.format("Failed to find java.nio.DirectByteBuffer: $s", e.getMessage()));
}
catch(NoSuchMethodException e) {
} catch (NoSuchMethodException e) {
throw new IllegalStateException(String.format("Failed to find constructor f java.nio.DirectByteBuffer: $s", e.getMessage()));
}
}
Expand All @@ -46,14 +44,16 @@ private static Constructor<?> findDirectByteBufferConstructor() {
*
* @param addr
* @param size
* @param att object holding the underlying memory to attach to the buffer.
* This will prevent the garbage collection of the memory area that's
* associated with the new <code>DirectByteBuffer</code>
* @return
*/
public static ByteBuffer newDirectByteBuffer(long addr, int size)
{
public static ByteBuffer newDirectByteBuffer(long addr, int size, Object att) {
dbbCC.setAccessible(true);
Object b = null;
try {
b = dbbCC.newInstance(new Long(addr), new Integer(size));
b = dbbCC.newInstance(new Long(addr), new Integer(size), att);
return ByteBuffer.class.cast(b);
} catch (Exception e) {
throw new IllegalStateException(String.format("Failed to create DirectByteBuffer: %s", e.getMessage()));
Expand Down
4 changes: 2 additions & 2 deletions larray/src/main/scala/xerial/larray/LArray.scala
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ trait RawByteArray[A] extends LArray[A] {
*/
def writeToArray(srcOffset: Long, dest: Array[Byte], destOffset: Int, length: Int): Int = {
val writeLen = math.min(dest.length - destOffset, math.min(length, byteLength - srcOffset)).toInt
val b = xerial.larray.buffer.UnsafeUtil.newDirectByteBuffer(address + srcOffset, writeLen)
val b = xerial.larray.buffer.UnsafeUtil.newDirectByteBuffer(address + srcOffset, writeLen, this)
b.get(dest, destOffset, writeLen)
writeLen
}
Expand All @@ -725,7 +725,7 @@ trait RawByteArray[A] extends LArray[A] {
*/
def readFromArray(src: Array[Byte], srcOffset: Int, destOffset: Long, length: Int): Int = {
val readLen = math.min(src.length - srcOffset, math.min(byteLength - destOffset, length)).toInt
val b = xerial.larray.buffer.UnsafeUtil.newDirectByteBuffer(address + destOffset, readLen)
val b = xerial.larray.buffer.UnsafeUtil.newDirectByteBuffer(address + destOffset, readLen, this)
b.put(src, srcOffset, readLen)
readLen
}
Expand Down