本文整理汇总了Java中java.nio.ByteBuffer.isDirect方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.isDirect方法的具体用法?Java ByteBuffer.isDirect怎么用?Java ByteBuffer.isDirect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.isDirect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: read
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public int read(ByteBuffer buffer, long position)
{
int size = 0;
byte[] temBuff = null;
try {
if (buffer.isDirect()) {
//TODO: have a better way to allocate this using thread local or pooling
temBuff = new byte[buffer.capacity()];
size = read(position, temBuff, 0, buffer.limit());
buffer.put(temBuff, 0, size);
} else {
temBuff = buffer.array();
size = read(position, temBuff, 0, buffer.limit());
buffer.limit(buffer.capacity());
buffer.position(size);
}
return size;
} catch (IOException e) {
throw new FSReadError(e, filePath.getName());
}
}
示例2: writeToChannel
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Writes a {@link java.nio.ByteBuffer} to a {@link java.nio.channels.WritableByteChannel}
*
* @param byteBuffer source buffer
* @param channel channel to write to
*/
public static void writeToChannel(ByteBuffer byteBuffer, WritableByteChannel channel) throws IOException {
if (byteBuffer.isDirect() || (byteBuffer.remaining() <= WRITE_CHUNK_SIZE)) {
while (byteBuffer.hasRemaining()) {
channel.write(byteBuffer);
}
} else {
// duplicate the buffer in order to be able to change the limit
ByteBuffer tmpBuffer = byteBuffer.duplicate();
try {
while (byteBuffer.hasRemaining()) {
tmpBuffer.limit(Math.min(byteBuffer.limit(), tmpBuffer.position() + WRITE_CHUNK_SIZE));
while (tmpBuffer.hasRemaining()) {
channel.write(tmpBuffer);
}
byteBuffer.position(tmpBuffer.position());
}
} finally {
// make sure we update byteBuffer to indicate how far we came..
byteBuffer.position(tmpBuffer.position());
}
}
}
示例3: setBytes
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static void setBytes(long address, ByteBuffer buffer)
{
int start = buffer.position();
int count = buffer.limit() - start;
if (count == 0)
return;
if (buffer.isDirect())
setBytes(((DirectBuffer)buffer).address() + start, address, count);
else
setBytes(address, buffer.array(), buffer.arrayOffset() + start, count);
}
示例4: read
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public ByteBuffer read(ByteBuffer byteBuffer, int offset,
int length, long max_wait_time)
throws IOException
{
int size = offset + length;
if (shouldUseDirectByteBuffers()) {
if (! byteBuffer.isDirect()) {
throw wrapper.unexpectedNonDirectByteBufferWithChannelSocket();
}
if (size > byteBuffer.capacity()) {
if (orb.transportDebugFlag) {
// print address of ByteBuffer being released
int bbAddress = System.identityHashCode(byteBuffer);
StringBuffer bbsb = new StringBuffer(80);
bbsb.append(".read: releasing ByteBuffer id (")
.append(bbAddress).append(") to ByteBufferPool.");
String bbmsg = bbsb.toString();
dprint(bbmsg);
}
orb.getByteBufferPool().releaseByteBuffer(byteBuffer);
byteBuffer = orb.getByteBufferPool().getByteBuffer(size);
}
byteBuffer.position(offset);
byteBuffer.limit(size);
readFully(byteBuffer, length, max_wait_time);
byteBuffer.position(0);
byteBuffer.limit(size);
return byteBuffer;
}
if (byteBuffer.isDirect()) {
throw wrapper.unexpectedDirectByteBufferWithNonChannelSocket();
}
byte[] buf = new byte[size];
readFully(getSocket().getInputStream(), buf,
offset, length, max_wait_time);
return ByteBuffer.wrap(buf);
}
示例5: nextShallowCopy
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Creates a new KeyValue object positioned in the supplied ByteBuffer and sets the ByteBuffer's
* position to the start of the next KeyValue. Does not allocate a new array or copy data.
* @param bb
* @param includesMvccVersion
* @param includesTags
*/
public static KeyValue nextShallowCopy(final ByteBuffer bb, final boolean includesMvccVersion,
boolean includesTags) {
if (bb.isDirect()) {
throw new IllegalArgumentException("only supports heap buffers");
}
if (bb.remaining() < 1) {
return null;
}
KeyValue keyValue = null;
int underlyingArrayOffset = bb.arrayOffset() + bb.position();
int keyLength = bb.getInt();
int valueLength = bb.getInt();
ByteBufferUtils.skip(bb, keyLength + valueLength);
int tagsLength = 0;
if (includesTags) {
// Read short as unsigned, high byte first
tagsLength = ((bb.get() & 0xff) << 8) ^ (bb.get() & 0xff);
ByteBufferUtils.skip(bb, tagsLength);
}
int kvLength = (int) KeyValue.getKeyValueDataStructureSize(keyLength, valueLength, tagsLength);
keyValue = new KeyValue(bb.array(), underlyingArrayOffset, kvLength);
if (includesMvccVersion) {
long mvccVersion = ByteBufferUtils.readVLong(bb);
keyValue.setSequenceId(mvccVersion);
}
return keyValue;
}
示例6: read
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public int read(ByteBuffer dst) throws IOException {
refCount.reference();
boolean exc = true;
try {
int nread = 0;
if (dst.isDirect()) {
nread = DomainSocket.readByteBufferDirect0(DomainSocket.this.fd,
dst, dst.position(), dst.remaining());
} else if (dst.hasArray()) {
nread = DomainSocket.readArray0(DomainSocket.this.fd,
dst.array(), dst.position() + dst.arrayOffset(),
dst.remaining());
} else {
throw new AssertionError("we don't support " +
"using ByteBuffers that aren't either direct or backed by " +
"arrays");
}
if (nread > 0) {
dst.position(dst.position() + nread);
}
exc = false;
return nread;
} finally {
unreference(exc);
}
}
示例7: release
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public void release(ByteBuffer bb) {
if (bb.isDirect()) {
try {
delegate().release(bb);
} catch (IOException ioe) {
throw new InternalError(ioe);
}
}
}
示例8: checkAllDirect
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Used for assertions. Returns false if one of the buffers is not direct
* @param buffers
* @return
*/
private final boolean checkAllDirect(final ByteBuffer buffers[]) {
for (final ByteBuffer b : buffers ) {
if (!b.isDirect()) {
return false;
}
}
return true;
}
示例9: update
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Starts or continues a SHA-1 message digest calculation.
* Only the remaining bytes of the given ByteBuffer are used.
* @param buffer input data
*/
public void update(ByteBuffer buffer) {
length += buffer.remaining();
//Save current position to leave given buffer unchanged
int position = buffer.position();
//Complete the final buffer if needed
completeFinalBuffer(buffer);
if(!buffer.hasArray() || buffer.isDirect())
{
if(cacheBlock == null) // only allocate if we process direct byte buffers
cacheBlock = new byte[cacheSize];
while(buffer.remaining() >= 64)
{
int toProcess = Math.min(buffer.remaining()-buffer.remaining()%64,cacheSize);
buffer.get(cacheBlock, 0, toProcess);
for(int i = 0;i < toProcess; i+=64)
transform(cacheBlock,i);
}
} else // use direct array access for heap buffers
{
final int endPos = buffer.position()+buffer.remaining()-buffer.remaining()%64;
final int internalEndPos = endPos+buffer.arrayOffset();
for(int i = buffer.arrayOffset()+buffer.position();i < internalEndPos;i+=64)
transform(buffer.array(),i);
buffer.position(endPos);
}
if(buffer.remaining() != 0) {
finalBuffer.put(buffer);
}
buffer.position(position);
}
示例10: putInt
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Put an int value out to the specified ByteBuffer offset in big-endian format.
*
* @param buf the ByteBuffer to write to
* @param offset offset in the ByteBuffer
* @param val int to write out
* @return incremented offset
*/
public static int putInt(ByteBuffer buf, int offset, int val) {
if (littleEndian) {
val = Integer.reverseBytes(val);
}
if (buf.isDirect()) {
theUnsafe.putInt(((DirectBuffer) buf).address() + offset, val);
} else {
theUnsafe.putInt(buf.array(), offset + buf.arrayOffset() + BYTE_ARRAY_BASE_OFFSET, val);
}
return offset + Bytes.SIZEOF_INT;
}
示例11: toByte
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Returns the byte at the given offset
*
* @param buf the buffer to read
* @param offset the offset at which the byte has to be read
* @return the byte at the given offset
*/
public static byte toByte(ByteBuffer buf, int offset) {
if (buf.isDirect()) {
return theUnsafe.getByte(((DirectBuffer) buf).address() + offset);
} else {
return theUnsafe.getByte(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset);
}
}
示例12: copy
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Copies specified number of bytes from given offset of {@code src} ByteBuffer to the
* {@code dest} array.
*
* @param src
* @param srcOffset
* @param dest
* @param destOffset
* @param length
*/
public static void copy(ByteBuffer src, int srcOffset, byte[] dest, int destOffset, int length) {
long srcAddress = srcOffset;
Object srcBase = null;
if (src.isDirect()) {
srcAddress = srcAddress + ((DirectBuffer) src).address();
} else {
srcAddress = srcAddress + BYTE_ARRAY_BASE_OFFSET + src.arrayOffset();
srcBase = src.array();
}
long destAddress = destOffset + BYTE_ARRAY_BASE_OFFSET;
unsafeCopy(srcBase, srcAddress, dest, destAddress, length);
}
示例13: putByte
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Put a byte value out to the specified BB position in big-endian format.
*
* @param buf the byte buffer
* @param offset position in the buffer
* @param b byte to write out
* @return incremented offset
*/
public static int putByte(ByteBuffer buf, int offset, byte b) {
if (buf.isDirect()) {
theUnsafe.putByte(((DirectBuffer) buf).address() + offset, b);
} else {
theUnsafe.putByte(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, b);
}
return offset + 1;
}
示例14: readWithBounceBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Read using the bounce buffer.
*
* A 'direct' read actually has three phases. The first drains any
* remaining bytes from the slow read buffer. After this the read is
* guaranteed to be on a checksum chunk boundary. If there are still bytes
* to read, the fast direct path is used for as many remaining bytes as
* possible, up to a multiple of the checksum chunk size. Finally, any
* 'odd' bytes remaining at the end of the read cause another slow read to
* be issued, which involves an extra copy.
*
* Every 'slow' read tries to fill the slow read buffer in one go for
* efficiency's sake. As described above, all non-checksum-chunk-aligned
* reads will be served from the slower read path.
*
* @param buf The buffer to read into.
* @param canSkipChecksum True if we can skip checksums.
*/
private synchronized int readWithBounceBuffer(ByteBuffer buf,
boolean canSkipChecksum) throws IOException {
int total = 0;
int bb = drainDataBuf(buf); // drain bounce buffer if possible
if (bb >= 0) {
total += bb;
if (buf.remaining() == 0) return total;
}
boolean eof = true, done = false;
do {
if (buf.isDirect() && (buf.remaining() >= maxReadaheadLength)
&& ((dataPos % bytesPerChecksum) == 0)) {
// Fast lane: try to read directly into user-supplied buffer, bypassing
// bounce buffer.
int oldLimit = buf.limit();
int nRead;
try {
buf.limit(buf.position() + maxReadaheadLength);
nRead = fillBuffer(buf, canSkipChecksum);
} finally {
buf.limit(oldLimit);
}
if (nRead < maxReadaheadLength) {
done = true;
}
if (nRead > 0) {
eof = false;
}
total += nRead;
} else {
// Slow lane: refill bounce buffer.
if (fillDataBuf(canSkipChecksum)) {
done = true;
}
bb = drainDataBuf(buf); // drain bounce buffer if possible
if (bb >= 0) {
eof = false;
total += bb;
}
}
} while ((!done) && (buf.remaining() > 0));
return (eof && total == 0) ? -1 : total;
}
示例15: decode
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public void decode(ByteBuffer[] inputs, int[] erasedIndexes,
ByteBuffer[] outputs) {
checkParameters(inputs, erasedIndexes, outputs);
ByteBuffer validInput = CoderUtil.findFirstValidInput(inputs);
boolean usingDirectBuffer = validInput.isDirect();
int dataLen = validInput.remaining();
if (dataLen == 0) {
return;
}
checkParameterBuffers(inputs, true, dataLen, usingDirectBuffer, false);
checkParameterBuffers(outputs, false, dataLen, usingDirectBuffer, true);
int[] inputPositions = new int[inputs.length];
for (int i = 0; i < inputPositions.length; i++) {
if (inputs[i] != null) {
inputPositions[i] = inputs[i].position();
}
}
if (usingDirectBuffer) {
doDecode(inputs, erasedIndexes, outputs);
} else {
int[] inputOffsets = new int[inputs.length];
int[] outputOffsets = new int[outputs.length];
byte[][] newInputs = new byte[inputs.length][];
byte[][] newOutputs = new byte[outputs.length][];
ByteBuffer buffer;
for (int i = 0; i < inputs.length; ++i) {
buffer = inputs[i];
if (buffer != null) {
inputOffsets[i] = buffer.arrayOffset() + buffer.position();
newInputs[i] = buffer.array();
}
}
for (int i = 0; i < outputs.length; ++i) {
buffer = outputs[i];
outputOffsets[i] = buffer.arrayOffset() + buffer.position();
newOutputs[i] = buffer.array();
}
doDecode(newInputs, inputOffsets, dataLen,
erasedIndexes, newOutputs, outputOffsets);
}
for (int i = 0; i < inputs.length; i++) {
if (inputs[i] != null) {
// dataLen bytes consumed
inputs[i].position(inputPositions[i] + dataLen);
}
}
}