Skip to content

Commit 2ce705b

Browse files
authored
Merge pull request #218 from microsoftgraph/bugfix/charset
minor encoding improvements
2 parents c5f4936 + 456d57c commit 2ce705b

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/main/java/com/microsoft/graph/http/CoreHttpProvider.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import java.io.OutputStream;
4141
import java.io.UnsupportedEncodingException;
4242
import java.net.URL;
43+
import java.nio.charset.Charset;
44+
import java.nio.charset.StandardCharsets;
4345
import java.util.List;
4446
import java.util.Locale;
4547
import java.util.Map;
@@ -74,7 +76,7 @@ public class CoreHttpProvider implements IHttpProvider<Request> {
7476
/**
7577
* The encoding type for getBytes
7678
*/
77-
private static final String JSON_ENCODING = "UTF-8";
79+
private static final Charset JSON_ENCODING = StandardCharsets.UTF_8;
7880
/**
7981
* The content type for JSON responses
8082
*/
@@ -295,14 +297,10 @@ public <Result, Body> Request getHttpRequest(@Nonnull final IHttpRequest request
295297
} else {
296298
logger.logDebug("Sending " + serializable.getClass().getName() + " as request body");
297299
final String serializeObject = serializer.serializeObject(serializable);
298-
try {
299-
bytesToWrite = serializeObject.getBytes(JSON_ENCODING);
300-
} catch (final UnsupportedEncodingException ex) {
301-
final ClientException clientException = new ClientException("Unsupported encoding problem: ",
302-
ex);
303-
logger.logError("Unsupported encoding problem: " + ex.getMessage(), ex);
304-
throw clientException;
305-
}
300+
if(serializeObject == null) {
301+
throw new ClientException("Error during serialization of request body, the result was null", null);
302+
}
303+
bytesToWrite = serializeObject.getBytes(JSON_ENCODING);
306304

307305
// If the user hasn't specified a Content-Type for the request
308306
if (!hasHeader(requestHeaders, CONTENT_TYPE_HEADER_NAME)) {
@@ -593,9 +591,8 @@ private Request convertIHttpRequestToOkHttpRequest(IHttpRequest request) {
593591
@Nullable
594592
public static String streamToString(@Nonnull final InputStream input) {
595593
Objects.requireNonNull(input, "parameter input cannot be null");
596-
final String httpStreamEncoding = "UTF-8";
597594
final String endOfFile = "\\A";
598-
try (final Scanner scanner = new Scanner(input, httpStreamEncoding)) {
595+
try (final Scanner scanner = new Scanner(input, JSON_ENCODING.name())) {
599596
scanner.useDelimiter(endOfFile);
600597
if (scanner.hasNext()) {
601598
return scanner.next();

0 commit comments

Comments
 (0)