org.apache.kafka.common.utils.Utils.readFullyOrFail()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(3.7k)|赞(0)|评价(0)|浏览(95)

本文整理了Java中org.apache.kafka.common.utils.Utils.readFullyOrFail()方法的一些代码示例,展示了Utils.readFullyOrFail()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utils.readFullyOrFail()方法的具体详情如下:
包路径:org.apache.kafka.common.utils.Utils
类名称:Utils
方法名:readFullyOrFail

Utils.readFullyOrFail介绍

[英]Read data from the channel to the given byte buffer until there are no bytes remaining in the buffer. If the end of the file is reached while there are bytes remaining in the buffer, an EOFException is thrown.
[中]将数据从通道读取到给定字节缓冲区,直到缓冲区中没有剩余字节。如果在缓冲区中剩余字节时到达文件末尾,则会引发EOFEException。

代码示例

代码示例来源:origin: apache/kafka

private RecordBatch loadBatchWithSize(int size, String description) {
  FileChannel channel = fileRecords.channel();
  try {
    ByteBuffer buffer = ByteBuffer.allocate(size);
    Utils.readFullyOrFail(channel, buffer, position, description);
    buffer.rewind();
    return toMemoryRecordBatch(buffer);
  } catch (IOException e) {
    throw new KafkaException("Failed to load record batch at position " + position + " from " + fileRecords, e);
  }
}

代码示例来源:origin: apache/kafka

@Override
public FileChannelRecordBatch nextBatch() throws IOException {
  FileChannel channel = fileRecords.channel();
  if (position >= end - HEADER_SIZE_UP_TO_MAGIC)
    return null;
  logHeaderBuffer.rewind();
  Utils.readFullyOrFail(channel, logHeaderBuffer, position, "log header");
  logHeaderBuffer.rewind();
  long offset = logHeaderBuffer.getLong(OFFSET_OFFSET);
  int size = logHeaderBuffer.getInt(SIZE_OFFSET);
  // V0 has the smallest overhead, stricter checking is done later
  if (size < LegacyRecord.RECORD_OVERHEAD_V0)
    throw new CorruptRecordException(String.format("Found record size %d smaller than minimum record " +
            "overhead (%d) in file %s.", size, LegacyRecord.RECORD_OVERHEAD_V0, fileRecords.file()));
  if (position > end - LOG_OVERHEAD - size)
    return null;
  byte magic = logHeaderBuffer.get(MAGIC_OFFSET);
  final FileChannelRecordBatch batch;
  if (magic < RecordBatch.MAGIC_VALUE_V2)
    batch = new LegacyFileChannelRecordBatch(offset, magic, fileRecords, position, size);
  else
    batch = new DefaultFileChannelRecordBatch(offset, magic, fileRecords, position, size);
  position += batch.sizeInBytes();
  return batch;
}

代码示例来源:origin: apache/kafka

/**
 * Tests that `readFullyOrFail` behaves correctly if multiple `FileChannel.read` operations are required to fill
 * the destination buffer.
 */
@Test
public void testReadFullyOrFailWithPartialFileChannelReads() throws IOException {
  FileChannel channelMock = mock(FileChannel.class);
  final int bufferSize = 100;
  ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
  String expectedBufferContent = fileChannelMockExpectReadWithRandomBytes(channelMock, bufferSize);
  Utils.readFullyOrFail(channelMock, buffer, 0L, "test");
  assertEquals("The buffer should be populated correctly", expectedBufferContent,
      new String(buffer.array()));
  assertFalse("The buffer should be filled", buffer.hasRemaining());
  verify(channelMock, atLeastOnce()).read(any(), anyLong());
}

代码示例来源:origin: apache/kafka

ByteBuffer largeBuffer = ByteBuffer.allocate(msg.length() + 1);
Utils.readFullyOrFail(channel, perfectBuffer, 0, "perfect");
assertFalse("Buffer should be filled up", perfectBuffer.hasRemaining());
assertEquals("Buffer should be populated correctly", msg, new String(perfectBuffer.array()));
Utils.readFullyOrFail(channel, smallBuffer, 0, "small");
assertFalse("Buffer should be filled", smallBuffer.hasRemaining());
assertEquals("Buffer should be populated correctly", "hello", new String(smallBuffer.array()));
Utils.readFullyOrFail(channel, smallBuffer, 7, "small");
assertFalse("Buffer should be filled", smallBuffer.hasRemaining());
assertEquals("Buffer should be populated correctly", "world", new String(smallBuffer.array()));
  Utils.readFullyOrFail(channel, largeBuffer, 0, "large");
  fail("Expected EOFException to be raised");
} catch (EOFException e) {

相关文章