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

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

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

Bytes.writePosition介绍

暂无

代码示例

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

/**
 * @param marshallable to write to excerpt.
 */
@Override
public void writeBytes(@NotNull WriteBytesMarshallable marshallable) throws UnrecoverableTimeoutException {
  try (DocumentContext dc = writingDocument()) {
    Bytes<?> bytes = dc.wire().bytes();
    long wp = bytes.writePosition();
    marshallable.writeMarshallable(bytes);
    if (wp == bytes.writePosition())
      dc.rollbackOnClose();
    else if (padToCacheAlignMode() == Padding.WORD)
      ((StoreAppenderContext) dc).padToWordAlign = true;
    else if (padToCacheAlignMode() != Padding.CACHE_LINE)
      ((StoreAppenderContext) dc).padToCacheAlign = false;
  }
}

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

/**
 * Creates a new Excerpt containing and index which will be 1L << 17L bytes long, This method is
 * used for creating both the primary and secondary indexes. Chronicle Queue uses a root primary
 * index ( each entry in the primary index points to a unique a secondary index. The secondary
 * index only records the addressForRead of every 64th except, the except are linearly scanned from
 * there on.  )
 *
 * @param wire the current wire
 * @return the addressForRead of the Excerpt containing the usable index, just after the header
 */
long newIndex(@NotNull WireOut wire, boolean index2index) throws StreamCorruptedException {
  long writePosition = this.writePosition.getVolatileValue();
  Bytes<?> bytes = wire.bytes();
  bytes.writePosition(writePosition);
  long position = wire.enterHeader(indexCount * 8 + 128);
  WriteMarshallable writer = index2index ? index2IndexTemplate : indexTemplate;
  writer.writeMarshallable(wire);
  wire.updateHeader(position, true, 0);
  return position;
}

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

@Override
public long recoverAndWriteHeader(@NotNull Wire wire, long timeoutMS, final LongValue lastPosition, Sequence sequence) throws UnrecoverableTimeoutException {
  Bytes<?> bytes = wire.bytes();
  long offset = bytes.writePosition();
  int num = bytes.readVolatileInt(offset);
    long pos = bytes.writePosition();
    try {
      bytes.writeSkip(4);
        wire.getValueOut().text(stackTrace);
      wire.addPadding(Math.toIntExact(sizeToSkip + (pos + 4) - bytes.writePosition()));
    } finally {
      bytes.writePosition(pos);

代码示例来源: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);
}

代码示例来源: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: net.openhft/chronicle-bytes

public static void write(@NotNull Bytes bytes, int value) throws BufferOverflowException {
  long position = bytes.writePosition();
  bytes.write(template);
  try {
    bytes.append(position + VALUE, value, DIGITS);
  } catch (IllegalArgumentException e) {
    throw new AssertionError(e);
  }
}

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

public static void write(@NotNull Bytes bytes, long value) throws BufferOverflowException, IllegalArgumentException {
  long position = bytes.writePosition();
  bytes.write(template);
  bytes.append(position + VALUE, value, DIGITS);
}

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

public static void write(@NotNull Bytes bytes, long capacity) throws BufferOverflowException, IllegalArgumentException {
  assert (bytes.writePosition() & 0x7) == 0;
  bytes.writeLong(capacity);
  bytes.writeLong(0L); // used
  long start = bytes.writePosition();
  bytes.zeroOut(start, start + (capacity << 3));
  bytes.writeSkip(capacity << 3);
}

代码示例来源: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-bytes

public static void lazyWrite(@NotNull Bytes bytes, long capacity) throws BufferOverflowException {
  assert (bytes.writePosition() & 0x7) == 0;
  bytes.writeLong(capacity);
  bytes.writeLong(0L); // used
  bytes.writeSkip(capacity << 3);
}

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

public UncheckedBytes(@NotNull Bytes underlyingBytes) throws IllegalStateException {
  super(underlyingBytes.bytesStore(), underlyingBytes.writePosition(), underlyingBytes.writeLimit());
  this.underlyingBytes = underlyingBytes;
  readPosition(underlyingBytes.readPosition());
}

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

default void cipher(@NotNull Cipher cipher, @NotNull Bytes outBytes, @NotNull ByteBuffer using1, @NotNull ByteBuffer using2) throws IllegalStateException {
  long readPos = outBytes.readPosition();
  try {
    long writePos = outBytes.writePosition();
    BytesStore inBytes;
    long size = readRemaining();
    len += cipher.doFinal(using1, using2);
    assert len == using2.position();
    outBytes.writePosition(writePos + using2.position());
  } catch (@NotNull Exception e) {

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

public UncheckedNativeBytes(@NotNull Bytes<Underlying> underlyingBytes)
    throws IllegalStateException {
  this.underlyingBytes = underlyingBytes;
  this.bytesStore = (NativeBytesStore<Underlying>) underlyingBytes.bytesStore();
  assert bytesStore.start() == 0;
  writePosition = underlyingBytes.writePosition();
  writeLimit = underlyingBytes.writeLimit();
  readPosition = underlyingBytes.readPosition();
  capacity = bytesStore.capacity();
}

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

/**
 * @param marshallable to write to excerpt.
 */
@Override
public void writeBytes(@NotNull WriteBytesMarshallable marshallable) throws UnrecoverableTimeoutException {
  try (DocumentContext dc = writingDocument()) {
    Bytes<?> bytes = dc.wire().bytes();
    long wp = bytes.writePosition();
    marshallable.writeMarshallable(bytes);
    if (wp == bytes.writePosition())
      dc.rollbackOnClose();
    else if (padToCacheAlignMode() == Padding.WORD)
      ((StoreAppenderContext) dc).padToWordAlign = true;
    else if (padToCacheAlignMode() != Padding.CACHE_LINE)
      ((StoreAppenderContext) dc).padToCacheAlign = false;
  }
}

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

static Bytes<ByteBuffer> wrapForWrite(@NotNull ByteBuffer byteBuffer) {
  BytesStore<?, ByteBuffer> bs = BytesStore.wrap(byteBuffer);
  try {
    Bytes<ByteBuffer> bbb = bs.bytesForWrite();
    bbb.writePosition(byteBuffer.position());
    bbb.writeLimit(byteBuffer.limit());
    return bbb;

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

public static void write(@NotNull Bytes bytes, long capacity) {
  long start = bytes.writePosition();
  bytes.write(SECTION1);
  bytes.append(capacity);
  while (bytes.writePosition() - start < CAPACITY + DIGITS) {
    bytes.writeUnsignedByte(' ');
  }
  bytes.write(SECTION2);
  bytes.write(ZERO);
  bytes.write(SECTION3);
  for (long i = 0; i < capacity; i++) {
    if (i > 0)
      bytes.appendUtf8(", ");
    bytes.write(ZERO);
  }
  bytes.write(SECTION4);
}

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

/**
 * write and exceptions and rolls back if no data was written
 */
void writeData(boolean isNotComplete, @NotNull Bytes inBytes, @NotNull WriteMarshallable c) {
  @NotNull final WriteMarshallable marshallable = out -> {
    final long readPosition = inBytes.readPosition();
    final long position = outWire.bytes().writePosition();
    try {
      c.writeMarshallable(outWire);
    } catch (Throwable t) {
      inBytes.readPosition(readPosition);
      if (LOG.isInfoEnabled())
        LOG.info("While reading " + inBytes.toDebugString(),
            " processing wire " + c, t);
      outWire.bytes().writePosition(position);
      outWire.writeEventName(() -> "exception").throwable(t);
    }
    // write 'reply : {} ' if no data was sent
    if (position == outWire.bytes().writePosition()) {
      outWire.writeEventName(reply).marshallable(EMPTY);
    }
  };
  if (isNotComplete)
    outWire.writeNotCompleteDocument(false, marshallable);
  else
    outWire.writeDocument(false, marshallable);
  logYaml();
}

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

/**
 * Creates a new Excerpt containing and index which will be 1L << 17L bytes long, This method is
 * used for creating both the primary and secondary indexes. Chronicle Queue uses a root primary
 * index ( each entry in the primary index points to a unique a secondary index. The secondary
 * index only records the addressForRead of every 64th except, the except are linearly scanned from
 * there on.  )
 *
 * @param wire the current wire
 * @return the addressForRead of the Excerpt containing the usable index, just after the header
 */
long newIndex(@NotNull WireOut wire, boolean index2index) throws StreamCorruptedException {
  long writePosition = this.writePosition.getVolatileValue();
  Bytes<?> bytes = wire.bytes();
  bytes.writePosition(writePosition);
  long position = wire.enterHeader(indexCount * 8 + 128);
  WriteMarshallable writer = index2index ? index2IndexTemplate : indexTemplate;
  writer.writeMarshallable(wire);
  wire.updateHeader(position, true, 0);
  return position;
}

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

/**
 * write and exceptions and rolls back if no data was written
 */
void writeData(@NotNull WireIn wireIn, @NotNull WriteMarshallable c) {
  @NotNull Bytes inBytes = wireIn.bytes();
  outWire.writeDocument(false, out -> {
    final long readPosition = inBytes.readPosition();
    final long position = outWire.bytes().writePosition();
    try {
      c.writeMarshallable(outWire);
    } catch (Throwable t) {
      final String readingYaml = wireIn.readingPeekYaml();
      inBytes.readPosition(readPosition);
      if (LOG.isInfoEnabled())
        LOG.info("While readingBytes=" + inBytes.toDebugString() + "\nreadingYaml=" +
                readingYaml,
            "\nprocessing wire " + c, t);
      outWire.bytes().writePosition(position);
      outWire.writeEventName(() -> "readingYaml").text(readingYaml);
      outWire.writeEventName(() -> "exception").throwable(t);
    }
    // write 'reply : {} ' if no data was sent
    if (position == outWire.bytes().writePosition()) {
      outWire.writeEventName(reply).marshallable(EMPTY);
    }
  });
  logYaml();
}

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

@SuppressWarnings("resource")
@Override
public long recoverAndWriteHeader(@NotNull Wire wire, long timeoutMS, final LongValue lastPosition, Sequence sequence) throws UnrecoverableTimeoutException {
  Bytes<?> bytes = wire.bytes();
  long offset = bytes.writePosition();
  int num = bytes.readVolatileInt(offset);
    long pos = bytes.writePosition();
    try {
      bytes.writeSkip(4);
        wire.getValueOut().text(stackTrace);
      wire.addPadding(Math.toIntExact(sizeToSkip + (pos + 4) - bytes.writePosition()));
    } finally {
      bytes.writePosition(pos);

相关文章

微信公众号

最新文章

更多