net.openhft.chronicle.bytes.Bytes.wrapForRead()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(6.0k)|赞(0)|评价(0)|浏览(73)

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

Bytes.wrapForRead介绍

[英]Wrap the ByteBuffer ready for reading Method for convenience only - might not be ideal for performance (creates garbage). To avoid garbage, use something like this example:

import net.openhft.chronicle.bytes.Bytes;assert sb.toString().equals(HELLO_WORLD): "Failed - strings not equal!"; 
} 
private static void doWrite(Bytes b, ByteBuffer toWrite)  
//no garbage when writing to Bytes from ByteBuffer 
b.clear(); 
b.write(b.writePosition(), toWrite, toWrite.position(), toWrite.limit()); 
} 
private static ByteBuffer doRead(Bytes b)  
//no garbage when getting the underlying ByteBuffer 
assert b.underlyingObject() instanceof ByteBuffer; 
ByteBuffer byteBuffer = (ByteBuffer) b.underlyingObject(); 
return byteBuffer; 
} 
} 
}

[中]包装ByteBuffer ready for ready for reading方法仅为方便起见-可能对性能不太理想(创建垃圾)。要避免垃圾,请使用以下示例:

import net.openhft.chronicle.bytes.Bytes;assert sb.toString().equals(HELLO_WORLD): "Failed - strings not equal!"; 
} 
private static void doWrite(Bytes b, ByteBuffer toWrite)  
//no garbage when writing to Bytes from ByteBuffer 
b.clear(); 
b.write(b.writePosition(), toWrite, toWrite.position(), toWrite.limit()); 
} 
private static ByteBuffer doRead(Bytes b)  
//no garbage when getting the underlying ByteBuffer 
assert b.underlyingObject() instanceof ByteBuffer; 
ByteBuffer byteBuffer = (ByteBuffer) b.underlyingObject(); 
return byteBuffer; 
} 
} 
}

代码示例

代码示例来源:origin: OpenHFT/Chronicle-Queue

@Test
public void testReadingLessBytesThanWritten() {
  try (final ChronicleQueue queue = builder(getTmpDir(), wireType)
      .build()) {
    final ExcerptAppender appender = queue.acquireAppender();
    final Bytes<byte[]> expected = Bytes.wrapForRead("some long message".getBytes(ISO_8859_1));
    for (int i = 0; i < 10; i++) {
      appender.writeBytes(expected);
    }
    final ExcerptTailer tailer = queue.createTailer();
    // Sequential read
    for (int i = 0; i < 10; i++) {
      Bytes b = Bytes.allocateDirect(8);
      tailer.readBytes(b);
      Assert.assertEquals(expected.readInt(0), b.readInt(0));
      b.release();
    }
  }
}

代码示例来源:origin: OpenHFT/Chronicle-Queue

@Test//(timeout = 5000)
public void testTailerReadingEmptyQueue() {
  assumeFalse(Jvm.isArm());
  final File path = DirectoryUtils.tempDir("testTailerReadingEmptyQueue");
  final ChronicleQueue rqueue = SingleChronicleQueueBuilder.fieldlessBinary(path)
      .testBlockSize()
      .rollCycle(TEST_DAILY)
      .build();
  final ExcerptTailer tailer = rqueue.createTailer();
  final ChronicleQueue wqueue = SingleChronicleQueueBuilder.fieldlessBinary(path)
      .testBlockSize()
      .rollCycle(TEST_DAILY)
      .build();
  Bytes bytes = Bytes.elasticByteBuffer();
  assertFalse(tailer.readBytes(bytes));
  final ExcerptAppender appender = wqueue.acquireAppender();
  appender.writeBytes(Bytes.wrapForRead("Hello World".getBytes(ISO_8859_1)));
  bytes.clear();
  assertTrue(tailer.readBytes(bytes));
  assertEquals("Hello World", bytes.toString());
  bytes.release();
}

代码示例来源:origin: net.openhft/chronicle-bytes

static Bytes<byte[]> fromString(@NotNull String text) throws IllegalArgumentException, IllegalStateException {
  return wrapForRead(text.getBytes(StandardCharsets.ISO_8859_1));
}

代码示例来源:origin: OpenHFT/Chronicle-TimeSeries

public InMemoryDoubleColumn(TimeSeries timeSeries, String name, BytesDoubleLookup lookup, long capacity) {
    super(timeSeries, name);
    this.lookup = lookup;
//        this.bytes = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(lookup.sizeFor(capacity));
    this.bytes = Bytes.wrapForRead(ByteBuffer.allocateDirect(Math.toIntExact(lookup.sizeFor(capacity))));
  }

代码示例来源:origin: net.openhft/chronicle-bytes

@NotNull
public static NativeBytesStore from(@NotNull byte[] bytes) {
  try {
    @NotNull NativeBytesStore nbs = nativeStore(bytes.length);
    Bytes.wrapForRead(bytes).copyTo(nbs);
    return nbs;
  } catch (IllegalArgumentException e) {
    throw new AssertionError(e);
  }
}

代码示例来源:origin: net.openhft/chronicle-bytes

/**
 * Convert text to bytes using ISO-8859-1 encoding and return a Bytes ready for reading.
 *
 * @param text to convert
 * @return Bytes ready for reading.
 */
static Bytes<byte[]> from(@NotNull CharSequence text) {
  if (text instanceof BytesStore)
    return ((BytesStore) text).copy().bytesForRead();
  return wrapForRead(text.toString().getBytes(StandardCharsets.ISO_8859_1));
}

代码示例来源:origin: net.openhft/chronicle-bytes

public static Bytes readFile(@org.jetbrains.annotations.NotNull String name) throws IOException {
  File file = new File(name);
  URL url = null;
  if (!file.exists()) {
    url = urlFor(name);
    file = new File(url.getFile());
  }
  return // name.endsWith(".gz") || !file.exists() || OS.isWindows() ?
      Bytes.wrapForRead(readAsBytes(url == null ? new FileInputStream(file) : open(url)));
  //: MappedFile.readOnly(file).acquireBytesForRead(0);
}

代码示例来源:origin: OpenHFT/Chronicle-TimeSeries

public InMemoryLongColumn(TimeSeries timeSeries, String name, BytesLongLookup lookup, long capacity) {
  super(timeSeries, name);
  this.lookup = lookup;
  long value = lookup.sizeFor(capacity);
  this.bytes = Jvm.isDebug()
      ? Bytes.wrapForRead(ByteBuffer.allocateDirect(Math.toIntExact(value)))
      : NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(value);
}

代码示例来源:origin: OpenHFT/Chronicle-TimeSeries

@Override
  public void ensureCapacity(long capacity) {
    long cap = lookup.sizeFor(capacity);
    if (cap > bytes.realCapacity()) {
//            BytesStore bytes2 = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(Maths.divideRoundUp(cap, OS.pageSize()));
      BytesStore bytes2 = Bytes.wrapForRead(ByteBuffer.allocateDirect(Math.toIntExact(lookup.sizeFor(capacity))));
      bytes2.write(0, bytes);
      bytes.release();
      bytes = bytes2;
    }
  }

代码示例来源:origin: OpenHFT/Chronicle-TimeSeries

@Override
public void ensureCapacity(long capacity) {
  long cap = lookup.sizeFor(capacity);
  if (cap > bytes.realCapacity()) {
    long value = lookup.sizeFor(capacity);
    BytesStore bytes2 = Jvm.isDebug()
        ? Bytes.wrapForRead(ByteBuffer.allocateDirect(Math.toIntExact(value)))
        : NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(value);
    bytes2.write(0, bytes);
    bytes.release();
    bytes = bytes2;
  }
}

代码示例来源:origin: net.openhft/chronicle-map

Bytes<ByteBuffer> headerBytes = Bytes.wrapForRead(headerBuffer);
headerBytes.readPosition(headerBuffer.position());
headerBytes.readLimit(headerBuffer.limit());

相关文章

微信公众号

最新文章

更多