Skip to content

Commit 8d2e825

Browse files
committed
Minor improvement to UTF-8 encoding reporting: should use JsonGenerationException instead of IllegalArgumentException
1 parent 5ebd2e7 commit 8d2e825

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,7 @@ && isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) {
18251825
* fit in the output buffer regardless of UTF-8 expansion.
18261826
*/
18271827
private final int _shortUTF8Encode(char[] str, int i, int end)
1828+
throws IOException
18281829
{
18291830
// First: let's see if it's all ASCII: that's rather fast
18301831
int ptr = _outputTail;
@@ -1847,6 +1848,7 @@ private final int _shortUTF8Encode(char[] str, int i, int end)
18471848
* characters.
18481849
*/
18491850
private final int _shortUTF8Encode2(char[] str, int i, int end, int outputPtr)
1851+
throws IOException
18501852
{
18511853
final byte[] outBuf = _outputBuffer;
18521854
while (i < end) {
@@ -1892,6 +1894,7 @@ private final int _shortUTF8Encode2(char[] str, int i, int end, int outputPtr)
18921894
}
18931895

18941896
private final int _shortUTF8Encode(String str, int i, int end)
1897+
throws IOException
18951898
{
18961899
// First: let's see if it's all ASCII: that's rather fast
18971900
int ptr = _outputTail;
@@ -1909,6 +1912,7 @@ private final int _shortUTF8Encode(String str, int i, int end)
19091912
}
19101913

19111914
private final int _shortUTF8Encode2(String str, int i, int end, int outputPtr)
1915+
throws IOException
19121916
{
19131917
final byte[] outBuf = _outputBuffer;
19141918
while (i < end) {
@@ -2094,28 +2098,33 @@ private void _mediumUTF8Encode(String str, int inputPtr, int inputEnd) throws IO
20942098
/**
20952099
* Method called to calculate UTF codepoint, from a surrogate pair.
20962100
*/
2097-
private int _convertSurrogate(int firstPart, int secondPart)
2101+
private int _convertSurrogate(int firstPart, int secondPart) throws IOException
20982102
{
20992103
// Ok, then, is the second part valid?
21002104
if (secondPart < SURR2_FIRST || secondPart > SURR2_LAST) {
2101-
throw new IllegalArgumentException("Broken surrogate pair: first char 0x"+Integer.toHexString(firstPart)+", second 0x"+Integer.toHexString(secondPart)+"; illegal combination");
2105+
String msg = String.format("Broken surrogate pair: first char 0x%04X, second 0x%04X; illegal combination",
2106+
firstPart, secondPart);
2107+
_reportError(msg);
21022108
}
21032109
return 0x10000 + ((firstPart - SURR1_FIRST) << 10) + (secondPart - SURR2_FIRST);
21042110
}
21052111

2106-
private void _throwIllegalSurrogate(int code)
2112+
private void _throwIllegalSurrogate(int code) throws IOException
21072113
{
21082114
if (code > 0x10FFFF) { // over max?
2109-
throw new IllegalArgumentException("Illegal character point (0x"+Integer.toHexString(code)+") to output; max is 0x10FFFF as per RFC 4627");
2115+
_reportError(String.format(
2116+
"Illegal character point (0x%X) to output; max is 0x10FFFF as per RFC 4627", code));
21102117
}
21112118
if (code >= SURR1_FIRST) {
21122119
if (code <= SURR1_LAST) { // Unmatched first part (closing without second part?)
2113-
throw new IllegalArgumentException("Unmatched first part of surrogate pair (0x"+Integer.toHexString(code)+")");
2120+
_reportError(String.format(
2121+
"Unmatched first part of surrogate pair (0x%04X)", code));
21142122
}
2115-
throw new IllegalArgumentException("Unmatched second part of surrogate pair (0x"+Integer.toHexString(code)+")");
2123+
_reportError(String.format(
2124+
"Unmatched second part of surrogate pair (0x%04X)", code));
21162125
}
21172126
// should we ever get this?
2118-
throw new IllegalArgumentException("Illegal character point (0x"+Integer.toHexString(code)+") to output");
2127+
_reportError(String.format("Illegal character point (0x%X) to output", code));
21192128
}
21202129

21212130
/*

0 commit comments

Comments
 (0)