diff --git a/libai/src/main/java/ai/api/AIDataService.java b/libai/src/main/java/ai/api/AIDataService.java index 384f57a..5cd9d78 100644 --- a/libai/src/main/java/ai/api/AIDataService.java +++ b/libai/src/main/java/ai/api/AIDataService.java @@ -34,8 +34,6 @@ import java.io.InputStream; import java.lang.reflect.Type; import java.net.*; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.Calendar; import java.util.Collection; import java.util.Collections; @@ -58,7 +56,6 @@ public class AIDataService { private static final Logger Log = LogManager.getLogger(AIDataService.class); - private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; private static final AIServiceContext UNDEFINED_SERVICE_CONTEXT = null; private static final String REQUEST_METHOD_POST = "POST"; private static final String REQUEST_METHOD_DELETE = "DELETE"; @@ -668,11 +665,11 @@ protected String doTextRequest(final String endpoint, connection.connect(); final BufferedOutputStream outputStream = new BufferedOutputStream(connection.getOutputStream()); - IOUtils.writeAll(queryData, outputStream, DEFAULT_CHARSET); + IOUtils.writeAll(queryData, outputStream); outputStream.close(); final InputStream inputStream = new BufferedInputStream(connection.getInputStream()); - final String response = IOUtils.readAll(inputStream, DEFAULT_CHARSET); + final String response = IOUtils.readAll(inputStream); inputStream.close(); return response; @@ -681,7 +678,7 @@ protected String doTextRequest(final String endpoint, try { final InputStream errorStream = connection.getErrorStream(); if (errorStream != null) { - final String errorString = IOUtils.readAll(errorStream, DEFAULT_CHARSET); + final String errorString = IOUtils.readAll(errorStream); Log.debug(errorString); return errorString; } @@ -874,12 +871,12 @@ protected TResponse doRequest( if (queryData != null) { final BufferedOutputStream outputStream = new BufferedOutputStream(connection.getOutputStream()); - IOUtils.writeAll(queryData, outputStream, DEFAULT_CHARSET); + IOUtils.writeAll(queryData, outputStream); outputStream.close(); } final InputStream inputStream = new BufferedInputStream(connection.getInputStream()); - final String response = IOUtils.readAll(inputStream, DEFAULT_CHARSET); + final String response = IOUtils.readAll(inputStream); inputStream.close(); try { @@ -898,7 +895,7 @@ protected TResponse doRequest( try { final InputStream errorStream = connection.getErrorStream(); if (errorStream != null) { - final String errorString = IOUtils.readAll(errorStream, DEFAULT_CHARSET); + final String errorString = IOUtils.readAll(errorStream); Log.debug(errorString); throw new AIServiceException(errorString, e); } diff --git a/libai/src/main/java/ai/api/http/HttpClient.java b/libai/src/main/java/ai/api/http/HttpClient.java index 88528a2..0e85142 100644 --- a/libai/src/main/java/ai/api/http/HttpClient.java +++ b/libai/src/main/java/ai/api/http/HttpClient.java @@ -30,8 +30,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -41,7 +39,6 @@ public class HttpClient { private static final Logger Log = LogManager.getLogger(HttpClient.class); private static final int CHUNK_LENGTH = 2048; private static final int BUFFER_LENGTH = 4096; - private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; /** * Cannot be null @@ -149,7 +146,7 @@ public void finishMultipart() throws IOException { */ public String getResponse() throws IOException { final InputStream inputStream = new BufferedInputStream(connection.getInputStream()); - final String response = IOUtils.readAll(inputStream, DEFAULT_CHARSET); + final String response = IOUtils.readAll(inputStream); inputStream.close(); return response; } @@ -158,7 +155,7 @@ public String getErrorString() { try { final InputStream inputStream = new BufferedInputStream(connection.getErrorStream()); final String response; - response = IOUtils.readAll(inputStream, DEFAULT_CHARSET); + response = IOUtils.readAll(inputStream); inputStream.close(); return response; } catch (final IOException e) { diff --git a/libai/src/main/java/ai/api/util/IOUtils.java b/libai/src/main/java/ai/api/util/IOUtils.java index 5f8d3ff..e3d56dc 100644 --- a/libai/src/main/java/ai/api/util/IOUtils.java +++ b/libai/src/main/java/ai/api/util/IOUtils.java @@ -15,6 +15,7 @@ public class IOUtils { private static final int DEFAULT_BUFFER_SIZE = 4096; + private static final String DEFAULT_CHARSET = "UTF-8"; /** * Write string to byte stream @@ -25,11 +26,36 @@ public class IOUtils { */ public static void writeAll(String data, OutputStream outputStream, Charset charset) throws IOException + { + writeAll(data, outputStream, charset.name()); + } + + /** + * Write string to byte stream + * @param data Source string + * @param outputStream Output stream + * @param charset Convert string to bytes according to given + * @throws IOException + */ + public static void writeAll(String data, OutputStream outputStream, String charset) + throws IOException { if ((data != null) && (data.length() > 0)) { outputStream.write(data.getBytes(charset)); } } + + /** + * Write string to byte stream + * @param data Source string + * @param outputStream Output stream + * @throws IOException + */ + public static void writeAll(String data, OutputStream outputStream) + throws IOException + { + writeAll(data, outputStream, DEFAULT_CHARSET); + } /** * Read all stream byte data into {@link String} @@ -39,11 +65,32 @@ public static void writeAll(String data, OutputStream outputStream, Charset char * @throws IOException */ public static String readAll(InputStream inputStream, Charset charset) throws IOException { + return readAll(inputStream, charset.name()); + } + + /** + * Read all stream byte data into {@link String} + * @param inputStream Source stream + * @param charset Convert bytes to chars according to given + * @return Empty {@link String} if there was no data in stream + * @throws IOException + */ + public static String readAll(InputStream inputStream, String charset) throws IOException { try (InputStreamReader streamReader = new InputStreamReader(inputStream, charset)) { return readAll(streamReader); } } + /** + * Read all stream byte data into {@link String} + * @param inputStream Source stream + * @return Empty {@link String} if there was no data in stream + * @throws IOException + */ + public static String readAll(InputStream inputStream) throws IOException { + return readAll(inputStream, DEFAULT_CHARSET); + } + /** * Read all chars into String * @param streamReader Input stream reader