Skip to content

Commit bbaf10e

Browse files
committed
Remove dead catch block from MultipartContentDecoder.decodeBytes
With CodingErrorAction.REPLACE the decoder never throws CharacterCodingException, making the catch branch unreachable.
1 parent 22536b1 commit bbaf10e

2 files changed

Lines changed: 6 additions & 19 deletions

File tree

internal-api/src/main/java/datadog/trace/api/http/MultipartContentDecoder.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package datadog.trace.api.http;
22

33
import java.nio.ByteBuffer;
4-
import java.nio.charset.CharacterCodingException;
54
import java.nio.charset.Charset;
65
import java.nio.charset.CodingErrorAction;
76

@@ -11,16 +10,12 @@ public final class MultipartContentDecoder {
1110
public static String decodeBytes(byte[] buf, int length, String contentType) {
1211
Charset charset = extractCharset(contentType);
1312
if (charset == null) charset = Charset.defaultCharset();
14-
try {
15-
return charset
16-
.newDecoder()
17-
.onMalformedInput(CodingErrorAction.REPLACE)
18-
.onUnmappableCharacter(CodingErrorAction.REPLACE)
19-
.decode(ByteBuffer.wrap(buf, 0, length))
20-
.toString();
21-
} catch (CharacterCodingException e) {
22-
return new String(buf, 0, length, Charset.defaultCharset());
23-
}
13+
return charset
14+
.newDecoder()
15+
.onMalformedInput(CodingErrorAction.REPLACE)
16+
.onUnmappableCharacter(CodingErrorAction.REPLACE)
17+
.decode(ByteBuffer.wrap(buf, 0, length))
18+
.toString();
2419
}
2520

2621
public static Charset extractCharset(String contentType) {

internal-api/src/test/java/datadog/trace/api/http/MultipartContentDecoderTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNull;
55

6-
import java.nio.charset.Charset;
76
import java.nio.charset.StandardCharsets;
87
import org.junit.jupiter.api.Test;
98
import org.junit.jupiter.params.ParameterizedTest;
@@ -56,13 +55,6 @@ void decodeBytesReturnsEmptyStringForZeroLength() {
5655
assertEquals("", MultipartContentDecoder.decodeBytes(new byte[16], 0, null));
5756
}
5857

59-
@Test
60-
void decodeBytesUsesMachineDefaultFallbackWhenBytesCannotBeDecoded() {
61-
byte[] bytes = "café".getBytes(StandardCharsets.ISO_8859_1);
62-
String expected = new String(bytes, Charset.defaultCharset());
63-
assertEquals(expected, MultipartContentDecoder.decodeBytes(bytes, bytes.length, null));
64-
}
65-
6658
@Test
6759
void decodeBytesReplacesMalformedBytesWithReplacementCharacterUsingDeclaredCharset() {
6860
// 0xE9 (ISO-8859-1 'é') is not valid UTF-8; REPLACE substitutes U+FFFD

0 commit comments

Comments
 (0)