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

The following examples show how to use net.openhft.chronicle.bytes.Bytes#writeByte() . 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: Rc4Cipher.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
public void prga(Bytes<?> buffer, long size) {
    int i = 0;
    int j = 0;
    for (int c = 0; c < size; c++) {
        i = (i + 1) % 256;
        j = (j + state[i]) % 256;
        swap(i, j);
        buffer.writeByte((byte) state[(state[i] + state[j]) % 256]);
    }
}
 
Example 2
Source File: JsonParserTest.java    From SAXophone with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void testPull(String json) {
    StringWriter stringWriter = new StringWriter();
    JsonParser p = JsonParser.builder().applyAdapter(new WriterAdapter(stringWriter)).build();
    Bytes jsonBytes = stringToBytes(json);
    Bytes parseBytes = Bytes.elasticByteBuffer();
    for (long i = 0; i < jsonBytes.capacity(); i++) {
        parseBytes.writeByte(jsonBytes.readByte(i));
        p.parse(parseBytes);
    }
    p.finish();
    com.google.gson.JsonParser referenceParser = new com.google.gson.JsonParser();
    JsonElement o1 = referenceParser.parse(json);
    JsonElement o2 = referenceParser.parse(stringWriter.toString());
    assertEquals(o1, o2);
}
 
Example 3
Source File: ReplicatedMapEntryStages.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
void updateReplicationState(byte identifier, long timestamp) {
    initDelayedUpdateChecksum(true);
    Bytes segmentBytes = s.segmentBytesForWrite();
    segmentBytes.writePosition(replicationBytesOffset);
    segmentBytes.writeLong(timestamp);
    segmentBytes.writeByte(identifier);
}
 
Example 4
Source File: RawAccessJava.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void Appender() {
    if (!assert_from_cpp())
        return;

    String tmp = "/dev/shm/RawAccessJtoC";
    System.out.println(tmp); // so C++ knows this ran rather than skipped

    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {

        ExcerptAppender appender = cq.acquireAppender();

        for (int i = 0; i < COUNT; ++i) {
            try (DocumentContext dc = appender.writingDocument()) {

                Bytes bytes = dc.wire().bytes();

                // will contain the size of the blob
                long start = bytes.writePosition();
                bytes.writeSkip(RAW_SIZE_PREFIX);

                {
                    bytes.writeByte((byte) 0xab);
                    bytes.writeShort((short) 12);
                    bytes.writeInt(123);
                    bytes.writeLong(123456789L);
                    bytes.writeFloat(1.234f);
                    bytes.writeDouble(123.456);
                    bytes.writeChar('a');
                    bytes.write8bit("Hello World");
                }

                long end = bytes.writePosition();
                bytes.writeInt(start, (int) (end - start - RAW_SIZE_PREFIX));
            }
        }
    }
}
 
Example 5
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 6
Source File: ReplicatedChronicleMap.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
private void writePayload(Bytes payload, Bytes destination) {
    destination.writeByte(BOOTSTRAP_TIME_HUNK);
    destination.write(payload, payload.readPosition(), payload.readRemaining());
}
 
Example 7
Source File: ReplicatedChronicleMap.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
/**
 * This method does not set a segment lock, A segment lock should be obtained before calling
 * this method, especially when being used in a multi threaded context.
 */
private void writeExternalEntry0(ReplicableEntry entry, Bytes destination, ArrayList<String> keys) {
    destination.writeByte(ENTRY_HUNK);

    destination.writeStopBit(entry.originTimestamp());
    if (entry.originIdentifier() == 0)
        throw new IllegalStateException("Identifier can't be 0");
    destination.writeByte(entry.originIdentifier());

    Data key;
    boolean isDeleted;
    if (entry instanceof MapEntry) {
        isDeleted = false;
        key = ((MapEntry) entry).key();
    } else {
        isDeleted = true;
        key = ((MapAbsentEntry) entry).absentKey();
    }

    try {
        keys.add( key.get().toString() );
    }catch(Exception e){
        keys.add( "<Binary Data>" );
    }

    destination.writeBoolean(isDeleted);

    keySizeMarshaller.writeSize(destination, key.size());
    key.writeTo(destination, destination.writePosition());
    destination.writeSkip(key.size());

    boolean traceEnabled = LOG.isTraceEnabled();
    String message = null;
    if (traceEnabled) {
        if (isDeleted) {
            LOG.trace("WRITING ENTRY TO DEST -  into local-id={}, remove(key={})",
                    identifier(), key);
        } else {
            message = String.format(
                    "WRITING ENTRY TO DEST  -  into local-id=%d, put(key=%s,",
                    identifier(), key);
        }
    }

    if (isDeleted)
        return;

    Data value = ((MapEntry) entry).value();
    valueSizeMarshaller.writeSize(destination, value.size());
    value.writeTo(destination, destination.writePosition());
    destination.writeSkip(value.size());

    if (traceEnabled) {
        LOG.debug(message + "value=" + value + ")");
    }
}
 
Example 8
Source File: BooleanMarshaller.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Bytes out, @NotNull Boolean toWrite) {
    out.writeByte((byte) (toWrite ? 'Y' : 0));
}