本文整理了Java中java.nio.ByteBuffer.capacity()
方法的一些代码示例,展示了ByteBuffer.capacity()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer.capacity()
方法的具体详情如下:
包路径:java.nio.ByteBuffer
类名称:ByteBuffer
方法名:capacity
暂无
代码示例来源:origin: libgdx/libgdx
IntToByteBufferAdapter (ByteBuffer byteBuffer) {
super((byteBuffer.capacity() >> 2));
this.byteBuffer = byteBuffer;
this.byteBuffer.clear();
}
代码示例来源:origin: google/guava
/**
* Flips the buffer output buffer so we can start reading bytes from it. If we are starting to
* drain because there was overflow, and there aren't actually any characters to drain, then the
* overflow must be due to a small output buffer.
*/
private void startDraining(boolean overflow) {
byteBuffer.flip();
if (overflow && byteBuffer.remaining() == 0) {
byteBuffer = ByteBuffer.allocate(byteBuffer.capacity() * 2);
} else {
draining = true;
}
}
代码示例来源:origin: apache/kafka
@Override
public ByteBuffer get(int minCapacity) {
if (cachedBuffer != null && cachedBuffer.capacity() >= minCapacity) {
ByteBuffer res = cachedBuffer;
cachedBuffer = null;
return res;
} else {
cachedBuffer = null;
return ByteBuffer.allocate(minCapacity);
}
}
代码示例来源:origin: apache/kafka
@Test(expected = IllegalArgumentException.class)
public void testWriteTransactionalNotAllowedMagicV1() {
ByteBuffer buffer = ByteBuffer.allocate(128);
buffer.position(bufferOffset);
long pid = 9809;
short epoch = 15;
int sequence = 2342;
new MemoryRecordsBuilder(buffer, RecordBatch.MAGIC_VALUE_V1, compressionType, TimestampType.CREATE_TIME,
0L, 0L, pid, epoch, sequence, true, false, RecordBatch.NO_PARTITION_LEADER_EPOCH, buffer.capacity());
}
代码示例来源:origin: apache/kylin
@Override
public void doMap(NullWritable key, Text value, Context context) throws IOException, InterruptedException {
tmpBuf.clear();
int size = value.getLength()+ 1;
if (size >= tmpBuf.capacity()) {
tmpBuf = ByteBuffer.allocate(countNewSize(tmpBuf.capacity(), size));
}
tmpBuf.put(Bytes.toBytes(index)[3]);
tmpBuf.put(value.getBytes(), 0, value.getLength());
outputKey.set(tmpBuf.array(), 0, tmpBuf.position());
sortableKey.init(outputKey, type);
context.write(sortableKey, NullWritable.get());
}
代码示例来源:origin: redisson/redisson
@Override
public void recycle() {
if (null != buffer) {
buffer.position(0);
position = 0;
limit = buffer.capacity();
buffer.limit(limit);
}
}
代码示例来源:origin: google/ExoPlayer
@Override
public void queueInput(ByteBuffer inputBuffer) {
Assertions.checkState(outputChannels != null);
int position = inputBuffer.position();
int limit = inputBuffer.limit();
int frameCount = (limit - position) / (2 * channelCount);
int outputSize = frameCount * outputChannels.length * 2;
if (buffer.capacity() < outputSize) {
buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder());
} else {
buffer.clear();
}
while (position < limit) {
for (int channelIndex : outputChannels) {
buffer.putShort(inputBuffer.getShort(position + 2 * channelIndex));
}
position += channelCount * 2;
}
inputBuffer.position(limit);
buffer.flip();
outputBuffer = buffer;
}
代码示例来源:origin: bumptech/glide
@Test
public void getOrientation_withExifSegmentAndPreambleButLessThanLength_returnsUnknown()
throws IOException {
ByteBuffer jpegHeaderBytes = getExifMagicNumber();
ByteBuffer exifSegmentPreamble =
ByteBuffer.wrap(DefaultImageHeaderParser.JPEG_EXIF_SEGMENT_PREAMBLE_BYTES);
ByteBuffer data = ByteBuffer.allocate(2 + 1 + 1 + 2 + exifSegmentPreamble.capacity());
data.put(jpegHeaderBytes)
.put((byte) DefaultImageHeaderParser.SEGMENT_START_ID)
.put((byte) DefaultImageHeaderParser.EXIF_SEGMENT_TYPE)
// SEGMENT_LENGTH, add two because length includes the segment length short, and one to go
// beyond the preamble bytes length for the test.
.putShort(
(short) (DefaultImageHeaderParser.JPEG_EXIF_SEGMENT_PREAMBLE_BYTES.length + 2 + 1))
.put(DefaultImageHeaderParser.JPEG_EXIF_SEGMENT_PREAMBLE_BYTES);
data.position(0);
DefaultImageHeaderParser parser = new DefaultImageHeaderParser();
assertEquals(ImageHeaderParser.UNKNOWN_ORIENTATION, parser.getOrientation(data, byteArrayPool));
}
代码示例来源:origin: google/ExoPlayer
@Override
public void queueInput(ByteBuffer buffer) {
int remaining = buffer.remaining();
if (remaining == 0) {
return;
}
audioBufferSink.handleBuffer(buffer.asReadOnlyBuffer());
if (this.buffer.capacity() < remaining) {
this.buffer = ByteBuffer.allocateDirect(remaining).order(ByteOrder.nativeOrder());
} else {
this.buffer.clear();
}
this.buffer.put(buffer);
this.buffer.flip();
outputBuffer = this.buffer;
}
代码示例来源:origin: apache/zookeeper
updateNow();
while (buf.isReadable()) {
if (incomingBuffer.remaining() > buf.readableBytes()) {
int newLimit = incomingBuffer.position()
+ buf.readableBytes();
incomingBuffer.limit(newLimit);
incomingBuffer.limit(incomingBuffer.capacity());
incomingBuffer.flip();
if (incomingBuffer == lenBuffer) {
recvCount.getAndIncrement();
} else if (!initialized) {
readConnectResult();
lenBuffer.clear();
incomingBuffer = lenBuffer;
initialized = true;
} else {
sendThread.readResponse(incomingBuffer);
lenBuffer.clear();
incomingBuffer = lenBuffer;
updateLastHeard();
代码示例来源:origin: neo4j/neo4j
private long flushFreeIds0( ByteBuffer writeBuffer ) throws IOException
{
channel.position( channel.size() );
writeBuffer.clear();
while ( !freeIds.isEmpty() )
{
long id = freeIds.dequeue();
if ( id == NO_RESULT )
{
continue;
}
writeBuffer.putLong( id );
if ( writeBuffer.position() == writeBuffer.capacity() )
{
writeBuffer.flip();
channel.writeAll( writeBuffer );
writeBuffer.clear();
}
}
writeBuffer.flip();
if ( writeBuffer.hasRemaining() )
{
channel.writeAll( writeBuffer );
}
return channel.position();
}
代码示例来源:origin: google/ExoPlayer
private void initData(int size) {
if (data == null || data.capacity() < size) {
data = ByteBuffer.allocateDirect(size);
} else {
data.position(0);
data.limit(size);
}
}
代码示例来源:origin: prestodb/presto
/**
* Wrap the visible portion of a {@link java.nio.ByteBuffer}.
*/
public static Slice wrappedBuffer(ByteBuffer buffer)
{
if (buffer.isDirect()) {
long address = getAddress(buffer);
return new Slice(null, address + buffer.position(), buffer.remaining(), buffer.capacity(), buffer);
}
if (buffer.hasArray()) {
return new Slice(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
}
throw new IllegalArgumentException("cannot wrap " + buffer.getClass().getName());
}
代码示例来源:origin: neo4j/neo4j
@Override
public void write( byte[] b, int off, int len ) throws IOException
{
int written = 0;
int index = off;
while ( written < len )
{
buffer.clear();
buffer.put( b, index + written, Math.min( len - written, buffer.capacity() ) );
buffer.flip();
written += channel.write( buffer );
}
}
代码示例来源:origin: apache/incubator-druid
@Override
public void reset()
{
// Clear the entire usedFlagBuffer
final int usedFlagBufferCapacity = usedFlagBuffer.capacity();
// putLong() instead of put() can boost the performance of clearing the buffer
final int n = (usedFlagBufferCapacity / Long.BYTES) * Long.BYTES;
for (int i = 0; i < n; i += Long.BYTES) {
usedFlagBuffer.putLong(i, 0L);
}
for (int i = n; i < usedFlagBufferCapacity; i++) {
usedFlagBuffer.put(i, (byte) 0);
}
}
代码示例来源:origin: apache/incubator-druid
@Override
public void writeTo(WritableByteChannel channel) throws IOException
{
checkOpen();
for (int i = 0; i <= headBufferIndex; i++) {
ByteBuffer buffer = buffers.get(i);
buffer.flip();
Channels.writeFully(channel, buffer);
// switch back to the initial state
buffer.limit(buffer.capacity());
}
}
代码示例来源:origin: apache/geode
private void createAndVerifyNewWriteBuffer(ByteBuffer buffer, boolean useDirectBuffer) {
buffer.position(buffer.capacity());
ByteBuffer newBuffer =
Buffers.expandWriteBufferIfNeeded(Buffers.BufferType.UNTRACKED, buffer, 500,
mock(DMStats.class));
assertEquals(buffer.position(), newBuffer.position());
assertEquals(500, newBuffer.capacity());
newBuffer.flip();
for (int i = 0; i < 256; i++) {
byte expected = (byte) (i & 0xff);
byte actual = (byte) (newBuffer.get() & 0xff);
assertEquals(expected, actual);
}
}
代码示例来源:origin: apache/kafka
/**
* Creates an instance of this class that will write to the received `buffer` up to its `limit`. If necessary to
* satisfy `write` or `position` calls, larger buffers will be allocated so the {@link #buffer()} method may return
* a different buffer than the received `buffer` parameter.
*
* Prefer one of the constructors that allocate the internal buffer for clearer semantics.
*/
public ByteBufferOutputStream(ByteBuffer buffer) {
this.buffer = buffer;
this.initialPosition = buffer.position();
this.initialCapacity = buffer.capacity();
}
代码示例来源:origin: google/ExoPlayer
/** Prepares to output {@code size} bytes in {@code buffer}. */
private void prepareForOutput(int size) {
if (buffer.capacity() < size) {
buffer = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
} else {
buffer.clear();
}
if (size > 0) {
hasOutputNoise = true;
}
}
代码示例来源:origin: neo4j/neo4j
@Override
public FlushableChannel put( byte[] value, int length ) throws IOException
{
int offset = 0;
while ( offset < length )
{
int chunkSize = min( length - offset, buffer.capacity() >> 1 );
bufferWithGuaranteedSpace( chunkSize ).put( value, offset, chunkSize );
offset += chunkSize;
}
return this;
}
内容来源于网络,如有侵权,请联系作者删除!