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

The following examples show how to use net.openhft.chronicle.bytes.Bytes#write() . 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: Ed25519Test.java    From Chronicle-Salt with Apache License 2.0 6 votes vote down vote up
@Test
public void sign2() {
    final String SIGN_PRIVATE = "B18E1D0045995EC3D010C387CCFEB984D783AF8FBB0F40FA7DB126D889F6DADD";
    Bytes privateKey = fromHex(SIGN_PRIVATE);
    Bytes publicKey = bytesWithZeros(32);
    Bytes secretKey = bytesWithZeros(64);
    Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey);
    checkZeros(secretKey);
    checkZeros(privateKey);

    Bytes message = privateKey;
    Bytes signAndMsg = Bytes.allocateDirect(Ed25519.SIGNATURE_LENGTH + message.readRemaining());
    signAndMsg.writeSkip(Ed25519.SIGNATURE_LENGTH);
    signAndMsg.write(privateKey);
    Ed25519.sign(signAndMsg, secretKey);

    String SIGN_EXPECTED = "86b4707fadb1ef4613efadd12143cd9dffb2eac329c38923c03f9e315c3dd33bde1ef101137fbc403eb3f3d7ff283155053c667eb65908fe6fcd653eab550e0f";
    Bytes signExpected = fromHex(SIGN_EXPECTED + SIGN_PRIVATE);
    assertEquals(signExpected.toHexString(), signAndMsg.toHexString());

    signAndMsg.releaseLast();
}
 
Example 2
Source File: VanillaWireOutPublisher.java    From Chronicle-Network with Apache License 2.0 6 votes vote down vote up
/**
 * Apply waiting messages and return false if there was none.
 *
 * @param bytes buffer to write to.
 */
@Override
public void applyAction(@NotNull Bytes bytes) {
    resetUsedByThread();

    if (this.bytes.readRemaining() > 0) {

        synchronized (lock()) {

            if (YamlLogging.showServerWrites())
                logBuffer();

            bytes.write(this.bytes);
            this.bytes.clear();
        }
    }
}
 
Example 3
Source File: BytesRingBuffer.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
private long read(@NotNull Bytes bytes, long offset) {
    offset %= capacity();
    long endOffSet = nextOffset(offset, bytes.remaining());

    if (endOffSet >= offset) {
        bytes.write(buffer, offset, bytes.remaining());
        bytes.flip();
        return endOffSet;
    }

    bytes.write(buffer, offset, capacity() - offset);
    bytes.write(buffer, 0, bytes.remaining());
    bytes.flip();

    return endOffSet;
}
 
Example 4
Source File: Sodium.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
static Bytes<?> fromHex(int padding, String s) {
    byte[] byteArr = DatatypeConverter.parseHexBinary(s);
    Bytes<?> bytes = Bytes.allocateDirect(padding + byteArr.length);
    if (padding > 0) {
        bytes.zeroOut(0, padding);
        bytes.writePosition(padding);
    }
    bytes.write(byteArr);
    return bytes;
}
 
Example 5
Source File: SHA2Test.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
private static void doTest256(byte[] inputStr, String expectedHex) {
    Bytes<?> input = Bytes.allocateElasticDirect();
    input.write(inputStr);
    Bytes<?> hash256 = Bytes.allocateElasticDirect();
    SHA2.appendSha256(hash256, input);
    Bytes<?> expected = Bytes.allocateElasticDirect();
    expected.write(DatatypeConverter.parseHexBinary(expectedHex));
    assertEquals(expected.toHexString(), hash256.toHexString());
    expected.releaseLast();
    input.releaseLast();
    hash256.releaseLast();
}
 
Example 6
Source File: SHA2Test.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
private static void doTest512(String inputStr, String expectedHex) {
    Bytes<?> input = Bytes.allocateElasticDirect();
    input.append(inputStr);
    Bytes<?> hash512 = Bytes.allocateElasticDirect();
    SHA2.appendSha512(hash512, input);
    hash512.readPosition(0);
    Bytes<?> expected = Bytes.allocateElasticDirect();
    expected.write(DatatypeConverter.parseHexBinary(expectedHex));
    assertEquals(expected.toHexString(), hash512.toHexString());
    expected.releaseLast();
    input.releaseLast();
    hash512.releaseLast();
}
 
Example 7
Source File: BytesForTesting.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
Bytes fromHex(int padding, String s) {
    byte[] byteArr = DatatypeConverter.parseHexBinary(s);
    Bytes bytes = bytesWithZeros(padding + byteArr.length);
    if (padding > 0) {
        bytes.zeroOut(0, padding);
        bytes.writePosition(padding);
    }
    bytes.write(byteArr);
    return bytes;
}
 
Example 8
Source File: NonClusteredSslIntegrationTest.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
@Override
public void process(@NotNull final Bytes in, @NotNull final Bytes out, final StubNetworkContext nc) {
    latch.countDown();
    try {
        if (nc.isAcceptor() && in.readRemaining() != 0) {
            final int magic = in.readInt();
            if (magic != 0xFEDCBA98)
                throw new IllegalStateException("Invalid magic number " + Integer.toHexString(magic));
            final long received = in.readLong();
            final int len = in.readInt();
            final byte[] tmp = new byte[len];
            in.read(tmp);
            if (DEBUG) {
                if (len > 10) {
                    System.out.printf("%s received payload of length %d%n", label, len);
                    System.out.println(in);
                } else {
                    System.out.printf("%s received [%d] %d/%s%n", label, tmp.length, received, new String(tmp, StandardCharsets.US_ASCII));
                }
            }
            operationCount++;
        } else if (!nc.isAcceptor()) {
            if (System.currentTimeMillis() > lastSent + 100L) {
                out.writeInt(0xFEDCBA98);
                out.writeLong((counter++));
                final String payload = "ping-" + (counter - 1);
                out.writeInt(payload.length());
                out.write(payload.getBytes(StandardCharsets.US_ASCII));
                if (DEBUG) {
                    System.out.printf("%s sent [%d] %d/%s%n", label, payload.length(), counter - 1, payload);
                }
                operationCount++;
                lastSent = System.currentTimeMillis();
            }
        }
    } catch (RuntimeException e) {
        System.err.printf("Exception in %s: %s/%s%n", label, e.getClass().getSimpleName(), e.getMessage());
        e.printStackTrace();
    }
}
 
Example 9
Source File: EchoHandler.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
@Override
    public void process(@NotNull final Bytes in, @NotNull final Bytes out, T nc) {
//        System.out.println(in.readRemaining());
        if (in.readRemaining() == 0)
            return;
//        System.out.println("P start " + in.toDebugString());
        long toWrite = Math.min(in.readRemaining(), out.writeRemaining());
        out.write(in, in.readPosition(), toWrite);
        in.readSkip(toWrite);
//        System.out.println("... P End " + in.toDebugString());
    }
 
Example 10
Source File: TimedEchoHandler.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
@Override
public void process(@NotNull final Bytes in, @NotNull final Bytes out, T nc) {
    if (in.readRemaining() == 0)
        return;
    long toWrite = Math.min(in.readRemaining(), out.writeRemaining());
    out.write(in, in.readPosition(), toWrite);
    out.writeLong(System.nanoTime());
    in.readSkip(toWrite);
}
 
Example 11
Source File: DirectChronicleQueueStringTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private void writeSome(DirectChronicleQueue chronicle) {
    NativeBytesStore allocate = NativeBytesStore.nativeStoreWithFixedCapacity(EXPECTED_BYTES.length);
    final Bytes toWrite = allocate.bytes();
    for (int i = 0; i < RUNS; i++) {
        toWrite.clear();
        toWrite.write(EXPECTED_BYTES);
        toWrite.flip();
        chronicle.appendDocument(toWrite);
    }
}
 
Example 12
Source File: SignAndVerifyPerfMain.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
static Bytes<?> fromHex(String s) {
    byte[] byteArr = DatatypeConverter.parseHexBinary(s);
    Bytes<?> bytes = Bytes.allocateDirect(byteArr.length);
    bytes.write(byteArr);
    return bytes;
}
 
Example 13
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 14
Source File: CharSequenceCustomEncodingBytesWriter.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
private void writeOutputBuffer(Bytes out) {
    int remaining = outputBuffer.remaining();
    out.write(out.writePosition(), outputBuffer, 0, remaining);
    out.writeSkip(remaining);
}
 
Example 15
Source File: ThroughputPerfMain2.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public static void main(String[] args) {
    String base = path + "/delete-" + System.nanoTime() + ".me";
    long start = System.nanoTime();
    long count = 0;
    nbs = NativeBytesStore.nativeStoreWithFixedCapacity(size);

    long blockSize = OS.is64Bit() ? 4L << 30 : 256L << 20;
    try (ChronicleQueue q = SingleChronicleQueueBuilder.binary(base)
            .rollCycle(RollCycles.LARGE_HOURLY_XSPARSE)
            .blockSize(blockSize)
            .build()) {

        ExcerptAppender appender = q.acquireAppender();
        count += appender.batchAppend(time * 1000, ThroughputPerfMain2::writeMessages);
    }

    nbs.releaseLast();
    long mid = System.nanoTime();
    long time1 = mid - start;

    Bytes bytes = Bytes.allocateElasticDirect(64);
    try (ChronicleQueue q = SingleChronicleQueueBuilder.binary(base)
            .rollCycle(RollCycles.LARGE_HOURLY_XSPARSE)
            .blockSize(blockSize)
            .build()) {
        ExcerptTailer tailer = q.createTailer();
        for (long i = 0; i < count; i++) {
            try (DocumentContext dc = tailer.readingDocument()) {
                bytes.clear();
                bytes.write(dc.wire().bytes());
            }
        }
    }
    bytes.releaseLast();
    long end = System.nanoTime();
    long time2 = end - mid;

    System.out.printf("Writing %,d messages took %.3f seconds, at a rate of %,d per second%n",
            count, time1 / 1e9, (long) (1e9 * count / time1));
    System.out.printf("Reading %,d messages took %.3f seconds, at a rate of %,d per second%n",
            count, time2 / 1e9, (long) (1e9 * count / time2));

    System.gc(); // make sure its cleaned up for windows to delete.
    IOTools.deleteDirWithFiles(base, 2);
}