22
33import org .apache .commons .io .FileUtils ;
44import java .io .IOException ;
5+ import java .io .InputStream ;
56import java .io .BufferedReader ;
67import java .io .InputStreamReader ;
78import java .io .File ;
89import java .net .URL ;
10+ import java .net .URLConnection ;
911import java .util .regex .Pattern ;
12+ import java .util .zip .GZIPInputStream ;
13+ import java .util .zip .ZipException ;
1014
1115class LocalBinary {
1216
@@ -58,7 +62,8 @@ private void initialize() throws LocalException {
5862 throw new LocalException ("Failed to detect OS type" );
5963 }
6064
61- httpPath = BIN_URL + binFileName ;
65+ String sourceURL = BIN_URL ;
66+ httpPath = sourceURL + binFileName ;
6267 }
6368
6469 private boolean isAlpine () {
@@ -174,7 +179,7 @@ private void downloadBinary(String destParentDir, Boolean custom) throws LocalEx
174179 URL url = new URL (httpPath );
175180
176181 File f = new File (source );
177- FileUtils . copyURLToFile (url , f );
182+ newCopyToFile (url , f );
178183
179184 changePermissions (binaryPath );
180185 } catch (Exception e ) {
@@ -192,4 +197,26 @@ private void changePermissions(String path) {
192197 public String getBinaryPath () {
193198 return binaryPath ;
194199 }
200+
201+ private static void newCopyToFile (URL url , File f ) throws IOException {
202+ URLConnection conn = url .openConnection ();
203+ conn .setRequestProperty ("User-Agent" , "browserstack-local-java/" + Local .getPackageVersion ());
204+ conn .setRequestProperty ("Accept-Encoding" , "gzip, *" );
205+ String contentEncoding = conn .getContentEncoding ();
206+
207+ if (contentEncoding == null || !contentEncoding .toLowerCase ().contains ("gzip" )) {
208+ FileUtils .copyToFile (conn .getInputStream (), f );
209+ return ;
210+ }
211+
212+ try (InputStream stream = new GZIPInputStream (conn .getInputStream ())) {
213+ if (System .getenv ().containsKey ("BROWSERSTACK_LOCAL_DEBUG_GZIP" )) {
214+ System .out .println ("using gzip in " + conn .getRequestProperty ("User-Agent" ));
215+ }
216+
217+ FileUtils .copyToFile (stream , f );
218+ } catch (ZipException e ) {
219+ FileUtils .copyURLToFile (url , f );
220+ }
221+ }
195222}
0 commit comments