public InputStream fetch ( URL url ) throws DownloadFailedException { if ( "file" . equalsIgnoreCase ( url . getProtocol ( ) ) ) { final File file ; try { file = new File ( url . toURI ( ) ) ; } catch ( URISyntaxException ex ) { final String msg = format ( "Download failed, unable to locate '%s'" , url . toString ( ) ) ; throw new DownloadFailedException ( msg ) ; } if ( file . exists ( ) ) { try { return new FileInputStream ( file ) ; } catch ( IOException ex ) { final String msg = format ( "Download failed, unable to rerieve '%s'" , url . toString ( ) ) ; throw new DownloadFailedException ( msg , ex ) ; } } else { final String msg = format ( "Download failed, file ('%s') does not exist" , url . toString ( ) ) ; throw new DownloadFailedException ( msg ) ; } } else { if ( connection != null ) { LOGGER . warn ( "HTTP URL Connection was not properly closed" ) ; connection . disconnect ( ) ; connection = null ; } connection = obtainConnection ( url ) ; final String encoding = connection . getContentEncoding ( ) ; try { if ( encoding != null && "gzip" . equalsIgnoreCase ( encoding ) ) { return new GZIPInputStream ( connection . getInputStream ( ) ) ; } else if ( encoding != null && "deflate" . equalsIgnoreCase ( encoding ) ) { return new InflaterInputStream ( connection . getInputStream ( ) ) ; } else { return connection . getInputStream ( ) ; } } catch ( IOException ex ) { checkForCommonExceptionTypes ( ex ) ; final String msg = format ( "Error retrieving '%s'%nConnection Timeout: %d%nEncoding: %s%n" , url . toString ( ) , connection . getConnectTimeout ( ) , encoding ) ; throw new DownloadFailedException ( msg , ex ) ; } catch ( Exception ex ) { final String msg = format ( "Unexpected exception retrieving '%s'%nConnection Timeout: %d%nEncoding: %s%n" , url . toString ( ) , connection . getConnectTimeout ( ) , encoding ) ; throw new DownloadFailedException ( msg , ex ) ; } } }
Retrieves the resource identified by the given URL and returns the InputStream .