File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed
main/java/io/netty/buffer
test/java/io/netty/buffer Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change 15
15
*/
16
16
package io .netty .buffer ;
17
17
18
- import io .netty .util .CharsetUtil ;
19
18
import io .netty .util .internal .ObjectUtil ;
20
19
21
20
import java .io .DataOutput ;
@@ -102,7 +101,16 @@ public void writeByte(int v) throws IOException {
102
101
103
102
@ Override
104
103
public void writeBytes (String s ) throws IOException {
105
- buffer .writeCharSequence (s , CharsetUtil .US_ASCII );
104
+ // We don't use `ByteBuf.writeCharSequence` here, because `writeBytes` is specified to only write the
105
+ // lower-order by of multibyte characters (exactly one byte per character in the string), while
106
+ // `writeCharSequence` will instead write a '?' replacement character.
107
+ int length = s .length ();
108
+ buffer .ensureWritable (length );
109
+ int offset = buffer .writerIndex ();
110
+ for (int i = 0 ; i < length ; i ++) {
111
+ buffer .setByte (offset + i , (byte ) s .charAt (i ));
112
+ }
113
+ buffer .writerIndex (offset + length );
106
114
}
107
115
108
116
@ Override
Original file line number Diff line number Diff line change 15
15
*/
16
16
package io .netty .buffer ;
17
17
18
- import io .netty .util .ReferenceCountUtil ;
19
18
import org .junit .jupiter .api .Test ;
20
19
import org .junit .jupiter .api .function .Executable ;
21
20
@@ -351,4 +350,18 @@ public void testGeneralByteBufOutputStream() throws Exception {
351
350
out .buffer ().release ();
352
351
assertEquals (0 , out .buffer ().refCnt ());
353
352
}
353
+
354
+ @ Test
355
+ void writeStringMustIgnoreHigherOrderByte () throws Exception {
356
+ ByteBuf buf = Unpooled .buffer ();
357
+ ByteBufOutputStream out = new ByteBufOutputStream (buf , false );
358
+ try {
359
+ out .writeBytes ("√" );
360
+ } finally {
361
+ out .close ();
362
+ }
363
+ assertEquals (0x221A , '√' ); // This is a multibyte character
364
+ assertEquals (0x1A , buf .readByte ()); // Only the lower-order byte is written
365
+ assertEquals (0 , buf .readableBytes ());
366
+ }
354
367
}
You can’t perform that action at this time.
0 commit comments