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

The following examples show how to use net.openhft.chronicle.bytes.Bytes#wrapForRead() . 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: BytesBufferHandler.java    From Chronicle-Network with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void handleDecryptedData(final ByteBuffer input, final ByteBuffer output) {
    final Bytes<ByteBuffer> applicationInput;
    if (input.position() != 0) {
        input.flip();
        applicationInput = Bytes.wrapForRead(input);
        AbstractReferenceCounted.unmonitor(applicationInput); // temporary wrapper
    } else {
        applicationInput = EMPTY_APPLICATION_INPUT;
    }

    final Bytes<ByteBuffer> applicationOutput = Bytes.wrapForWrite(output);
    try {
        delegateHandler.process(applicationInput, applicationOutput, networkContext);
        output.position((int) applicationOutput.writePosition());
    } finally {
        applicationOutput.releaseLast();
    }

    input.position((int) applicationInput.readPosition());
    if (applicationInput.readPosition() != 0) {
        input.compact();
    }
}
 
Example 2
Source File: ChronicleQueueIndexTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
private boolean hasEOFAtEndOfFile(final File file) throws IOException {

        Bytes bytes;
        FileInputStream fis = new FileInputStream(file);
        fis.skip(131328);
        byte[] bytes1 = new byte[fis.available()];
        int read = fis.read(bytes1);
        if (read != bytes1.length) {
            throw new AssertionError();
        }
        bytes = Bytes.wrapForRead(bytes1);

        // to check that "00 00 00 c0") is the EOF you can run net.openhft.chronicle.queue.ChronicleQueueIndexTest.eofAsHex
        //  eofAsHex();

        // check that the EOF is in the last few bytes.
//        String lastFewBytes = bytes.toHexString(131328, 128);
        String lastFewBytes = bytes.toHexString(0, 128);
        //System.out.println(lastFewBytes);

        // 00 00 00 c0 is the EOF
        return lastFewBytes.contains("00 00 00 c0");
    }
 
Example 3
Source File: BatchSignAndVerifyEd25519Test.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void signAndVerify() {
    Bytes privateKeyBuffer = null;
    Bytes secretKeyBuffer = null;
    Bytes privateOrSecret = bft.fromHex(privateOrSecretKey);
    if (privateOrSecret.readRemaining() == Ed25519.SECRET_KEY_LENGTH) {
        secretKeyBuffer = privateOrSecret;
    } else {
        privateKeyBuffer = privateOrSecret;
    }

    Bytes publicKeyBuffer = bft.fromHex(publicKey);
    if (secretKeyBuffer == null) {
        secretKeyBuffer = bft.bytesWithZeros(Ed25519.SECRET_KEY_LENGTH);
        Bytes tmpPublicKeyBuffer = bft.bytesWithZeros(Ed25519.PUBLIC_KEY_LENGTH);
        Ed25519.privateToPublicAndSecret(tmpPublicKeyBuffer, secretKeyBuffer, privateKeyBuffer);
        assertEquals(publicKeyBuffer.toHexString(), tmpPublicKeyBuffer.toHexString());
    }
    Bytes messageBuffer = bft.fromHex(message);
    Bytes signExpectedBuffer;
    if (signExpected.length() == 128) {
        signExpectedBuffer = Bytes.wrapForRead(DatatypeConverter.parseHexBinary(signExpected + message));
    } else {
        signExpectedBuffer = Bytes.wrapForRead(DatatypeConverter.parseHexBinary(signExpected));
    }
    Bytes signedMsgBuffer = bft.fromHex(Ed25519.SIGNATURE_LENGTH, message);
    signedMsgBuffer.writePosition(0);
    Ed25519.sign(signedMsgBuffer, messageBuffer, secretKeyBuffer);
    assertEquals(signExpectedBuffer.toHexString(), signedMsgBuffer.toHexString());
    signedMsgBuffer.readPosition(0);
    publicKeyBuffer.readPositionRemaining(0, Ed25519.PUBLIC_KEY_LENGTH);
    assertTrue(Ed25519.verify(signedMsgBuffer, publicKeyBuffer));
}
 
Example 4
Source File: InMemoryLongColumn.java    From Chronicle-TimeSeries with GNU Lesser General Public License v3.0 5 votes vote down vote up
public InMemoryLongColumn(TimeSeries timeSeries, String name, BytesLongLookup lookup, long capacity) {
    super(timeSeries, name);
    this.lookup = lookup;
    long value = lookup.sizeFor(capacity);
    this.bytes = Jvm.isDebug()
            ? Bytes.wrapForRead(ByteBuffer.allocateDirect(Math.toIntExact(value)))
            : NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(value);
}
 
Example 5
Source File: InMemoryLongColumn.java    From Chronicle-TimeSeries with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void ensureCapacity(long capacity) {
    long cap = lookup.sizeFor(capacity);
    if (cap > bytes.realCapacity()) {
        long value = lookup.sizeFor(capacity);
        BytesStore bytes2 = Jvm.isDebug()
                ? Bytes.wrapForRead(ByteBuffer.allocateDirect(Math.toIntExact(value)))
                : NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(value);
        bytes2.write(0, bytes);
        bytes.release();
        bytes = bytes2;
    }
}
 
Example 6
Source File: InMemoryDoubleColumn.java    From Chronicle-TimeSeries with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
    public void ensureCapacity(long capacity) {
        long cap = lookup.sizeFor(capacity);
        if (cap > bytes.realCapacity()) {
//            BytesStore bytes2 = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(Maths.divideRoundUp(cap, OS.pageSize()));
            BytesStore bytes2 = Bytes.wrapForRead(ByteBuffer.allocateDirect(Math.toIntExact(lookup.sizeFor(capacity))));
            bytes2.write(0, bytes);
            bytes.release();
            bytes = bytes2;
        }
    }
 
Example 7
Source File: InMemoryDoubleColumn.java    From Chronicle-TimeSeries with GNU Lesser General Public License v3.0 4 votes vote down vote up
public InMemoryDoubleColumn(TimeSeries timeSeries, String name, BytesDoubleLookup lookup, long capacity) {
        super(timeSeries, name);
        this.lookup = lookup;
//        this.bytes = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(lookup.sizeFor(capacity));
        this.bytes = Bytes.wrapForRead(ByteBuffer.allocateDirect(Math.toIntExact(lookup.sizeFor(capacity))));
    }
 
Example 8
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);
    }
}