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

The following examples show how to use net.openhft.chronicle.bytes.Bytes#readInt() . 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: SetMarshaller.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public Set<T> read(Bytes in, @Nullable Set<T> using) {
    int size = in.readInt();
    if (using == null) {
        using = new HashSet<>((int) (size / 0.75));
        for (int i = 0; i < size; i++) {
            using.add(elementReader.read(in, null));
        }
    } else {
        orderedElements.addAll(using);
        using.clear();
        for (int i = 0; i < size; i++) {
            using.add(elementReader.read(in, orderedElements.pollFirst()));
        }
        orderedElements.clear(); // for GC, avoid zombie object links
    }
    return using;
}
 
Example 2
Source File: ListMarshaller.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public List<T> read(Bytes in, @Nullable List<T> using) {
    int size = in.readInt();
    if (using == null) {
        using = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            using.add(null);
        }
    } else if (using.size() < size) {
        while (using.size() < size) {
            using.add(null);
        }
    } else if (using.size() > size) {
        using.subList(size, using.size()).clear();
    }
    for (int i = 0; i < size; i++) {
        using.set(i, elementReader.read(in, using.get(i)));
    }
    return using;
}
 
Example 3
Source File: MapMarshaller.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public Map<K, V> read(Bytes in, @Nullable Map<K, V> using) {
    int size = in.readInt();
    if (using == null) {
        using = new HashMap<>(((int) (size / 0.75)));
        for (int i = 0; i < size; i++) {
            using.put(keyReader.read(in, null), valueReader.read(in, null));
        }
    } else {
        using.forEach((k, v) -> {
            orderedKeys.add(k);
            orderedValues.add(v);
        });
        using.clear();
        for (int i = 0; i < size; i++) {
            using.put(keyReader.read(in, orderedKeys.pollFirst()),
                    valueReader.read(in, orderedValues.pollFirst()));
        }
        orderedKeys.clear(); // for GC, avoid zombie object links
        orderedValues.clear();
    }
    return using;
}
 
Example 4
Source File: CharSequenceArrayBytesMarshaller.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public CharSequence[] read(Bytes in, @Nullable CharSequence[] using) {
    int len = in.readInt();
    if (using == null)
        using = new CharSequence[len];
    if (using.length != len)
        using = Arrays.copyOf(using, len);
    for (int i = 0; i < len; i++) {
        CharSequence cs = using[i];
        if (cs instanceof StringBuilder) {
            in.readUtf8((StringBuilder) cs);
        } else {
            StringBuilder sb = new StringBuilder(0);
            in.readUtf8(sb);
            using[i] = sb;
        }
    }
    return using;
}
 
Example 5
Source File: WireTcpHandler.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
private void ensureCapacity() {
    @NotNull final Bytes<?> bytes = inWire.bytes();
    if (bytes.readRemaining() >= 4) {
        final long pos = bytes.readPosition();
        int length = bytes.readInt(pos);
        final long size = pos + Wires.SPB_HEADER_SIZE * 2 + Wires.lengthOf(length);
        if (size > bytes.realCapacity()) {
            resizeInWire(size);
        }
    }
}
 
Example 6
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 7
Source File: IntegerMarshaller.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public Integer read(@NotNull Bytes in, long size, @Nullable Integer using) {
    return in.readInt();
}
 
Example 8
Source File: IntegerMarshaller.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public Integer read(Bytes in, @Nullable Integer using) {
    return in.readInt();
}
 
Example 9
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;
}
 
Example 10
Source File: RawAccessJava.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
public void Tailer() {
    if (!assert_from_cpp())
        return;

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

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

        ExcerptTailer tailer = cq.createTailer();

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

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

                bytes.readSkip(-QUEUE_HEADER_SIZE);
                int header = bytes.readInt();

                // document length, inc 4-byte length
                int length = Wires.lengthOf(header);

                // actual length of data
                int data_length = bytes.readInt();

                assertEquals(bytes.readByte(), (byte) 0xab);
                assertEquals(bytes.readShort(), (short) 12);
                assertEquals(bytes.readInt(), 123);
                assertEquals(bytes.readLong(), 123456789L);
                assertEquals(bytes.readFloat(), 1.234f, 1.0e-7);
                assertEquals(bytes.readDouble(), 123.456, 1.0e-7);
                assertEquals(bytes.readChar(), 'a');

                StringBuilder sb = new StringBuilder();
                bytes.read8bit(sb);
                assertEquals(sb.toString(), "Hello World");
            }
        }
    }
}