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

The following examples show how to use net.openhft.chronicle.bytes.Bytes#read() . 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: ByteBufferSizedReader.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public ByteBuffer read(@NotNull Bytes in, long size, @Nullable ByteBuffer using) {
    if (size < 0L || size > (long) Integer.MAX_VALUE)
        throw new IllegalArgumentException("ByteBuffer size should be non-negative int, " +
                size + " given. Memory corruption?");
    int bufferCap = (int) size;
    if (using == null || using.capacity() < bufferCap) {
        using = ByteBuffer.allocate(bufferCap);
    } else {
        using.position(0);
        using.limit(bufferCap);
    }
    in.read(using);
    using.flip();
    return using;
}
 
Example 2
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 3
Source File: ByteArraySizedReader.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public byte[] read(@NotNull Bytes in, long size, @Nullable byte[] using) {
    if (size < 0L || size > (long) Integer.MAX_VALUE) {
        throw new IORuntimeException("byte[] size should be non-negative int, " +
                size + " given. Memory corruption?");
    }
    int arrayLength = (int) size;
    if (using == null || arrayLength != using.length)
        using = new byte[arrayLength];
    in.read(using);
    return using;
}
 
Example 4
Source File: CharSequenceCustomEncodingBytesReader.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public CharSequence read(Bytes in, @Nullable CharSequence using) {
    long csLengthAsLong = in.readStopBit();
    if (csLengthAsLong > Integer.MAX_VALUE) {
        throw new IORuntimeException("cs len shouldn't be more than " + Integer.MAX_VALUE +
                ", " + csLengthAsLong + " read");
    }
    int csLength = (int) csLengthAsLong;
    StringBuilder sb;
    if (using instanceof StringBuilder) {
        sb = (StringBuilder) using;
        sb.setLength(0);
        sb.ensureCapacity(csLength);
    } else {
        sb = new StringBuilder(csLength);
    }

    int remainingBytes = in.readInt();
    charsetDecoder.reset();
    inputBuffer.clear();
    outputBuffer.clear();
    boolean endOfInput = false;
    // this loop inspired by the CharsetDecoder.decode(ByteBuffer) implementation
    while (true) {
        if (!endOfInput) {
            int inputChunkSize = Math.min(inputBuffer.remaining(), remainingBytes);
            inputBuffer.limit(inputBuffer.position() + inputChunkSize);
            in.read(inputBuffer);
            inputBuffer.flip();
            remainingBytes -= inputChunkSize;
            endOfInput = remainingBytes == 0;
        }

        CoderResult cr = inputBuffer.hasRemaining() ?
                charsetDecoder.decode(inputBuffer, outputBuffer, endOfInput) :
                CoderResult.UNDERFLOW;

        if (cr.isUnderflow() && endOfInput)
            cr = charsetDecoder.flush(outputBuffer);

        if (cr.isUnderflow()) {
            if (endOfInput) {
                break;
            } else {
                inputBuffer.compact();
                continue;
            }
        }

        if (cr.isOverflow()) {
            outputBuffer.flip();
            sb.append(outputBuffer);
            outputBuffer.clear();
            continue;
        }

        try {
            cr.throwException();
        } catch (CharacterCodingException e) {
            throw new IORuntimeException(e);
        }
    }
    outputBuffer.flip();
    sb.append(outputBuffer);

    return sb;
}