Skip to content

Commit

Permalink
Fix automatic character encoding detection
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
leadpony committed Jan 23, 2021
1 parent 7a540f4 commit 1479206
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the Joy Authors.
* Copyright 2019-2021 the Joy Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
6 changes: 5 additions & 1 deletion joy-core/src/main/java/org/leadpony/joy/core/Message.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the Joy Authors.
* Copyright 2019-2021 the Joy Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -102,6 +102,10 @@ public static String thatParserHasBeenAlreadyClosed() {
return format("ParserHasBeenAlreadyClosed");
}

public static String thatCharacterEncodingCannotBeDetected() {
return format("CharacterEncodingCannotBeDetected");
}

/*
* Messages for JSON generator
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the Joy Authors.
* Copyright 2019-2021 the Joy Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the Joy Authors.
* Copyright 2019-2021 the Joy Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -72,6 +72,9 @@ static Reader createStreamReader(InputStream in) {
int b2 = in.read();
if (b2 < 0) {
// 1 letter
if (b1 == 0) {
throw unknownEncodingException();
}
return createReader(in, DEFAULT_ENCODING, (byte) b1);
}

Expand Down Expand Up @@ -154,6 +157,10 @@ private static Reader createReader(InputStream in, Charset charset, byte... byte
return new InputStreamReader(s, charset);
}

private static JsonException unknownEncodingException() {
return new JsonException(Message.thatCharacterEncodingCannotBeDetected());
}

private StreamReaders() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ IOErrorOccurredWhileParserWasClosing=An I/O error occurred while the parser was

ParserHasBeenAlreadyClosed=The parser has been already closed.

CharacterEncodingCannotBeDetected=Character encoding cannot be detected automatically.

#
# Messages for JsonGenerator
#
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<dependency>
<groupId>org.leadpony</groupId>
<artifactId>jsonp-test-suite</artifactId>
<version>2.1.0-SNAPSHOT</version>
<version>2.1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down

0 comments on commit 1479206

Please sign in to comment.