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

The following examples show how to use net.openhft.chronicle.bytes.Bytes#readLimit() . 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: JsonParser.java    From SAXophone with GNU Lesser General Public License v3.0 6 votes vote down vote up
boolean on() throws IOException {
    Bytes buf = lexer.outBuf;
    long bufPos = lexer.outPos;
    long bufLen = lexer.outLen;
    boolean borrowBuf = buf.readPosition() != bufPos ||
            buf.readRemaining() != bufLen;
    long pos = 0, lim = 0;
    if (borrowBuf) {
        pos = buf.readPosition();
        lim = buf.readLimit();
        //buf.clear();
        buf.readLimit(bufPos + bufLen);
        buf.readPosition(bufPos);
    }
    boolean go = apply(value(buf));
    if (borrowBuf) {
        //buf.clear();
        buf.readLimit(lim);
        buf.readPosition(pos);
    }
    return go;
}
 
Example 2
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 3
Source File: SodiumTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void signAndVerify2() {
    final String SIGN_PRIVATE = "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd";
    final String SIGN_MESSAGE = "916c7d1d268fc0e77c1bef238432573c39be577bbea0998936add2b50a653171"
            + "ce18a542b0b7f96c1691a3be6031522894a8634183eda38798a0c5d5d79fbd01"
            + "dd04a8646d71873b77b221998a81922d8105f892316369d5224c9983372d2313"
            + "c6b1f4556ea26ba49d46e8b561e0fc76633ac9766e68e21fba7edca93c4c7460" + "376d7f3ac22ff372c18f613f2ae2e856af40";
    final String SIGN_SIGNATURE = "6bd710a368c1249923fc7a1610747403040f0cc30815a00f9ff548a896bbda0b"
            + "4eb2ca19ebcf917f0f34200a9edbad3901b64ab09cc5ef7b9bcc3c40c0ff7509";

    Bytes publicKey = bytesWithZeros(32);
    Bytes secretKey = bytesWithZeros(64);
    Bytes privateKey = fromHex(SIGN_PRIVATE);
    assertEquals(0,
            SODIUM.crypto_sign_ed25519_seed_keypair(publicKey.addressForWrite(0), secretKey.addressForWrite(0), privateKey.addressForRead(0)));
    publicKey.readPositionRemaining(0, 32);
    secretKey.readPositionRemaining(0, 64);
    // System.out.println(publicKey.toHexString());
    // System.out.println(secretKey.toHexString());

    Bytes sigAndMsg2 = fromHex(SIGN_SIGNATURE);
    Bytes message = fromHex(SIGN_MESSAGE);
    Bytes sigAndMsg = fromHex(64 + (SIGN_MESSAGE.length() / 2), "");

    LongLongByReference sigLen = new LongLongByReference(0);
    assertEquals(0, SODIUM.crypto_sign_ed25519(sigAndMsg.addressForWrite(0), sigLen, message.addressForRead(0), (int) message.readRemaining(),
            secretKey.addressForRead(0)));
    checkZeros(sigAndMsg);
    assertEquals(210, sigLen.longValue());
    sigAndMsg.readLimit(64);
    assertEquals(sigAndMsg2.toHexString(), sigAndMsg.toHexString());

    Bytes buffer = bytesWithZeros(210);

    LongLongByReference bufferLen = new LongLongByReference(0);
    assertEquals(0,
            SODIUM.crypto_sign_ed25519_open(buffer.addressForWrite(0), bufferLen, sigAndMsg.addressForRead(0), 210, publicKey.addressForRead(0)));
    assertEquals(210 - 64, bufferLen.longValue());

}
 
Example 4
Source File: TcpChannelHub.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
/**
 * process system messages which originate from the server
 *
 * @param header      a value representing the type of message
 * @param messageSize the size of the message
 * @throws IOException if a buffer cannot be read
 */
private void processServerSystemMessage(final int header, final int messageSize) throws IOException {

    serverHeartBeatHandler.clear();
    final Bytes bytes = serverHeartBeatHandler;

    bytes.clear();
    @NotNull final ByteBuffer byteBuffer = (ByteBuffer) bytes.underlyingObject();
    byteBuffer.clear();
    // we have to first write the header back to the bytes so that is can be
    // viewed as a document
    bytes.writeInt(0, header);
    byteBuffer.position(SIZE_OF_SIZE);
    byteBuffer.limit(SIZE_OF_SIZE + messageSize);
    readBuffer(byteBuffer);

    bytes.readLimit(byteBuffer.position());

    final StringBuilder eventName = Wires.acquireStringBuilder();
    final Wire inWire = TcpChannelHub.this.wireType.apply(bytes);
    if (YamlLogging.showHeartBeats())
        logToStandardOutMessageReceived(inWire);
    inWire.readDocument(null, d -> {
                @NotNull final ValueIn valueIn = d.readEventName(eventName);
                if (EventId.heartbeat.contentEquals(eventName))
                    reflectServerHeartbeatMessage(valueIn);
                else if (EventId.onClosingReply.contentEquals(eventName))
                    receivedClosedAcknowledgement.countDown();

            }
    );
}
 
Example 5
Source File: TcpChannelHub.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
/**
 * blocks indefinitely until the number of expected bytes is received
 *
 * @param wire          the wire that the data will be written into, this wire must contain an underlying ByteBuffer
 * @param numberOfBytes the size of the data to read
 * @throws IOException if anything bad happens to the socket connection
 */
private void blockingRead(@NotNull final WireIn wire, final long numberOfBytes) throws IOException {

    @NotNull final Bytes<?> bytes = wire.bytes();
    bytes.ensureCapacity(bytes.writePosition() + numberOfBytes);

    @NotNull final ByteBuffer buffer = (ByteBuffer) bytes.underlyingObject();
    final int start = (int) bytes.writePosition();
    //noinspection ConstantConditions
    buffer.position(start);

    buffer.limit((int) (start + numberOfBytes));
    readBuffer(buffer);
    bytes.readLimit(buffer.position());
}
 
Example 6
Source File: Lexer.java    From SAXophone with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * scan a string for interesting characters that might need further
 * review.  return the number of chars that are uninteresting and can
 * be skipped.
 * (lth) hi world, any thoughts on how to make this routine faster?
 */
private void stringScan(Bytes bytes) {
    int mask = IJC | NFP | (validateUTF8 ? NUC : 0);
    long pos = bytes.readPosition();
    long limit = bytes.readLimit();
    while (pos < limit && ((CHAR_LOOKUP_TABLE[bytes.readUnsignedByte(pos)] & mask) == 0)) {
        pos++;
    }
    bytes.readPosition(pos);
}
 
Example 7
Source File: BenchmarkMain.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private static long readMessage(Bytes<?> bytes) {
    Jvm.safepoint();
    long start = bytes.readLong();
    long rp = bytes.readPosition();
    long rl = bytes.readLimit();
    long addr = bytes.addressForRead(rp);
    long addrEnd = bytes.addressForRead(rl);
    Memory memory = OS.memory();
    for (addr += 8; addr + 7 < addrEnd; addr += 8)
        memory.readLong(addr);
    Jvm.safepoint();
    return start;
}
 
Example 8
Source File: StoreTailer.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private void inACycleFound(@NotNull final Bytes<?> bytes) {
        context.closeReadLimit(bytes.capacity());
        privateWire().readAndSetLength(bytes.readPosition());
        final long end = bytes.readLimit();
        context.closeReadPosition(end);
//        Jvm.optionalSafepoint();
    }
 
Example 9
Source File: BatchAppenderNativeTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testNative() {
    if (!OS.isMacOSX())
        return;

    Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();
    try {

        BatchAppenderNative batchAppenderNative = new BatchAppenderNative();

        // this will append a message in wire of hello world
        long result = batchAppenderNative.writeMessages(bytes.addressForWrite(0), bytes.realCapacity(), 1);

        int len = (int) result;
        int count = (int) (result >> 32);
        bytes.readLimit(len);

        Assert.assertEquals(16, len);
        Assert.assertEquals(1, count);

        Wire w = WireType.BINARY.apply(bytes);

        for (int i = 0; i < count; i++) {
            try (DocumentContext dc = w.readingDocument()) {
                Assert.assertEquals("hello world", dc.wire().getValueIn().text());
            }
        }
    } finally {
        bytes.releaseLast();
    }
}
 
Example 10
Source File: ChronicleMapBuilder.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
private VanillaChronicleMap<K, V, ?> openWithExistingFile(
        File file, RandomAccessFile raf, ChronicleHashResources resources,
        boolean recover, boolean overrideBuilderConfig,
        ChronicleHashCorruption.Listener corruptionListener)
        throws IOException {
    ChronicleHashCorruptionImpl corruption = recover ? new ChronicleHashCorruptionImpl() : null;
    try {
        int headerSize = waitUntilReady(raf, file, recover);
        FileChannel fileChannel = raf.getChannel();
        ByteBuffer headerBuffer = readSelfBootstrappingHeader(
                file, raf, headerSize, recover, corruptionListener, corruption);
        if (headerSize != headerBuffer.remaining())
            throw new AssertionError();
        boolean headerCorrect = checkSumSelfBootstrappingHeader(headerBuffer, headerSize);
        boolean headerWritten = false;
        if (!headerCorrect) {
            if (overrideBuilderConfig) {
                VanillaChronicleMap<K, V, ?> mapObjectForHeaderOverwrite = newMap();
                headerBuffer = writeHeader(fileChannel, mapObjectForHeaderOverwrite);
                headerSize = headerBuffer.remaining();
                headerWritten = true;
            } else {
                throw throwRecoveryOrReturnIOException(file,
                        "Self Bootstrapping Header checksum doesn't match the stored checksum",
                        recover);
            }
        }
        Bytes<ByteBuffer> headerBytes = Bytes.wrapForRead(headerBuffer);
        headerBytes.readPosition(headerBuffer.position());
        headerBytes.readLimit(headerBuffer.limit());
        Wire wire = new TextWire(headerBytes);
        VanillaChronicleMap<K, V, ?> map = wire.getValueIn().typedMarshallable();
        map.initBeforeMapping(file, raf, headerBuffer.limit(), recover);
        long dataStoreSize = map.globalMutableState().getDataStoreSize();
        if (!recover && dataStoreSize > file.length()) {
            throw new IOException("The file " + file + " the map is serialized from " +
                    "has unexpected length " + file.length() + ", probably corrupted. " +
                    "Data store size is " + dataStoreSize);
        }
        map.initTransientsFromBuilder(this);
        if (!recover) {
            map.createMappedStoreAndSegments(resources);
        } else {
            if (!headerWritten)
                writeNotComplete(fileChannel, headerBuffer, headerSize);
            map.recover(resources, corruptionListener, corruption);
            commitChronicleMapReady(map, raf, headerBuffer, headerSize);
        }
        return map;
    } catch (Throwable t) {
        if (recover && !(t instanceof IOException) &&
                !(t instanceof ChronicleHashRecoveryFailedException)) {
            throw new ChronicleHashRecoveryFailedException(t);
        }
        throw Throwables.propagateNotWrapping(t, IOException.class);
    }
}