Skip to content

Commit

Permalink
allow forced gzip read
Browse files Browse the repository at this point in the history
  • Loading branch information
bvolpato committed Aug 21, 2017
1 parent 725837e commit e916573
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/main/java/org/brunocvcunha/inutils4j/MyHTTPUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
Expand All @@ -28,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream;

/**
* HTTP (In)utilities.
Expand All @@ -44,10 +47,32 @@ public class MyHTTPUtils {
* @throws IOException I/O error happened
*/
public static String getContent(String stringUrl) throws IOException {
URL url = new URL(stringUrl);
return MyStreamUtils.readContent(url.openStream());
InputStream stream = getContentStream(stringUrl);
return MyStreamUtils.readContent(stream);
}

/**
* Get stream for URL only
*
* @param stringUrl URL to get content
* @return the input stream
* @throws IOException I/O error happened
*/
public static InputStream getContentStream(String stringUrl) throws MalformedURLException, IOException {
URL url = new URL(stringUrl);

URLConnection urlConnection = url.openConnection();


InputStream stream;
if (urlConnection.getContentEncoding().equals("gzip")) {
stream = new GZIPInputStream(urlConnection.getInputStream());
} else {
stream = urlConnection.getInputStream();
}
return stream;
}

/**
* Get content for url/parameters
*
Expand Down

0 comments on commit e916573

Please sign in to comment.