private InputStreamAndLength downloadInputStream(DownloadConfig downloadConfig, Distribution distribution) throws IOException { URL url = new URL(getDownloadUrl(downloadConfig, distribution)); Optional<Proxy> proxy = downloadConfig.proxyFactory().map(ProxyFactory::createProxy); try { URLConnection openConnection; if (proxy.isPresent()) { openConnection = url.openConnection(proxy.get()); } else { openConnection = url.openConnection(); } openConnection.setRequestProperty("User-Agent",downloadConfig.getUserAgent()); TimeoutConfig timeoutConfig = downloadConfig.getTimeoutConfig(); openConnection.setConnectTimeout(timeoutConfig.getConnectionTimeout()); openConnection.setReadTimeout(downloadConfig.getTimeoutConfig().getReadTimeout()); InputStream downloadStream = openConnection.getInputStream(); return new InputStreamAndLength(downloadStream,openConnection.getContentLength()); } catch (IOException iox) { throw new IOException("Could not open inputStream for " + url + " with proxy " + proxy, iox); } }
long length = downloadStreamAndLength.contentLength(); InputStream downloadStream = downloadStreamAndLength.downloadStream();
long length = downloadStreamAndLength.contentLength(); InputStream downloadStream = downloadStreamAndLength.downloadStream();
private InputStreamAndLength downloadInputStream(IDownloadConfig downloadConfig, Distribution distribution) throws MalformedURLException, IOException { URL url = new URL(getDownloadUrl(downloadConfig, distribution)); Proxy proxy = downloadConfig.proxyFactory().createProxy(); try { URLConnection openConnection; if (proxy!=null) { openConnection = url.openConnection(proxy); } else { openConnection = url.openConnection(); } openConnection.setRequestProperty("User-Agent",downloadConfig.getUserAgent()); ITimeoutConfig timeoutConfig = downloadConfig.getTimeoutConfig(); openConnection.setConnectTimeout(timeoutConfig.getConnectionTimeout()); openConnection.setReadTimeout(downloadConfig.getTimeoutConfig().getReadTimeout()); InputStream downloadStream = openConnection.getInputStream(); return new InputStreamAndLength(downloadStream,openConnection.getContentLength()); } catch (IOException iox) { throw new IOException("Could not open inputStream for "+url+(proxy!=null ? " with proxy "+proxy : ""), iox); } }