@NotNull @Override public CharSequence read(Bytes bytes, @Nullable CharSequence ignored) { long size = bytes.readStopBit(); if (size == NULL_LENGTH) return null; if (size < 0 || size > Integer.MAX_VALUE) throw new IllegalStateException("Invalid length: " + size); if (size == 0) return ""; char[] chars = new char[(int) size]; int length = bytes.readInt(); long limit = bytes.readLimit(); try { bytes.readLimit(bytes.readPosition() + length); DataInputStream dis = new DataInputStream( new BufferedInputStream(new InflaterInputStream(bytes.inputStream()))); for (int i = 0; i < size; i++) chars[i] = (char) (dis.readByte() & 0xff); } catch (IOException e) { throw new IORuntimeException(e); } finally { bytes.readPosition(bytes.readLimit()); bytes.readLimit(limit); } try { return STRING_FACTORY.fromChars(chars); } catch (Exception e) { throw new AssertionError(e); } }
public boolean equalsBytes(@NotNull Bytes b2, long remaining) { long i = 0; try { for (; i < remaining - 7; i++) if (readLong(readPosition() + i) != b2.readLong(b2.readPosition() + i)) return false; for (; i < remaining; i++) if (readByte(readPosition() + i) != b2.readByte(b2.readPosition() + i)) return false; } catch (BufferUnderflowException | IORuntimeException e) { throw Jvm.rethrow(e); } return true; }
public static boolean readData( @NotNull WireIn wireIn, @Nullable ReadMarshallable metaDataConsumer, @Nullable ReadMarshallable dataConsumer) { final Bytes<?> bytes = wireIn.bytes(); boolean read = false; while (bytes.readRemaining() >= 4) { long position = bytes.readPosition(); int header = bytes.readVolatileInt(position); if (!isKnownLength(header)) return read; bytes.readSkip(4); final boolean ready = Wires.isReady(header); final int len = Wires.lengthOf(header); if (Wires.isData(header)) { if (dataConsumer == null) { return false; } else { ((InternalWireIn) wireIn).setReady(ready); bytes.readWithLength(len, b -> dataConsumer.readMarshallable(wireIn)); return true; } } else { if (metaDataConsumer == null) { // skip the header bytes.readSkip(len); } else { // bytes.readWithLength(len, b -> metaDataConsumer.accept(wireIn)); // inlined to avoid garbage if ((long) len > bytes.readRemaining()) throw new BufferUnderflowException(); long limit0 = bytes.readLimit(); long limit = bytes.readPosition() + (long) len; try { bytes.readLimit(limit); metaDataConsumer.readMarshallable(wireIn); } finally { bytes.readLimit(limit0); bytes.readPosition(limit); } } if (dataConsumer == null) return true; read = true; } } return read; }
public static boolean readData( long offset, @NotNull WireIn wireIn, @Nullable ReadMarshallable metaDataConsumer, @Nullable ReadMarshallable dataConsumer) { final Bytes bytes = wireIn.bytes(); long position = bytes.readPosition(); long limit = bytes.readLimit(); try { bytes.readLimit(bytes.isElastic() ? bytes.capacity() : bytes.realCapacity()); bytes.readPosition(offset); return readData(wireIn, metaDataConsumer, dataConsumer); } finally { bytes.readLimit(limit); bytes.readPosition(position); } }
public UncheckedNativeBytes(@NotNull Bytes<Underlying> underlyingBytes) throws IllegalStateException { underlyingBytes.reserve(); this.bytesStore = (NativeBytesStore<Underlying>) underlyingBytes.bytesStore(); assert bytesStore.start() == 0; writePosition = underlyingBytes.writePosition(); writeLimit = underlyingBytes.writeLimit(); readPosition = underlyingBytes.readPosition(); capacity = bytesStore.capacity(); }
/** * 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, IORuntimeException { final long pos = buffer.readPosition(); final long limit = buffer.readLimit(); buffer.readPosition(position); buffer.readLimit(position + len); try { 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); } }
@Override public void process( @NotNull final Bytes in, @NotNull final Bytes out, final SessionDetailsProvider sessionDetails) { if (in.readRemaining() == 0) return; // System.out.println("P start " + in.toDebugString()); long toWrite = Math.min(in.readRemaining(), out.writeRemaining()); out.write( (net.openhft.chronicle.bytes.BytesStore) in, (long) in.readPosition(), (long) toWrite); in.readSkip(toWrite); // System.out.println("... P End " + in.toDebugString()); }
public static void rawReadData(@NotNull WireIn wireIn, @NotNull ReadMarshallable dataConsumer) { final Bytes<?> bytes = wireIn.bytes(); int header = bytes.readInt(); assert Wires.isReady(header) && Wires.isData(header); final int len = Wires.lengthOf(header); long limit0 = bytes.readLimit(); long limit = bytes.readPosition() + (long) len; try { bytes.readLimit(limit); dataConsumer.readMarshallable(wireIn); } finally { bytes.readLimit(limit0); } }
@NotNull @Override public B writeByte(long offset, byte i8) throws AssertionError { try { byte i8a = underlyingBytesStore.readByte(offset); if (i8a != i8) { try { Bytes<Underlying> bytes = underlyingBytesStore.bytesForRead(); bytes.readPosition(offset); throw new AssertionError( bytes.toDebugString() + "\nExpected: " + i8a + "\nActual: " + i8); } catch (IllegalStateException e) { throw new AssertionError(e); } } return (B) this; } catch (BufferUnderflowException | IORuntimeException e) { throw new AssertionError(e); } }
@NotNull static String fromSizePrefixedBlobs(@NotNull Bytes bytes, long position, long length) { StringBuilder sb = new StringBuilder(); final long limit0 = bytes.readLimit(); final long position0 = bytes.readPosition(); try { bytes.readPosition(position); long limit2 = Math.min(limit0, position + length); bytes.readLimit(limit2); long missing = position + length - limit2; while (bytes.readRemaining() >= 4) { long header = bytes.readUnsignedInt(); int len = Wires.lengthOf(header); if (len > bytes.readRemaining()) throw new RuntimeException( "Are you sure this was written with writeDocument and has a 4 byte size prefix, " + len + " > " + bytes.readRemaining()); String type = Wires.isData(header) ? Wires.isReady(header) ? "!!data" : "!!not-ready-data!" : Wires.isReady(header) ? "!!meta-data" : "!!not-ready-meta-data!"; boolean binary = bytes.readByte(bytes.readPosition()) < ' '; sb.append("--- ").append(type).append(binary ? " #binary" : ""); if (missing > 0) sb.append(" # missing: ").append(missing); if (len > bytes.readRemaining()) sb.append(" # len: ").append(len).append(", remaining: ").append(bytes.readRemaining()); sb.append("\n"); Bytes textBytes = bytes; if (binary) { Bytes bytes2 = Bytes.elasticByteBuffer(); TextWire textWire = new TextWire(bytes2); long readLimit = bytes.readLimit(); try { bytes.readLimit(bytes.readPosition() + len); new BinaryWire(bytes).copyTo(textWire); } finally { bytes.readLimit(readLimit); } textBytes = bytes2; len = (int) textBytes.readRemaining(); } try { for (int i = 0; i < len; i++) { int ch = textBytes.readUnsignedByte(); sb.append((char) ch); } } catch (Exception e) { sb.append(" ").append(e); } if (sb.charAt(sb.length() - 1) != '\n') sb.append('\n'); } return sb.toString(); } finally { bytes.readLimit(limit0); bytes.readPosition(position0); } }
@Stage("Segment") public Bytes segmentBytesForWrite() { segmentBytes.readPosition(0); return segmentBytes; }