net.openhft.chronicle.bytes.Bytes类的使用及代码示例

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

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

Bytes介绍

[英]Bytes is a pointer to a region of memory within a BytesStore. It can be for a fixed region of memory or an "elastic" buffer which can be resized, but not for a fixed region.

This is a BytesStore which is mutable and not thread safe. It has a write position and read position which must follow these constraints

start() <= readPosition() <= writePosition() <= writeLimit() <= capacity()

Also readLimit() == writePosition() and readPosition() <= safeLimit();
[中]字节是指向字节存储中内存区域的指针。它可以用于固定的内存区域,也可以用于可调整大小的“弹性”缓冲区,但不能用于固定区域。
这是一个ByteStore,它是可变的,并且不是线程安全的。它有一个写位置和读位置,必须遵循这些约束
开始()<=readPosition()<=writePosition()<=writeLimit()<=capacity()
另外还有readLimit()==writePosition()和readPosition()<=safeLimit();

代码示例

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

@Override
public void accept(final WireIn wireIn, final Consumer<String> messageHandler) {
  final Bytes<?> serialisedMessage = wireIn.bytes();
  final byte dataFormatIndicator = serialisedMessage.readByte(serialisedMessage.readPosition());
  String text;
  if (isBinaryFormat(dataFormatIndicator)) {
    textConversionTarget.clear();
    final BinaryWire binaryWire = new BinaryWire(serialisedMessage);
    binaryWire.copyTo(wireType.apply(textConversionTarget));
    text = textConversionTarget.toString();
  } else {
    text = serialisedMessage.toString();
  }
  messageHandler.accept(text);
}

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

@Override
public void writeBytes(@NotNull BytesStore bytes) throws UnrecoverableTimeoutException {
  writeLock.lock();
  try {
    int cycle = queue.cycle();
    if (this.cycle != cycle || wire == null)
      rollCycleTo(cycle);
    position(writeHeader(wire, (int) queue.overlapSize()));
    assert ((AbstractWire) wire).isInsideHeader();
    beforeAppend(wire, wire.headerNumber() + 1);
    Bytes<?> wireBytes = wire.bytes();
    wireBytes.write(bytes);
    if (padToCacheLines == Padding.WORD)
      wireBytes.writeSkip((-wireBytes.writePosition()) & 0x3);
    wire.updateHeader(position, false, 0);
    lastIndex(wire.headerNumber());
    lastPosition = position;
    lastCycle = cycle;
    store.writePosition(position);
    writeIndexForPosition(lastIndex, position);
  } catch (StreamCorruptedException e) {
    throw new AssertionError(e);
  } finally {
    writeLock.unlock();
  }
}

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

@NotNull
private Wire readAnywhere(@NotNull Wire wire) {
  Bytes<?> bytes = wire.bytes();
  bytes.readLimit(bytes.capacity());
  return wire;
}

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

private void doRollback(boolean interrupted) {
  if (interrupted)
    LOG.warn("Thread is interrupted. Can't guarantee complete message, so not committing");
  // zero out all contents...
  for (long i = position; i <= wire.bytes().writePosition(); i++)
    wire.bytes().writeByte(i, (byte) 0);
  position = lastPosition;
  wire.bytes().writePosition(position);
  ((AbstractWire) wire).forceNotInsideHeader();
}

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

@NotNull
private ScanResult moveToIndexFromTheStart(@NotNull ExcerptContext ec, long index) {
  try {
    Wire wire = ec.wire();
    wire.bytes().readPositionUnlimited(0);
    if (wire.readDataHeader())
      return linearScan(wire, index, 0, wire.bytes().readPosition());
  } catch (EOFException fallback) {
    return ScanResult.END_OF_FILE;
  }
  return ScanResult.NOT_FOUND;
}

代码示例来源:origin: oracle/opengrok

@NotNull
@Override
@SuppressWarnings("rawtypes")
public BytesRef read(Bytes in, long size, @Nullable BytesRef using) {
  if (size < 0L || size > (long) Integer.MAX_VALUE) {
    throw new IORuntimeException("byte[] size should be non-negative int, " +
        size + " given. Memory corruption?");
  }
  int arrayLength = (int) size;
  if (using == null) {
    using = new BytesRef(new byte[arrayLength]);
  } else if (using.bytes.length < arrayLength) {
    using.bytes = new byte[arrayLength];
  }
  in.read(using.bytes, 0, arrayLength);
  using.offset = 0;
  using.length = arrayLength;
  return using;
}

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

@NotNull @SuppressWarnings("ConstantConditions")
final ValueIn valueIn = inWire.readEventName(eventName);
    @NotNull final ValueOut valueOut = out.writeEventName(CoreFields.reply);
    valueOut.sequence(v -> underlyingCollection.forEach(e -> toWire.accept(v, e)));
    return;
Jvm.warn().on(getClass(), e);
  assert outWire.startUse();
  try {
    @NotNull final Bytes<?> outBytes = outWire.bytes();
    long len = outBytes.writePosition();
    if (len >= SIZE_OF_SIZE) {
      String s = Wires.fromSizePrefixedBlobs((Wire) outWire);

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

@NotNull
@Override
public Long read(Bytes in, @Nullable Long using) {
  return in.readLong();
}

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

private void writeToFile(@NotNull Path path, @NotNull BytesStore value) {
  BytesStore<?, ByteBuffer> writingBytes;
  if (value.underlyingObject() instanceof ByteBuffer) {
    writingBytes = value;
  } else {
    Buffers b = Buffers.BUFFERS.get();
    Bytes<ByteBuffer> valueBuffer = b.valueBuffer;
    valueBuffer.clear();
    valueBuffer.write(value);
    writingBytes = valueBuffer;
  @NotNull File tmpFile = new File(file.getParentFile(), "." + file.getName() + "." + System.nanoTime());
  try (@NotNull FileChannel fc = new FileOutputStream(tmpFile).getChannel()) {
    @Nullable ByteBuffer byteBuffer = writingBytes.underlyingObject();
    byteBuffer.position(0);
    byteBuffer.limit((int) writingBytes.readLimit());
    fc.write(byteBuffer);
  } catch (IOException e) {
        Jvm.debug().on(getClass(), "Unable to rename file " + fse);
      try {
        Thread.sleep(i * i * 2);

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

private void onRead0(@NotNull DocumentContext inDc, @NotNull WireOut out, @NotNull WireIn in) {
  if (!YamlLogging.showHeartBeats()) {
    prevLogMessage.append(currentLogMessage);
    currentLogMessage.setLength(0);
    logToBuffer(in, currentLogMessage, in.bytes().readPosition() - 4);
  } else {
        Jvm.debug().on(getClass(), "received data:\n" + in.bytes().toHexString());
      @NotNull Consumer<WireType> wireTypeConsumer = wt -> {
      @Nullable Map<String, UserStat> userMonitoringMap = getMonitoringMap();
      if (userMonitoringMap != null) {
        UserStat userStat = userMonitoringMap.get(sessionDetails.userId());
      Jvm.warn().on(getClass(), in.readingPeekYaml() + "/n" + in.bytes().toDebugString(),
          e);

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

private static void readUtf8_SB1(
    @org.jetbrains.annotations.NotNull @NotNull Bytes bytes, @org.jetbrains.annotations.NotNull @NotNull StringBuilder appendable, @org.jetbrains.annotations.NotNull @NotNull StopCharTester tester)
    throws IOException, BufferUnderflowException {
  @org.jetbrains.annotations.Nullable NativeBytesStore nb = (NativeBytesStore) bytes.bytesStore();
  int i = 0, len = Maths.toInt32(bytes.readRemaining());
  long address = nb.address + nb.translate(bytes.readPosition());
  @org.jetbrains.annotations.Nullable Memory memory = nb.memory;
  if (Jvm.isJava9Plus()) {
    int appendableLength = appendable.capacity();
    for (; i < len && i < appendableLength; i++) {
        break;
      if (tester.isStopChar(c)) {
        bytes.readSkip(i + 1);
        StringUtils.setCount(appendable, i);
        return;
        break;
      if (tester.isStopChar(c)) {
        bytes.readSkip(i + 1);
        StringUtils.setCount(appendable, i);
        return;
  bytes.readSkip(i);
  if (i < len) {
    readUtf8_SB2(bytes, appendable, tester);

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

public static void parse8bit_SB1(@org.jetbrains.annotations.NotNull @NotNull Bytes bytes, @org.jetbrains.annotations.NotNull @NotNull StringBuilder sb, int utflen)
    throws BufferUnderflowException {
  if (utflen > bytes.readRemaining())
    throw new BufferUnderflowException();
  @Nullable NativeBytesStore nbs = (NativeBytesStore) bytes.bytesStore();
  long offset = bytes.readPosition();
  int count = BytesInternal.parse8bit_SB1(offset, nbs, sb, utflen);
  bytes.readSkip(count);
}

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

@Nullable
@Override
public String getAndPut(String key, @NotNull String value) {
  Buffers b = BUFFERS.get();
  Bytes<ByteBuffer> bytes = b.valueBuffer;
  bytes.clear();
  bytes.appendUtf8(value);
  @Nullable BytesStore retBytes = kvStore.getAndPut(key, bytes);
  if (retBytes == null) return null;
  else {
    String s = retBytes.toString();
    retBytes.release();
    return s;
  }
}

代码示例来源:origin: com.wavefront/proxy

@NotNull
@Override
public AgentDigest read(Bytes in, long size, @Nullable AgentDigest using) {
 Preconditions.checkArgument(size >= FIXED_SIZE);
 short compression = in.readShort();
 if (using == null || using.compression != compression) {
  using = new AgentDigest(compression, in.readLong());
 } else {
  using.dispatchTimeMillis = in.readLong();
 }
 using.totalWeight = 0d;
 using.lastUsedCell = (int) ((size - FIXED_SIZE) / PER_CENTROID_SIZE);
 using.tempUsed = 0;
 using.unmergedWeight = 0D;
 // need explicit nulling of weight past lastUsedCell
 Arrays.fill(using.weight, using.lastUsedCell, using.weight.length, 0D);
 for (int i = 0; i < using.lastUsedCell; ++i) {
  float weight = in.readFloat();
  using.weight[i] = weight;
  using.mean[i] = in.readFloat();
  using.totalWeight += weight;
 }
 return using;
}

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

@NotNull
  @Override
  public final T read(@NotNull Bytes in, long size, @Nullable T using) {
    if (using == null)
      using = createInstance();
    using.bytesStore(in.bytesStore(), in.readPosition(), size);
    return using;
  }
}

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

@NotNull
@Override
public Boolean read(Bytes in, @Nullable Boolean using) {
  return in.readByte() != 0;
}

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

@NotNull
public Bytes acquireBytesForWrite(long position)
    throws IOException, IllegalStateException, IllegalArgumentException {
  @Nullable MappedBytesStore mbs = acquireByteStore(position);
  @NotNull Bytes bytes = mbs.bytesForWrite();
  bytes.writePosition(position);
  mbs.release();
  return bytes;
}

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

private void readMarshallable(String path, @NotNull WireIn wire) {
  @NotNull StringBuilder name = new StringBuilder();
  while (!wire.isEmpty()) {
    @NotNull ValueIn in = wire.read(name);
    long pos = wire.bytes().readPosition();
    @NotNull String path2 = path + "/" + name;
    if (wire.getValueIn().isTyped()) {
      wire.bytes().readPosition(pos);
      @Nullable Object o = in.typedMarshallable();
      installableMap.put(path2, (Installable) o);
    } else {
      in.marshallable(w -> this.readMarshallable(path2, w));
    }
  }
}

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

long offset = bytes.writePosition();
int num = bytes.readVolatileInt(offset);
  long pos = bytes.writePosition();
  try {
    bytes.writeSkip(4);
    final String debugMessage = "!! Skipped due to recovery of locked header !! By thread " +
        Thread.currentThread().getName() + ", pid " + OS.getProcessId();
    wire.getValueOut().text(debugMessage);
    final StringWriter stackVisitor = new StringWriter();
    new RuntimeException().printStackTrace(new PrintWriter(stackVisitor));
      wire.getValueOut().text(stackTrace);
    wire.addPadding(Math.toIntExact(sizeToSkip + (pos + 4) - bytes.writePosition()));
  } finally {
    bytes.writePosition(pos);
  if (bytes.compareAndSwapInt(offset, num, emptyMetaData)) {
    warn().on(getClass(), msgStart + " switching to a corrupt meta data message");
    bytes.writeSkip(sizeToSkip + 4);
  } else {
    int num2 = bytes.readVolatileInt(offset);
    warn().on(getClass(), msgStart + " already set to " + Integer.toHexString(num2));
  warn().on(getClass(), msgStart + " but message now exists.");

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

private long writeHeader(@NotNull Wire wire, int safeLength) {
  Bytes<?> bytes = wire.bytes();
  // writePosition points at the last record in the queue, so we can just skip it and we're ready for write
  long pos = position;
  long lastPos = store.writePosition();
  if (pos < lastPos) {
    // queue moved since we last touched it - recalculate header number
    try {
      wire.headerNumber(queue.rollCycle().toIndex(cycle, store.lastSequenceNumber(this)));
    } catch (StreamCorruptedException ex) {
      Jvm.warn().on(getClass(), "Couldn't find last sequence", ex);
    }
  }
  int header = bytes.readVolatileInt(lastPos);
  assert header != NOT_INITIALIZED;
  lastPos += lengthOf(bytes.readVolatileInt(lastPos)) + SPB_HEADER_SIZE;
  bytes.writePosition(lastPos);
  return wire.enterHeader(safeLength);
}

相关文章

微信公众号

最新文章

更多