Use Apache IO Commons IOUtils instead of homerolled (#2845)

Probably should remove the funky Gzip support with the
overloaded use of the encoding parameter, but this is
a start.
This commit is contained in:
Tom Morris 2020-06-30 07:49:47 -04:00 committed by GitHub
parent e2a2dd2a4e
commit 54291ef441
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -35,8 +35,6 @@ package com.google.refine.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.time.Instant;
import java.time.LocalDateTime;
@ -54,6 +52,7 @@ import javax.servlet.http.HttpServletRequest;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.io.IOUtils;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
@ -135,32 +134,13 @@ public class ParsingUtilities {
}
static public String inputStreamToString(InputStream is, String encoding) throws IOException {
Reader reader;
if (encoding.equals("gzip")) {
InputStream inputStream = new GZIPInputStream(is);
reader = new InputStreamReader(inputStream);
} else {
reader = new InputStreamReader(is, encoding);
InputStream uncompressedStream = is;
// Historical special case only used by tests. Probably can be removed.
if ("gzip".equals(encoding)) {
uncompressedStream = new GZIPInputStream(is);
encoding = "UTF-8";
}
try {
return readerToString(reader);
} finally {
reader.close();
}
}
static public String readerToString(Reader reader) throws IOException {
StringBuffer sb = new StringBuffer();
char[] chars = new char[8192];
int c;
while ((c = reader.read(chars)) > 0) {
sb.insert(sb.length(), chars, 0, c);
}
return sb.toString();
return IOUtils.toString(uncompressedStream, encoding);
}
private static final URLCodec codec = new URLCodec();