优化代码,使用writerIndex标记代替Slice

master
剑器近 2021-07-23 09:26:24 +08:00
parent 7c1f0bce6c
commit 15943a2e55
3 changed files with 19 additions and 5 deletions

View File

@ -37,7 +37,11 @@ public class CollectionSchema<T> implements Schema<List<T>> {
@Override
public List<T> readFrom(ByteBuf input, int length) {
return this.readFrom(input.readSlice(length));
int writerIndex = input.writerIndex();
input.writerIndex(input.readerIndex() + length);
List<T> result = this.readFrom(input);
input.writerIndex(writerIndex);
return result;
}
@Override

View File

@ -35,8 +35,13 @@ public class ConvertSchema<T> implements Schema<T> {
@Override
public T readFrom(ByteBuf input, int length) {
if (length > 0)
input = input.readSlice(length);
if (length > 0) {
int writerIndex = input.writerIndex();
input.writerIndex(input.readerIndex() + length);
T result = converter.convert(input);
input.writerIndex(writerIndex);
return result;
}
return converter.convert(input);
}

View File

@ -25,8 +25,13 @@ public class ObjectSchema<T> implements Schema<T> {
@Override
public T readFrom(ByteBuf input, int length) {
if (length > 0)
input = input.readSlice(length);
if (length > 0) {
int writerIndex = input.writerIndex();
input.writerIndex(input.readerIndex() + length);
T result = schema.readFrom(input);
input.writerIndex(writerIndex);
return result;
}
return schema.readFrom(input);
}