/** * @return a temporary per thread instance of ByteBuffer pointing to this structs data. The length of the * buffer is same as the length of this struct. * * The instance returned is valid until the currentThread calls this method next time. * * Can be used to feed e.g. NIO api. Use ByteBuffer.duplicate() to obtain a non-temporary instance * * @throws NoSuchFieldException * @throws IllegalAccessException */ public ByteBuffer asByteBufferTemporary() { if ( getBase() instanceof MallocBytez ) { MallocBytez base = (MallocBytez) getBase(); ByteBuffer bb = tmpBuf.get(); try { address.setLong(bb, base.getBaseAdress() + getOffset()); capacity.setInt( bb, getByteSize()); } catch (IllegalAccessException e) { FSTUtil.<RuntimeException>rethrow(e); } bb.limit((int) (getOffset() + getByteSize())); bb.position((int) getOffset()); return tmpBuf.get(); } else { // assume HeapBytez. Allocates return ByteBuffer.wrap(getBase().asByteArray(), (int) getOffset(), getByteSize()); } }
@Override public void copyTo(BasicBytez other, long otherByteIndex, long myByteIndex, long lenBytes) { if ( lenBytes == 0 ) return; checkIndex(myByteIndex, (int) lenBytes); if ( other instanceof HeapBytez) { HeapBytez hp = (HeapBytez) other; unsafe.copyMemory(base,off+myByteIndex,hp.base,hp.off+otherByteIndex,lenBytes); } else if (other instanceof MallocBytez ) { MallocBytez mb = (MallocBytez) other; unsafe.copyMemory(base,off+myByteIndex,null,otherByteIndex+mb.getBaseAdress(),lenBytes); } else { for ( long i = 0; i < lenBytes; i++ ) { other.put(otherByteIndex+i,get(myByteIndex+i)); } } }
/** * @return a temporary per thread instance of ByteBuffer pointing to this structs data. The length of the * buffer is same as the length of this struct. * * The instance returned is valid until the currentThread calls this method next time. * * Can be used to feed e.g. NIO api. Use ByteBuffer.duplicate() to obtain a non-temporary instance * * @throws NoSuchFieldException * @throws IllegalAccessException */ public ByteBuffer asByteBufferTemporary() { if ( getBase() instanceof MallocBytez ) { MallocBytez base = (MallocBytez) getBase(); ByteBuffer bb = tmpBuf.get(); try { address.setLong(bb, base.getBaseAdress() + getOffset()); capacity.setInt( bb, getByteSize()); } catch (IllegalAccessException e) { FSTUtil.<RuntimeException>rethrow(e); } bb.limit((int) (getOffset() + getByteSize())); bb.position((int) getOffset()); return tmpBuf.get(); } else { // assume HeapBytez. Allocates return ByteBuffer.wrap(getBase().asByteArray(), (int) getOffset(), getByteSize()); } }
@Override public void copyTo(BasicBytez other, long otherByteIndex, long myByteIndex, long lenBytes) { if ( lenBytes == 0 ) return; checkIndex(myByteIndex, (int) lenBytes); if ( other instanceof HeapBytez) { HeapBytez hp = (HeapBytez) other; unsafe.copyMemory(base,off+myByteIndex,hp.base,hp.off+otherByteIndex,lenBytes); } else if (other instanceof MallocBytez ) { MallocBytez mb = (MallocBytez) other; unsafe.copyMemory(base,off+myByteIndex,null,otherByteIndex+mb.getBaseAdress(),lenBytes); } else { for ( long i = 0; i < lenBytes; i++ ) { other.put(otherByteIndex+i,get(myByteIndex+i)); } } }
private void initTmpBBuf() throws NoSuchFieldException, IllegalAccessException { // patch MallocBytes such that it points to a DirectByteBuffer (allows zerocopy using fst bytez utils) tmpSend = ByteBuffer.allocateDirect(0); Field address = null; Field capacity = null; List<Field> fields = new ArrayList<>(); FSTUtil.getAllFields(fields, tmpSend.getClass()); for (int i = 0; i < fields.size(); i++) { Field field = fields.get(i); if ( field.getName().equals("address") ) { address = field; } else if ( field.getName().equals("capacity") ) { capacity = field; } } address.setAccessible(true); capacity.setAccessible(true); MallocBytez base = (MallocBytez) history.getBase(); address.setLong(tmpSend, base.getBaseAdress() + history.getOffset()); capacity.setInt(tmpSend, history.getByteSize()); }
private void initTmpBBuf() throws NoSuchFieldException, IllegalAccessException { // patch MallocBytes such that it points to a DirectByteBuffer (allows zerocopy using fst bytez utils) tmpSend = ByteBuffer.allocateDirect(0); Field address = null; Field capacity = null; List<Field> fields = new ArrayList<>(); FSTUtil.getAllFields(fields, tmpSend.getClass()); for (int i = 0; i < fields.size(); i++) { Field field = fields.get(i); if ( field.getName().equals("address") ) { address = field; } else if ( field.getName().equals("capacity") ) { capacity = field; } } address.setAccessible(true); capacity.setAccessible(true); MallocBytez base = (MallocBytez) history.getBase(); address.setLong(tmpSend, base.getBaseAdress() + history.getOffset()); capacity.setInt(tmpSend, history.getByteSize()); }