Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit e405adb

Browse files
author
folomeev
committed
Get rid of StandardCharsets usage
1 parent ad70428 commit e405adb

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

libai/src/main/java/ai/api/AIDataService.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
import java.io.InputStream;
3535
import java.lang.reflect.Type;
3636
import java.net.*;
37-
import java.nio.charset.Charset;
38-
import java.nio.charset.StandardCharsets;
3937
import java.util.Calendar;
4038
import java.util.Collection;
4139
import java.util.Collections;
@@ -58,7 +56,6 @@
5856
public class AIDataService {
5957

6058
private static final Logger Log = LogManager.getLogger(AIDataService.class);
61-
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
6259
private static final AIServiceContext UNDEFINED_SERVICE_CONTEXT = null;
6360
private static final String REQUEST_METHOD_POST = "POST";
6461
private static final String REQUEST_METHOD_DELETE = "DELETE";
@@ -668,11 +665,11 @@ protected String doTextRequest(final String endpoint,
668665
connection.connect();
669666

670667
final BufferedOutputStream outputStream = new BufferedOutputStream(connection.getOutputStream());
671-
IOUtils.writeAll(queryData, outputStream, DEFAULT_CHARSET);
668+
IOUtils.writeAll(queryData, outputStream);
672669
outputStream.close();
673670

674671
final InputStream inputStream = new BufferedInputStream(connection.getInputStream());
675-
final String response = IOUtils.readAll(inputStream, DEFAULT_CHARSET);
672+
final String response = IOUtils.readAll(inputStream);
676673
inputStream.close();
677674

678675
return response;
@@ -681,7 +678,7 @@ protected String doTextRequest(final String endpoint,
681678
try {
682679
final InputStream errorStream = connection.getErrorStream();
683680
if (errorStream != null) {
684-
final String errorString = IOUtils.readAll(errorStream, DEFAULT_CHARSET);
681+
final String errorString = IOUtils.readAll(errorStream);
685682
Log.debug(errorString);
686683
return errorString;
687684
}
@@ -874,12 +871,12 @@ protected <TRequest,TResponse> TResponse doRequest(
874871

875872
if (queryData != null) {
876873
final BufferedOutputStream outputStream = new BufferedOutputStream(connection.getOutputStream());
877-
IOUtils.writeAll(queryData, outputStream, DEFAULT_CHARSET);
874+
IOUtils.writeAll(queryData, outputStream);
878875
outputStream.close();
879876
}
880877

881878
final InputStream inputStream = new BufferedInputStream(connection.getInputStream());
882-
final String response = IOUtils.readAll(inputStream, DEFAULT_CHARSET);
879+
final String response = IOUtils.readAll(inputStream);
883880
inputStream.close();
884881

885882
try {
@@ -898,7 +895,7 @@ protected <TRequest,TResponse> TResponse doRequest(
898895
try {
899896
final InputStream errorStream = connection.getErrorStream();
900897
if (errorStream != null) {
901-
final String errorString = IOUtils.readAll(errorStream, DEFAULT_CHARSET);
898+
final String errorString = IOUtils.readAll(errorStream);
902899
Log.debug(errorString);
903900
throw new AIServiceException(errorString, e);
904901
}

libai/src/main/java/ai/api/http/HttpClient.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import java.io.InputStream;
3131
import java.io.OutputStream;
3232
import java.net.HttpURLConnection;
33-
import java.nio.charset.Charset;
34-
import java.nio.charset.StandardCharsets;
3533

3634
import org.apache.logging.log4j.LogManager;
3735
import org.apache.logging.log4j.Logger;
@@ -41,7 +39,6 @@ public class HttpClient {
4139
private static final Logger Log = LogManager.getLogger(HttpClient.class);
4240
private static final int CHUNK_LENGTH = 2048;
4341
private static final int BUFFER_LENGTH = 4096;
44-
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
4542

4643
/**
4744
* Cannot be <code>null</code>
@@ -149,7 +146,7 @@ public void finishMultipart() throws IOException {
149146
*/
150147
public String getResponse() throws IOException {
151148
final InputStream inputStream = new BufferedInputStream(connection.getInputStream());
152-
final String response = IOUtils.readAll(inputStream, DEFAULT_CHARSET);
149+
final String response = IOUtils.readAll(inputStream);
153150
inputStream.close();
154151
return response;
155152
}
@@ -158,7 +155,7 @@ public String getErrorString() {
158155
try {
159156
final InputStream inputStream = new BufferedInputStream(connection.getErrorStream());
160157
final String response;
161-
response = IOUtils.readAll(inputStream, DEFAULT_CHARSET);
158+
response = IOUtils.readAll(inputStream);
162159
inputStream.close();
163160
return response;
164161
} catch (final IOException e) {

libai/src/main/java/ai/api/util/IOUtils.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
public class IOUtils {
1616

1717
private static final int DEFAULT_BUFFER_SIZE = 4096;
18+
private static final String DEFAULT_CHARSET = "UTF-8";
1819

1920
/**
2021
* Write string to byte stream
@@ -25,11 +26,36 @@ public class IOUtils {
2526
*/
2627
public static void writeAll(String data, OutputStream outputStream, Charset charset)
2728
throws IOException
29+
{
30+
writeAll(data, outputStream, charset.name());
31+
}
32+
33+
/**
34+
* Write string to byte stream
35+
* @param data Source string
36+
* @param outputStream Output stream
37+
* @param charset Convert string to bytes according to given
38+
* @throws IOException
39+
*/
40+
public static void writeAll(String data, OutputStream outputStream, String charset)
41+
throws IOException
2842
{
2943
if ((data != null) && (data.length() > 0)) {
3044
outputStream.write(data.getBytes(charset));
3145
}
3246
}
47+
48+
/**
49+
* Write string to byte stream
50+
* @param data Source string
51+
* @param outputStream Output stream
52+
* @throws IOException
53+
*/
54+
public static void writeAll(String data, OutputStream outputStream)
55+
throws IOException
56+
{
57+
writeAll(data, outputStream, DEFAULT_CHARSET);
58+
}
3359

3460
/**
3561
* Read all stream byte data into {@link String}
@@ -39,11 +65,32 @@ public static void writeAll(String data, OutputStream outputStream, Charset char
3965
* @throws IOException
4066
*/
4167
public static String readAll(InputStream inputStream, Charset charset) throws IOException {
68+
return readAll(inputStream, charset.name());
69+
}
70+
71+
/**
72+
* Read all stream byte data into {@link String}
73+
* @param inputStream Source stream
74+
* @param charset Convert bytes to chars according to given
75+
* @return Empty {@link String} if there was no data in stream
76+
* @throws IOException
77+
*/
78+
public static String readAll(InputStream inputStream, String charset) throws IOException {
4279
try (InputStreamReader streamReader = new InputStreamReader(inputStream, charset)) {
4380
return readAll(streamReader);
4481
}
4582
}
4683

84+
/**
85+
* Read all stream byte data into {@link String}
86+
* @param inputStream Source stream
87+
* @return Empty {@link String} if there was no data in stream
88+
* @throws IOException
89+
*/
90+
public static String readAll(InputStream inputStream) throws IOException {
91+
return readAll(inputStream, DEFAULT_CHARSET);
92+
}
93+
4794
/**
4895
* Read all chars into String
4996
* @param streamReader Input stream reader

0 commit comments

Comments
 (0)