Java Code Examples for net.openhft.chronicle.bytes.Bytes#readByte()

The following examples show how to use net.openhft.chronicle.bytes.Bytes#readByte() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FixSaxParser.java    From SAXophone with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void parse(Bytes bytes) {
    long limit = bytes.readLimit(), limit2 = limit;
    while (limit2 > bytes.readPosition() && bytes.readByte(limit2 - 1) != FIELD_TERMINATOR)
        limit2--;
    bytes.readLimit(limit2);

    while (bytes.readRemaining() > 0) {
        long fieldNum = bytes.parseLong();
        long pos = bytes.readPosition();

        searchForTheEndOfField(bytes);

        long end = bytes.readPosition() - 1;
        bytes.readLimit(end);
        bytes.readPosition(pos);
        handler.completeMessage(bytes);
        handler.onField(fieldNum, bytes);

        bytes.readLimit(limit);
        bytes.readPosition(end + 1);
    }

    bytes.readLimit(limit);
    bytes.readPosition(limit2);
}
 
Example 2
Source File: ReplicatedInput.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
public void processReplicatedEvent(byte remoteNodeIdentifier, Bytes replicatedInputBytes) {
    long timestamp = replicatedInputBytes.readStopBit();
    byte identifier = replicatedInputBytes.readByte();
    ru.initReplicationUpdate(identifier, timestamp, remoteNodeIdentifier);

    boolean isDeleted = replicatedInputBytes.readBoolean();
    long keySize = mh.m().keySizeMarshaller.readSize(replicatedInputBytes);
    long keyOffset = replicatedInputBytes.readPosition();

    q.initInputKey(q.getInputKeyBytesAsData(replicatedInputBytes, keyOffset, keySize));
    replicatedInputBytes.readSkip(keySize);
    if (isDeleted) {
        s.innerUpdateLock.lock();
        mh.m().remoteOperations.remove(this);
    } else {
        long valueSize = mh.m().valueSizeMarshaller.readSize(replicatedInputBytes);
        long valueOffset = replicatedInputBytes.readPosition();
        Data<V> value = q.wrapValueBytesAsData(replicatedInputBytes, valueOffset, valueSize);
        replicatedInputBytes.readSkip(valueSize);
        s.innerWriteLock.lock();
        mh.m().remoteOperations.put(this, value);
    }
}
 
Example 3
Source File: MessageToTextQueueEntryHandler.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 4
Source File: Ed25519Test.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
@Test
public void signAndVerify() {
    final String SIGN_PRIVATE = "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd";

    Bytes publicKey = bytesWithZeros(32);
    Bytes secretKey = bytesWithZeros(64);
    Bytes privateKey = fromHex(SIGN_PRIVATE);
    Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey);
    assertEquals(32, publicKey.readRemaining());
    assertEquals(64, secretKey.readRemaining());

    Bytes emptyMsg = bytesWithZeros(0);
    Bytes sigAndMsg0 = bytesWithZeros(ED25519_SECRETKEY_BYTES);
    Bytes sigAndMsg = bytesWithZeros(ED25519_SECRETKEY_BYTES);

    Ed25519.sign(sigAndMsg0, emptyMsg, secretKey);

    assertEquals(0, SODIUM.crypto_sign_ed25519(sigAndMsg.addressForWrite(0), new LongLongByReference(0), emptyMsg.addressForRead(0), 0,
            secretKey.addressForRead(0)));

    sigAndMsg.readPositionRemaining(0, 64);

    System.out.println(publicKey.toHexString());
    System.out.println(privateKey.toHexString());
    System.out.println(secretKey.toHexString());
    assertEquals(sigAndMsg.toHexString(), sigAndMsg0.toHexString());
    checkZeros(emptyMsg);
    checkZeros(secretKey);
    checkZeros(sigAndMsg);

    Bytes buffer = bytesWithZeros(ED25519_SECRETKEY_BYTES);

    assertTrue(Ed25519.verify(sigAndMsg, publicKey));
    for (int i = 0; i < sigAndMsg.readRemaining(); i++) {
        byte old = sigAndMsg.readByte(i);
        sigAndMsg.writeByte(i, old ^ 1);
        assertFalse(Ed25519.verify(sigAndMsg, publicKey));
        sigAndMsg.writeByte(i, old);
    }

    assertEquals(0, SODIUM.crypto_sign_ed25519_open(buffer.addressForWrite(0), new LongLongByReference(0), sigAndMsg.addressForRead(0), 0 + 64,
            publicKey.addressForRead(0)));
    sigAndMsg.readPositionRemaining(0, 64);
    System.out.println(sigAndMsg.toHexString());

}
 
Example 5
Source File: WireTcpHandler.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
@Override
public void process(@NotNull final Bytes in, @NotNull final Bytes out, final T nc) {

    if (isClosed())
        return;

    WireType wireType = wireType();
    if (wireType == null)
        wireType = in.readByte(in.readPosition() + 4) < 0 ? WireType.BINARY : WireType.TEXT;

    checkWires(in, out, wireType);

    // we assume that if any bytes were in lastOutBytesRemaining the sc.write() would have been
    // called and this will fail, if the other end has lost its connection
    if (outWire.bytes().writePosition() != lastWritePosition) {
        onBytesWritten();

        // NOTE you can not use remaining as the buffer maybe resized
        writeBps += (lastWritePosition - outWire.bytes().writePosition());
    }

    socketPollCount++;

    bytesReadCount += (in.readRemaining() - lastReadRemaining);

    final long now = System.currentTimeMillis();
    if (now > lastMonitor + 10000) {
        final NetworkStatsListener<T> networkStatsListener = this.nc.networkStatsListener();

        if (networkStatsListener != null && !networkStatsListener.isClosed()) {
            if (lastMonitor == 0) {
                networkStatsListener.onNetworkStats(0, 0, 0);
            } else {
                networkStatsListener.onNetworkStats(writeBps / 10, bytesReadCount / 10,
                        socketPollCount / 10);

                writeBps = bytesReadCount = socketPollCount = 0;
            }
        }
        lastMonitor = now;
    }

    if (publisher != null)
        publisher.applyAction(outWire);

    if (in.readRemaining() >= SIZE_OF_SIZE)
        onRead0();
    else
        onWrite(outWire);

    lastWritePosition = outWire.bytes().writePosition();
    lastReadRemaining = inWire.bytes().readRemaining();

}
 
Example 6
Source File: FixSaxParser.java    From SAXophone with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void searchForTheEndOfField(Bytes bytes) {
    while (bytes.readByte() != FIELD_TERMINATOR) ;
}
 
Example 7
Source File: BooleanMarshaller.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public Boolean read(Bytes in, @Nullable Boolean using) {
    return in.readByte() != 0;
}