Skip to content

Commit

Permalink
Fix misaligned buffer allocation method #1367
Browse files Browse the repository at this point in the history
  • Loading branch information
dmandalidis authored Sep 4, 2020
1 parent a2bc546 commit 57489fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/main/java/io/lettuce/core/codec/StringCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,17 @@ public void encode(String str, ByteBuf target) {
int sizeOf(String value, boolean estimate) {

if (estimate) {
return (int) (averageBytesPerChar * value.length());
return (int) averageBytesPerChar * value.length();
}

return (int) (maxBytesPerChar * value.length());
if (utf8) {
return ByteBufUtil.utf8MaxBytes(value);
}

if (ascii) {
return value.length();
}

return (int) maxBytesPerChar * value.length();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.jupiter.api.Test;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;

/**
Expand Down Expand Up @@ -124,7 +125,7 @@ void estimateSize() {
void sizeOf() {

assertThat(new StringCodec(StandardCharsets.UTF_8).sizeOf(teststring, false))
.isEqualTo(teststring.length() * 3);
.isEqualTo(ByteBufUtil.utf8MaxBytes(teststring));
assertThat(new StringCodec(StandardCharsets.US_ASCII).sizeOf(teststring, false))
.isEqualTo(teststring.length());
assertThat(new StringCodec(StandardCharsets.ISO_8859_1).sizeOf(teststring, false))
Expand Down

0 comments on commit 57489fb

Please sign in to comment.