Java Code Examples for io.protostuff.StringSerializer

The following examples show how to use io.protostuff.StringSerializer. These examples are extracted from open source projects. 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 Project: sofa-jraft   Source File: NioBufInput.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String readString() throws IOException {
    final int length = readRawVarInt32();
    if (length < 0) {
        throw ProtocolException.negativeSize();
    }

    if (nioBuffer.remaining() < length) {
        throw ProtocolException.misreportedSize();
    }

    final int position = nioBuffer.position();
    String result;
    if (nioBuffer.hasArray()) {
        if (UnsafeUtil.hasUnsafe()) {
            nioBuffer.position(position + length);
            result = UnsafeUtf8Util.decodeUtf8(nioBuffer.array(), nioBuffer.arrayOffset() + position, length);
        } else {
            nioBuffer.position(position + length);
            result = StringSerializer.STRING.deser(nioBuffer.array(), nioBuffer.arrayOffset() + position, length);
        }
    } else {
        if (UnsafeUtil.hasUnsafe()) {
            nioBuffer.position(position + length);
            result = UnsafeUtf8Util.decodeUtf8Direct(nioBuffer, position, length);
        } else {
            final byte[] tmp = new byte[length];
            nioBuffer.get(tmp);
            result = StringSerializer.STRING.deser(tmp);
        }
    }
    return result;
}
 
Example 2
Source Project: Jupiter   Source File: NioBufInput.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String readString() throws IOException {
    final int length = readRawVarInt32();
    if (length < 0) {
        throw ProtocolException.negativeSize();
    }

    if (nioBuffer.remaining() < length) {
        throw ProtocolException.misreportedSize();
    }

    final int position = nioBuffer.position();
    String result;
    if (nioBuffer.hasArray()) {
        if (UnsafeUtil.hasUnsafe()) {
            result = UnsafeUtf8Util.decodeUtf8(nioBuffer.array(), nioBuffer.arrayOffset() + position, length);
        } else {
            result = StringSerializer.STRING.deser(nioBuffer.array(), nioBuffer.arrayOffset() + position, length);
        }
        nioBuffer.position(position + length);
    } else {
        if (UnsafeUtil.hasUnsafe()) {
            result = UnsafeUtf8Util.decodeUtf8Direct(nioBuffer, position, length);
            nioBuffer.position(position + length);
        } else {
            byte[] tmp = new byte[length];
            nioBuffer.get(tmp);
            return StringSerializer.STRING.deser(tmp);
        }
    }
    return result;
}
 
Example 3
Source Project: protostuff   Source File: StringSerializerBenchmark.java    License: Apache License 2.0 5 votes vote down vote up
@Benchmark
public byte[] bufferedSerializer()
{
    try
    {
        final WriteSession session = new WriteSession(sharedBuffer);
        StringSerializer.writeUTF8(s, session, sharedBuffer);
        return session.toByteArray();
    }
    finally
    {
        sharedBuffer.clear();
    }
}
 
Example 4
Source Project: protostuff   Source File: StringSerializerBenchmark.java    License: Apache License 2.0 5 votes vote down vote up
@Benchmark
public byte[] bufferedRecycledSerializer()
{
    final WriteSession session = this.sharedSession;
    try
    {
        StringSerializer.writeUTF8(s, session, session.head);
        return session.toByteArray();
    }
    finally
    {
        session.clear();
    }
}