Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2f9edc6

Browse filesBrowse files
committed
support gzip for better performance
1 parent 6372337 commit 2f9edc6
Copy full SHA for 2f9edc6

File tree

Expand file treeCollapse file tree

1 file changed

+23
-6
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+23
-6
lines changed

‎src/main/java/org/kohsuke/github/GitHub.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHub.java
+23-6Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.Map;
4949
import java.util.Properties;
5050
import java.util.TimeZone;
51+
import java.util.zip.GZIPInputStream;
5152

5253
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
5354
import org.apache.commons.io.IOUtils;
@@ -199,20 +200,32 @@ private <T> T _retrieve(String tailApiUrl, Class<T> type, String method, boolean
199200
uc.setRequestProperty("Content-Length","0");
200201
uc.getOutputStream().close();
201202
}
203+
uc.setRequestProperty("Accept-Encoding", "gzip");
202204

205+
InputStreamReader r = null;
203206
try {
204-
InputStreamReader r = new InputStreamReader(uc.getInputStream(), "UTF-8");
207+
r = new InputStreamReader(wrapStream(uc, uc.getInputStream()), "UTF-8");
205208
if (type==null) {
206209
String data = IOUtils.toString(r);
207210
return null;
208211
}
209212
return MAPPER.readValue(r,type);
210213
} catch (IOException e) {
211214
handleApiError(e,uc);
215+
} finally {
216+
IOUtils.closeQuietly(r);
212217
}
213218
}
214219
}
215220

221+
private InputStream wrapStream(HttpURLConnection uc, InputStream in) throws IOException {
222+
String encoding = uc.getContentEncoding();
223+
if (encoding==null) return in;
224+
if (encoding.equals("gzip")) return new GZIPInputStream(in);
225+
226+
throw new UnsupportedOperationException("Unexpected Content-Encoding: "+encoding);
227+
}
228+
216229
/**
217230
* If the error is because of the API limit, wait 10 sec and return normally.
218231
* Otherwise throw an exception reporting an error.
@@ -231,11 +244,15 @@ private <T> T _retrieve(String tailApiUrl, Class<T> type, String method, boolean
231244
if (e instanceof FileNotFoundException)
232245
throw e; // pass through 404 Not Found to allow the caller to handle it intelligently
233246

234-
InputStream es = uc.getErrorStream();
235-
if (es!=null)
236-
throw (IOException)new IOException(IOUtils.toString(es,"UTF-8")).initCause(e);
237-
else
238-
throw e;
247+
InputStream es = wrapStream(uc, uc.getErrorStream());
248+
try {
249+
if (es!=null)
250+
throw (IOException)new IOException(IOUtils.toString(es,"UTF-8")).initCause(e);
251+
else
252+
throw e;
253+
} finally {
254+
IOUtils.closeQuietly(es);
255+
}
239256
}
240257

241258
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.