Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion util/src/main/java/eu/darkbot/util/http/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PushbackInputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
Expand All @@ -22,6 +23,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.zip.GZIPInputStream;

/**
* Utility for HTTP connections.
Expand Down Expand Up @@ -291,7 +293,22 @@ public void closeInputStream() throws IOException {
* @throws IOException of {@link Http#getConnection()}
*/
public InputStream getInputStream() throws IOException {
return getConnection().getInputStream();
PushbackInputStream in = new PushbackInputStream(getConnection().getInputStream(), 2);

byte[] header = new byte[2];
int count = 0;
while (count < header.length) {
int read = in.read(header, count, header.length - count);
if (read == -1) break;
count += read;
}
if (count > 0) in.unread(header, 0, count);

// Response may be gzip-compressed even without a Content-Encoding header
if (count == 2 && (header[0] & 0xff) == 0x1f && (header[1] & 0xff) == 0x8b) {
return new GZIPInputStream(in);
}
return in;
}

/**
Expand Down
Loading