io.protostuff.StringSerializer Java Examples

The following examples show how to use io.protostuff.StringSerializer. 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: NioBufInput.java    From sofa-jraft with 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 File: NioBufInput.java    From Jupiter with 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 File: StringSerializerBenchmark.java    From protostuff with 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 File: StringSerializerBenchmark.java    From protostuff with 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();
    }
}