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

The following examples show how to use net.openhft.chronicle.bytes.Bytes#writeStopBit() . 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: PointListSerializationTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Override
public void write(Bytes out, @NotNull A toWrite) {
    out.writeUtf8(toWrite.str_);
    if (toWrite.list_ != null) {
        int size = toWrite.list_.size();
        out.writeStopBit(size);
        for (int i = 0; i < size; i++) {
            toWrite.list_.get(i).writeMarshallable(out);
        }
    } else {
        out.writeStopBit(-1);
    }
}
 
Example 2
Source File: ReplicatedChronicleMap.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
/**
 * This method does not set a segment lock, A segment lock should be obtained before calling
 * this method, especially when being used in a multi threaded context.
 */
private void writeExternalEntry0(ReplicableEntry entry, Bytes destination, ArrayList<String> keys) {
    destination.writeByte(ENTRY_HUNK);

    destination.writeStopBit(entry.originTimestamp());
    if (entry.originIdentifier() == 0)
        throw new IllegalStateException("Identifier can't be 0");
    destination.writeByte(entry.originIdentifier());

    Data key;
    boolean isDeleted;
    if (entry instanceof MapEntry) {
        isDeleted = false;
        key = ((MapEntry) entry).key();
    } else {
        isDeleted = true;
        key = ((MapAbsentEntry) entry).absentKey();
    }

    try {
        keys.add( key.get().toString() );
    }catch(Exception e){
        keys.add( "<Binary Data>" );
    }

    destination.writeBoolean(isDeleted);

    keySizeMarshaller.writeSize(destination, key.size());
    key.writeTo(destination, destination.writePosition());
    destination.writeSkip(key.size());

    boolean traceEnabled = LOG.isTraceEnabled();
    String message = null;
    if (traceEnabled) {
        if (isDeleted) {
            LOG.trace("WRITING ENTRY TO DEST -  into local-id={}, remove(key={})",
                    identifier(), key);
        } else {
            message = String.format(
                    "WRITING ENTRY TO DEST  -  into local-id=%d, put(key=%s,",
                    identifier(), key);
        }
    }

    if (isDeleted)
        return;

    Data value = ((MapEntry) entry).value();
    valueSizeMarshaller.writeSize(destination, value.size());
    value.writeTo(destination, destination.writePosition());
    destination.writeSkip(value.size());

    if (traceEnabled) {
        LOG.debug(message + "value=" + value + ")");
    }
}
 
Example 3
Source File: CharSequenceCustomEncodingBytesWriter.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Bytes out, @NotNull CharSequence cs) {
    // Write the actual cs length for accurate StringBuilder.ensureCapacity() while reading
    out.writeStopBit(cs.length());
    long encodedSizePos = out.writePosition();
    out.writeSkip(4);
    charsetEncoder.reset();
    inputBuffer.clear();
    outputBuffer.clear();
    int csPos = 0;
    boolean endOfInput = false;
    // this loop inspired by the CharsetEncoder.encode(CharBuffer) implementation
    while (true) {
        if (!endOfInput) {
            int nextCsPos = Math.min(csPos + inputBuffer.remaining(), cs.length());
            append(inputBuffer, cs, csPos, nextCsPos);
            inputBuffer.flip();
            endOfInput = nextCsPos == cs.length();
            csPos = nextCsPos;
        }

        CoderResult cr = inputBuffer.hasRemaining() ?
                charsetEncoder.encode(inputBuffer, outputBuffer, endOfInput) :
                CoderResult.UNDERFLOW;

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

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

        if (cr.isOverflow()) {
            outputBuffer.flip();
            writeOutputBuffer(out);
            outputBuffer.clear();
            continue;
        }

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

    out.writeInt(encodedSizePos, (int) (out.writePosition() - encodedSizePos - 4));
}