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

Commit

Permalink
Get rid of StandardCharsets usage
Browse files Browse the repository at this point in the history
  • Loading branch information
folomeev committed Jan 23, 2017
1 parent ad70428 commit e405adb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
15 changes: 6 additions & 9 deletions libai/src/main/java/ai/api/AIDataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -874,12 +871,12 @@ protected <TRequest,TResponse> 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 {
Expand All @@ -898,7 +895,7 @@ protected <TRequest,TResponse> 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);
}
Expand Down
7 changes: 2 additions & 5 deletions libai/src/main/java/ai/api/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <code>null</code>
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down
47 changes: 47 additions & 0 deletions libai/src/main/java/ai/api/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand All @@ -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
Expand Down

0 comments on commit e405adb

Please sign in to comment.