Fix CharSequenceValueConverter.convertToByte implementation for AsciiString (#7994)
Motivation: The implementation of CharSequenceValueConverter.convertToByte did not correctly handle AsciiString if the length != 1. Modifications: - Only use fast-path for AsciiString with length of 1. - Add unit tests. Result: Fixes https://github.com/netty/netty/issues/7990
This commit is contained in:
parent
a4393831f0
commit
1611acf4ce
@ -77,7 +77,7 @@ public class CharSequenceValueConverter implements ValueConverter<CharSequence>
|
||||
|
||||
@Override
|
||||
public byte convertToByte(CharSequence value) {
|
||||
if (value instanceof AsciiString) {
|
||||
if (value instanceof AsciiString && value.length() == 1) {
|
||||
return ((AsciiString) value).byteAt(0);
|
||||
}
|
||||
return Byte.parseByte(value.toString());
|
||||
|
@ -14,6 +14,7 @@
|
||||
*/
|
||||
package io.netty.handler.codec;
|
||||
|
||||
import io.netty.util.AsciiString;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@ -30,6 +31,16 @@ public class CharSequenceValueConverterTest {
|
||||
assertFalse(converter.convertToBoolean(converter.convertBoolean(false)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteFromAsciiString() {
|
||||
assertEquals(127, converter.convertToByte(AsciiString.of("127")));
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testByteFromEmptyAsciiString() {
|
||||
converter.convertToByte(AsciiString.EMPTY_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByte() {
|
||||
assertEquals(Byte.MAX_VALUE, converter.convertToByte(converter.convertByte(Byte.MAX_VALUE)));
|
||||
|
Loading…
Reference in New Issue
Block a user