Skip to content

Commit

Permalink
Deprecate ContentCodec API with offest & length (#1440)
Browse files Browse the repository at this point in the history
* Deprecate ContentCodec API with offset & length
  • Loading branch information
tkountis authored Mar 16, 2021
1 parent 4d60e67 commit 1306f84
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ abstract class AbstractZipContentCodec extends AbstractContentCodec {

abstract InflaterInputStream newInflaterInputStream(InputStream in) throws IOException;

@Override
public Buffer encode(final Buffer src, final BufferAllocator allocator) {
return encode(src, 0, src.readableBytes(), allocator);
}

@Override
public final Buffer encode(final Buffer src, final int offset, final int length, final BufferAllocator allocator) {
if (offset < 0) {
Expand Down Expand Up @@ -187,6 +192,11 @@ public void onComplete() {
});
}

@Override
public Buffer decode(final Buffer src, final BufferAllocator allocator) {
return decode(src, 0, src.readableBytes(), allocator);
}

@Override
public final Buffer decode(final Buffer src, final int offset, final int length, final BufferAllocator allocator) {
if (offset < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public interface ContentCodec {
* @param allocator the {@link BufferAllocator} to use for allocating auxiliary buffers or the returned buffer
* @return {@link Buffer} the result buffer with the content encoded
*/
default Buffer encode(Buffer src, BufferAllocator allocator) {
return encode(src, 0, src.readableBytes(), allocator);
}
Buffer encode(Buffer src, BufferAllocator allocator);

/**
* Take a {@link Buffer} and encode its contents resulting in a {@link Buffer} with the encoded contents.
Expand All @@ -56,7 +54,10 @@ default Buffer encode(Buffer src, BufferAllocator allocator) {
* @param length the total count of bytes to read
* @param allocator the {@link BufferAllocator} to use for allocating auxiliary buffers or the returned buffer
* @return {@link Buffer} the result buffer with the content encoded
* @deprecated Use the plain {@link #encode(Buffer, BufferAllocator)} version and {@link Buffer#slice(int, int)}
* where needed.
*/
@Deprecated
Buffer encode(Buffer src, int offset, int length, BufferAllocator allocator);

/**
Expand All @@ -68,9 +69,7 @@ default Buffer encode(Buffer src, BufferAllocator allocator) {
* @param allocator the {@link BufferAllocator} to use for allocating auxiliary buffers or the returned buffer
* @return {@link Buffer} the result buffer with the content decoded
*/
default Buffer decode(Buffer src, BufferAllocator allocator) {
return decode(src, 0, src.readableBytes(), allocator);
}
Buffer decode(Buffer src, BufferAllocator allocator);

/**
* Take a {@link Buffer} and decode its contents resulting in a {@link Buffer} with the decoded content.
Expand All @@ -82,7 +81,10 @@ default Buffer decode(Buffer src, BufferAllocator allocator) {
* @param length the total count of bytes to read
* @param allocator the {@link BufferAllocator} to use for allocating auxiliary buffers or the returned buffer
* @return {@link Buffer} the result buffer with the content decoded
* @deprecated Use the plain {@link #decode(Buffer, BufferAllocator)} version and {@link Buffer#slice(int, int)}
* where needed.
*/
@Deprecated
Buffer decode(Buffer src, int offset, int length, BufferAllocator allocator);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public CharSequence name() {
return NAME;
}

@Override
public Buffer encode(final Buffer src, final BufferAllocator allocator) {
return src;
}

@Override
public Buffer decode(final Buffer src, final BufferAllocator allocator) {
return src;
}

@Override
public Buffer encode(final Buffer src, final int offset, final int length, final BufferAllocator allocator) {
return src;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,18 @@ final class NettyChannelContentCodec extends AbstractContentCodec {
}

@Override
public Buffer encode(final Buffer src, final int offset, final int length, final BufferAllocator allocator) {
public Buffer encode(final Buffer src, final BufferAllocator allocator) {
requireNonNull(allocator);

final Buffer slice = src.slice(src.readerIndex() + offset, length);
if (slice.readableBytes() == 0) {
if (src.readableBytes() == 0) {
throw new CodecEncodingException(this, "No data to encode.");
}
// Consume input buffer
src.skipBytes(offset + length);

final MessageToByteEncoder<ByteBuf> encoder = encoderSupplier.get();
final EmbeddedChannel channel = newEmbeddedChannel(encoder, allocator);

try {
ByteBuf origin = extractByteBufOrCreate(slice);
ByteBuf origin = extractByteBufOrCreate(src);
channel.writeOutbound(origin);

// May produce footer
Expand All @@ -99,6 +96,14 @@ public Buffer encode(final Buffer src, final int offset, final int length, final
}
}

@Override
public Buffer encode(final Buffer src, final int offset, final int length, final BufferAllocator allocator) {
final Buffer slice = src.slice(src.readerIndex() + offset, length);
// Consume input buffer
src.skipBytes(offset + length);
return encode(slice, allocator);
}

@Override
public Publisher<Buffer> encode(final Publisher<Buffer> from, final BufferAllocator allocator) {
requireNonNull(from);
Expand Down Expand Up @@ -178,22 +183,18 @@ public void onComplete() {
}

@Override
public Buffer decode(final Buffer src, final int offset, final int length, final BufferAllocator allocator) {
public Buffer decode(final Buffer src, final BufferAllocator allocator) {
requireNonNull(allocator);

final Buffer slice = src.slice(src.readerIndex() + offset, length);
if (slice.readableBytes() == 0) {
if (src.readableBytes() == 0) {
throw new CodecEncodingException(this, "No data to encode.");
}

// Consume input buffer
src.skipBytes(offset + length);

final ByteToMessageDecoder decoder = decoderSupplier.get();
final EmbeddedChannel channel = newEmbeddedChannel(decoder, allocator);

try {
ByteBuf origin = extractByteBufOrCreate(slice);
ByteBuf origin = extractByteBufOrCreate(src);
channel.writeInbound(origin);

Buffer buffer = drainChannelQueueToSingleBuffer(channel.inboundMessages(), allocator);
Expand All @@ -212,6 +213,14 @@ public Buffer decode(final Buffer src, final int offset, final int length, final
}
}

@Override
public Buffer decode(final Buffer src, final int offset, final int length, final BufferAllocator allocator) {
final Buffer slice = src.slice(src.readerIndex() + offset, length);
// Consume input buffer
src.skipBytes(offset + length);
return decode(slice, allocator);
}

@Override
public Publisher<Buffer> decode(final Publisher<Buffer> from, final BufferAllocator allocator) {
requireNonNull(from);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,41 +76,31 @@ public static ContentCodec[] params() {

@Test
public void testEncode() {
testEncode(DEFAULT_ALLOCATOR, 0);
testEncode(DEFAULT_ALLOCATOR);
}

@Test
public void testEncodeWithOffset() {
testEncode(DEFAULT_ALLOCATOR, 10);
public void testEncodeWithReadOnlyBuffer() {
testEncode(DEFAULT_RO_ALLOCATOR);
}

@Test(expected = CodecEncodingException.class)
public void testEncodeWithOffsetAndZeroLength() {
testEncode(DEFAULT_ALLOCATOR, 10, 0);
}

@Test(expected = CodecEncodingException.class)
public void testEncodeWithOffsetAndOverflowLength() {
testEncode(DEFAULT_ALLOCATOR, 1023, 0);
}

@Test
public void testEncodeWithOffsetAndReadOnlyBuffer() {
testEncode(DEFAULT_RO_ALLOCATOR, 10);
testEncode(DEFAULT_ALLOCATOR, 0);
}

private void testEncode(final BufferAllocator allocator, final int offset) {
testEncode(allocator, offset, INPUT.length() - offset);
private void testEncode(final BufferAllocator allocator) {
testEncode(allocator, INPUT.length());
}

private void testEncode(final BufferAllocator allocator, final int offset, final int length) {
private void testEncode(final BufferAllocator allocator, final int length) {
Buffer source = allocator.fromAscii(INPUT);
Buffer encoded = codec.encode(source, offset, length, DEFAULT_ALLOCATOR);
Buffer encoded = codec.encode(source.readSlice(length), DEFAULT_ALLOCATOR);

assertThat(encoded, notNullValue());

Buffer decoded = codec.decode(encoded, DEFAULT_ALLOCATOR);
assertThat(decoded.toString(US_ASCII), equalTo(INPUT.substring(offset)));
assertThat(decoded.toString(US_ASCII), equalTo(INPUT.substring(0, length)));
}

@Test(expected = CodecDecodingException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,13 @@ public String name() {
}

@Override
public Buffer encode(final Buffer src, final int offset, final int length,
final BufferAllocator allocator) {
src.readerIndex(src.readerIndex() + offset);

public Buffer encode(final Buffer src, final BufferAllocator allocator) {
final Buffer dst = allocator.newBuffer(OUGHT_TO_BE_ENOUGH);
DeflaterOutputStream output = null;
try {
output = new GZIPOutputStream(asOutputStream(dst));
output.write(src.array(), src.arrayOffset() + src.readerIndex(), length);
src.readerIndex(src.readerIndex() + length);
output.write(src.array(), src.arrayOffset() + src.readerIndex(), src.readableBytes());
src.skipBytes(src.readableBytes());
output.finish();
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -136,10 +133,12 @@ public Buffer encode(final Buffer src, final int offset, final int length,
}

@Override
public Buffer decode(final Buffer src, final int offset, final int length,
final BufferAllocator allocator) {
src.readerIndex(src.readerIndex() + offset);
public Buffer encode(final Buffer src, final int offset, final int length, final BufferAllocator allocator) {
throw new UnsupportedOperationException();
}

@Override
public Buffer decode(final Buffer src, final BufferAllocator allocator) {
final Buffer dst = allocator.newBuffer(OUGHT_TO_BE_ENOUGH);
InflaterInputStream input = null;
try {
Expand All @@ -156,6 +155,11 @@ public Buffer decode(final Buffer src, final int offset, final int length,
return dst;
}

@Override
public Buffer decode(final Buffer src, final int offset, final int length, final BufferAllocator allocator) {
throw new UnsupportedOperationException();
}

@Override
public Publisher<Buffer> encode(final Publisher<Buffer> from, final BufferAllocator allocator) {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public Iterable<T> deserialize(Buffer toDeserialize) {
Buffer buffer = toDeserialize;
int decodedLengthOfData = lengthOfData;
if (compressed) {
buffer = codec.decode(toDeserialize, 0, lengthOfData, DEFAULT_ALLOCATOR);
buffer = codec.decode(toDeserialize.readSlice(lengthOfData), DEFAULT_ALLOCATOR);
decodedLengthOfData = buffer.readableBytes();
}

Expand Down Expand Up @@ -306,7 +306,7 @@ private void serializeAndEncode(final MessageLite msg, final Buffer destination)
Buffer serialized = DEFAULT_ALLOCATOR.newBuffer(size);
serialize0(msg, serialized);

Buffer encoded = codec.encode(serialized, 0, serialized.readableBytes(), DEFAULT_ALLOCATOR);
Buffer encoded = codec.encode(serialized, DEFAULT_ALLOCATOR);

destination.writeByte(FLAG_COMPRESSED);
destination.writeInt(encoded.readableBytes());
Expand Down

0 comments on commit 1306f84

Please sign in to comment.