use of io.servicetalk.serializer.api.SerializationException in project servicetalk by apple.
the class VarIntLengthStreamingSerializer method getVarInt.
static int getVarInt(Buffer buffer) {
final int maxBytesToInspect = min(MAX_LENGTH_BYTES, buffer.readableBytes());
final int readerIndex = buffer.readerIndex();
int i = 0;
for (; i < maxBytesToInspect; ++i) {
final byte b = buffer.getByte(i + readerIndex);
if ((b & 0x80) == 0) {
if (i == 0) {
return buffer.readByte();
} else if (i == 1) {
final short varInt = buffer.readShort();
return ((varInt & 0x7F) << 7) | ((varInt & 0x7F00) >> 8);
} else if (i == 2) {
final int varInt = buffer.readMedium();
return ((varInt & 0x7F) << 14) | ((varInt & 0x7F00) >> 1) | ((varInt & 0x7F0000) >> 16);
} else if (i == 3) {
final int varInt = buffer.readInt();
return ((varInt & 0x7F) << 21) | ((varInt & 0x7F00) << 6) | ((varInt & 0x7F0000) >> 9) | ((varInt & 0x7F000000) >> 24);
} else {
assert i == 4;
final byte b0 = buffer.readByte();
final int varInt = buffer.readInt();
if ((varInt & 0xF8) != 0) {
throw new SerializationException("java int cannot support larger than " + Integer.MAX_VALUE);
}
return ((varInt & 0x7) << 28) | ((varInt & 0x7F00) << 13) | ((varInt & 0x7F0000) >> 2) | ((varInt & 0x7F000000) >> 17) | (b0 & 0x7F);
}
}
}
if (i == MAX_LENGTH_BYTES) {
throw new SerializationException("java int cannot support more than " + MAX_LENGTH_BYTES + " bytes of VarInt");
}
return -1;
}
use of io.servicetalk.serializer.api.SerializationException in project servicetalk by apple.
the class HttpSerializerErrorTest method blockingStreamingDeserializationHeaderMismatch.
@ParameterizedTest
@MethodSource("executors")
void blockingStreamingDeserializationHeaderMismatch(HttpTestExecutionStrategy serverStrategy) throws Exception {
serverExecutionStrategy = serverStrategy.executorSupplier.get();
HttpStreamingSerializerDeserializer<String> streamingSerializer = jsonStreamingSerializer(JACKSON.streamingSerializerDeserializer(String.class));
try (ServerContext srv = HttpServers.forAddress(localAddress(0)).executionStrategy(serverExecutionStrategy).listenBlockingStreamingAndAwait((ctx, request, responseFactory) -> {
try {
BlockingIterable<String> reqIterable = request.payloadBody(streamingSerializer);
try (HttpPayloadWriter<String> stream = responseFactory.sendMetaData(streamingSerializer)) {
for (String reqChunk : reqIterable) {
stream.write(reqChunk);
}
}
} catch (SerializationException e) {
responseFactory.status(BAD_REQUEST);
responseFactory.sendMetaData().close();
}
});
BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
HttpResponse resp = clt.request(clt.post("/foo").payloadBody(clt.executionContext().bufferAllocator().fromAscii("hello")));
assertEquals(BAD_REQUEST, resp.status());
}
}
use of io.servicetalk.serializer.api.SerializationException in project servicetalk by apple.
the class HttpSerializerErrorTest method streamingDeserializationHeaderMismatch.
@ParameterizedTest
@MethodSource("executors")
void streamingDeserializationHeaderMismatch(HttpTestExecutionStrategy serverStrategy) throws Exception {
serverExecutionStrategy = serverStrategy.executorSupplier.get();
HttpStreamingSerializerDeserializer<String> streamingSerializer = jsonStreamingSerializer(JACKSON.streamingSerializerDeserializer(String.class));
try (ServerContext srv = HttpServers.forAddress(localAddress(0)).executionStrategy(serverExecutionStrategy).listenStreamingAndAwait((ctx, request, responseFactory) -> {
try {
return succeeded(responseFactory.ok().payloadBody(request.payloadBody(streamingSerializer), streamingSerializer));
} catch (SerializationException e) {
return succeeded(responseFactory.badRequest());
}
});
BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
HttpResponse resp = clt.request(clt.post("/foo").payloadBody(clt.executionContext().bufferAllocator().fromAscii("hello")));
assertEquals(BAD_REQUEST, resp.status());
}
}
use of io.servicetalk.serializer.api.SerializationException in project servicetalk by apple.
the class ProtobufSerializer method serialize.
@Override
public void serialize(final T toSerialize, final BufferAllocator allocator, final Buffer buffer) {
final int writerIdx = buffer.writerIndex();
final int writableBytes = buffer.writableBytes();
final CodedOutputStream out = buffer.hasArray() ? newInstance(buffer.array(), buffer.arrayOffset() + writerIdx, writableBytes) : newInstance(buffer.toNioBuffer(writerIdx, writableBytes));
try {
toSerialize.writeTo(out);
} catch (IOException e) {
throw new SerializationException(e);
}
// Forward write index of our buffer
buffer.writerIndex(writerIdx + toSerialize.getSerializedSize());
}
use of io.servicetalk.serializer.api.SerializationException in project servicetalk by apple.
the class ProtobufSerializer method deserialize.
@Override
public T deserialize(final Buffer serializedData, final BufferAllocator allocator) {
try {
final CodedInputStream in;
if (serializedData.nioBufferCount() == 1) {
in = CodedInputStream.newInstance(serializedData.toNioBuffer());
} else {
// Aggregated payload body may consist of multiple Buffers. In this case,
// CompositeBuffer.toNioBuffer(idx, length) may return a single ByteBuffer (when requested
// length < components[0].length) or create a new ByteBuffer and copy multiple components
// into it. Later, proto parser will copy data from this temporary ByteBuffer again.
// To avoid unnecessary copying, we use newCodedInputStream(buffers, lengthOfData).
final ByteBuffer[] buffers = serializedData.toNioBuffers();
in = buffers.length == 1 ? CodedInputStream.newInstance(buffers[0]) : newCodedInputStream(buffers, serializedData.readableBytes());
}
T result = parser.parseFrom(in);
serializedData.skipBytes(result.getSerializedSize());
return result;
} catch (InvalidProtocolBufferException e) {
throw new SerializationException(e);
}
}
Aggregations