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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -54,6 +52,7 @@ import javax.servlet.http.HttpServletRequest;
import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.URLCodec; import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.io.IOUtils;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
@ -135,32 +134,13 @@ public class ParsingUtilities {
} }
static public String inputStreamToString(InputStream is, String encoding) throws IOException { static public String inputStreamToString(InputStream is, String encoding) throws IOException {
Reader reader; InputStream uncompressedStream = is;
if (encoding.equals("gzip")) { // Historical special case only used by tests. Probably can be removed.
InputStream inputStream = new GZIPInputStream(is); if ("gzip".equals(encoding)) {
reader = new InputStreamReader(inputStream); uncompressedStream = new GZIPInputStream(is);
} else { encoding = "UTF-8";
reader = new InputStreamReader(is, encoding);
} }
return IOUtils.toString(uncompressedStream, encoding);
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();
} }
private static final URLCodec codec = new URLCodec(); private static final URLCodec codec = new URLCodec();