@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; }
long linearScanByPosition0(@NotNull final Wire wire, final long toPosition, long indexOfNext, while (bytes.readPosition() <= toPosition) { WireIn.HeaderType headerType; try { if (!inclusive && toPosition == bytes.readPosition()) return i; int header = bytes.readVolatileInt(bytes.readPosition()); throw new IllegalArgumentException( "You can't know the index for an entry which hasn't been written. " + "start: " + startAddress + ", at: " + bytes.readPosition() + ", header: " + Integer.toHexString(header) + ", toPos: " + toPosition); if (bytes.readPosition() == toPosition) return i; int len = Wires.lengthOf(header); assert Wires.isReady(header); bytes.readSkip(len); ".readPosition()=" + bytes.readPosition() + ",toPosition=" + toPosition);
@Override public void dump(@NotNull Writer writer, long fromIndex, long toIndex) { try { long firstIndex = firstIndex(); writer.append("# index: ").append(Long.toHexString(dc.index())).append("\n"); Wire wire = dc.wire(); long start = wire.bytes().readPosition(); try { text.clear(); wire.bytes().readPosition(start); writer.append(wire.bytes()).append("\n");
@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; } }
/** * @return a Bytes to wrap this ByteStore from the start() to the realCapacity(). * @throws IllegalStateException if this Bytes has been released. */ @Override @NotNull default Bytes<Underlying> bytesForRead() throws IllegalStateException { try { return bytesForWrite() .readLimit(writeLimit()) .readPosition(start()); } catch (BufferUnderflowException e) { throw new IllegalStateException(e); } }
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); }
@org.jetbrains.annotations.NotNull public static Bytes fromHexString(@org.jetbrains.annotations.NotNull String s) { try { Bytes in = Bytes.from(s); Bytes out = Bytes.elasticByteBuffer(); OUTER: while (in.readRemaining() > 0) { in.parseHexLong(); for (int i = 0; i < 16; i++) { if (in.peekUnsignedByte() == ' ') { in.readSkip(1); if (in.peekUnsignedByte() == ' ') break OUTER; } long value = in.parseHexLong(); out.writeByte((byte) value); } if (in.readByte(in.readPosition() - 1) <= ' ') in.readSkip(-1); in.skipTo(StopCharTesters.CONTROL_STOP); } return out; } catch (BufferUnderflowException | BufferOverflowException e) { throw new AssertionError(e); } }
@Nullable public static MessageHistory readHistory(final DocumentContext dc, MessageHistory history) { final Wire wire = dc.wire(); if (wire == null) return null; Object parent = wire.parent(); wire.parent(null); try { final Bytes<?> bytes = wire.bytes(); final byte code = bytes.readByte(bytes.readPosition()); history.reset(); return code == (byte) FIELD_NUMBER ? readHistoryFromBytes(wire, history) : readHistoryFromWire(wire, history); } finally { wire.parent(parent); } }
/** * The buffer is not modified by this call * * @param buffer the buffer to use * @param position the position to create the string from * @param len the number of characters to show in the string * @return a string contain the text from offset {@code position} */ static String toString(@NotNull final Bytes buffer, long position, long len) throws BufferUnderflowException { final long pos = buffer.readPosition(); final long limit = buffer.readLimit(); buffer.readPositionRemaining(position, len); try { @NotNull final StringBuilder builder = new StringBuilder(); while (buffer.readRemaining() > 0) { builder.append((char) buffer.readByte()); } // remove the last comma return builder.toString(); } finally { buffer.readLimit(limit); buffer.readPosition(pos); } }
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(); } finally { try { outBytes.readPosition(readPos); } catch (BufferUnderflowException e) {
private void inACycleFound(Bytes<?> bytes) { context.closeReadLimit(bytes.capacity()); wire().readAndSetLength(bytes.readPosition()); long end = bytes.readLimit(); context.closeReadPosition(end); Jvm.optionalSafepoint(); }
long moveToEnd(final Wire wire) { Sequence sequence1 = this.sequence; if (sequence1 != null) { for (int i = 0; i < 128; i++) { long endAddress = writePosition.getVolatileValue(); if (endAddress == 0) return -1; long sequence = sequence1.getSequence(endAddress); if (sequence == Sequence.NOT_FOUND_RETRY) continue; if (sequence == Sequence.NOT_FOUND) return -1; Bytes<?> bytes = wire.bytes(); bytes.readPosition(endAddress); for (; ; ) { int header = bytes.readVolatileInt(endAddress); if (header == 0 || Wires.isNotComplete(header)) return sequence; int len = Wires.lengthOf(header) + 4; bytes.readSkip(len); endAddress += len; if (Wires.isData(header)) sequence += 1; } } } return -1; }
public static long findWithinCycle(@NotNull Wire key, @NotNull Comparator<Wire> c, int cycle, @NotNull ExcerptTailer tailer, @NotNull SingleChronicleQueue q, @NotNull final RollCycle rollCycle) { final long readPosition = key.bytes().readPosition(); try { long lowSeqNum = 0; if (!dc.isPresent()) return -1; key.bytes().readPosition(readPosition); int cmp = c.compare(dc.wire(), key); key.bytes().readPosition(readPosition);
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)); } } }
@org.jetbrains.annotations.NotNull @NotNull public static Bytes asBytes(@org.jetbrains.annotations.NotNull @NotNull RandomDataOutput bytes, long position, long limit) throws IllegalStateException, BufferOverflowException, BufferUnderflowException { Bytes sbytes = bytes.bytesForWrite(); sbytes.writeLimit(limit); sbytes.readLimit(limit); sbytes.readPosition(position); return sbytes; }
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; 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);
/** * converts the bytes to a ISO-8859-1 String, the end of the string is either the bytes .limit * () or a byte containing the stopByte ( which ever comes first ). If the string can be * obtained from the pool, this string is used instead. otherwise, the string is added to the * pool. * * @param bytes the bytes to convert to a string * @param length parse the string up to the length * @return the string made from bytes only ( rather than chars ) */ public String intern(@NotNull final Bytes bytes, int length) { try { int hash32 = BytesStoreHash.hash32(bytes, length); int h = hash32 & mask; String s = interner[h]; long position = bytes.readPosition(); if (BytesUtil.bytesEqual(s, bytes, position, length)) return s; int h2 = (hash32 >> shift) & mask; String s2 = interner[h2]; if (BytesUtil.bytesEqual(s2, bytes, position, length)) return s2; char[] chars = toCharArray(bytes, position, length); return interner[s == null || (s2 != null && toggle()) ? h : h2] = StringUtils.newString(chars); } finally { bytes.readSkip(length); } } }
@Override public T getUsing(@Nullable T using) { try { T result = (T) new ObjectInputStream(in).readObject(); bytes.readPosition(0); return result; } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } }
public static String toHexString(@org.jetbrains.annotations.NotNull @NotNull final Bytes bytes, long offset, long len) throws BufferUnderflowException { if (len == 0) @org.jetbrains.annotations.NotNull int[] lastLine = new int[width]; @org.jetbrains.annotations.NotNull String sep = ""; long position = bytes.readPosition(); long limit = bytes.readLimit(); bytes.readLimit(limit); bytes.readPosition(position);
long linearScanByPosition0(@NotNull final Wire wire, final long toPosition, long indexOfNext, while (bytes.readPosition() <= toPosition) { WireIn.HeaderType headerType; try { if (!inclusive && toPosition == bytes.readPosition()) return i; int header = bytes.readVolatileInt(bytes.readPosition()); throw new IllegalArgumentException( "You can't know the index for an entry which hasn't been written. " + "start: " + startAddress + ", at: " + bytes.readPosition() + ", header: " + Integer.toHexString(header) + ", toPos: " + toPosition); if (bytes.readPosition() == toPosition) return i; int len = Wires.lengthOf(header); assert Wires.isReady(header); bytes.readSkip(len); ".readPosition()=" + bytes.readPosition() + ",toPosition=" + toPosition);