@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); }
@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(); } }
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(); }
@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; }
@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; }
@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);
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);
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);
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);
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); }
@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; } }
@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; }
@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; } }
@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; }
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)); } } }
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.");
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); }