public static InputStream cache(String url, String file) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    if (new File(file).exists()) {
      return new FileInputStream(file);
    }
    else {
      TrustManager[] trustManagers = {
        new X509TrustManager() {
          public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
          }
          
          public void checkClientTrusted(
            X509Certificate[] certs, String authType) {
          }
          
          public void checkServerTrusted(
            X509Certificate[] certs, String authType) {
          }
        }
      };
      SSLContext ctx = SSLContext.getInstance("TLS");
      ctx.init(null, trustManagers, null);
      SSLSocketFactory sslFactory = ctx.getSocketFactory();
      URLConnection urlConnection = new URL(url).openConnection();
      if (urlConnection instanceof javax.net.ssl.HttpsURLConnection) {
        HttpsURLConnection conn = (HttpsURLConnection) urlConnection;
        conn.setSSLSocketFactory(sslFactory);
        conn.setRequestMethod("GET");
      }
      InputStream inputStream = urlConnection.getInputStream();
      FileOutputStream cache = new FileOutputStream(file);
      return new TeeInputStream(inputStream, cache);
    }
  }
public static InputStream cache ( String url , String file ) throws IOException , NoSuchAlgorithmException , KeyStoreException , KeyManagementException { if ( new File ( file ) . exists ( ) ) { return new FileInputStream ( file ) ; } else { TrustManager [ ] trustManagers = { new X509TrustManager ( ) { public X509Certificate [ ] getAcceptedIssuers ( ) { return new X509Certificate [ 0 ] ; } public void checkClientTrusted ( X509Certificate [ ] certs , String authType ) { } public void checkServerTrusted ( X509Certificate [ ] certs , String authType ) { } } } ; SSLContext ctx = SSLContext . getInstance ( "TLS" ) ; ctx . init ( null , trustManagers , null ) ; SSLSocketFactory sslFactory = ctx . getSocketFactory ( ) ; URLConnection urlConnection = new URL ( url ) . openConnection ( ) ; if ( urlConnection instanceof javax . net . ssl . HttpsURLConnection ) { HttpsURLConnection conn = ( HttpsURLConnection ) urlConnection ; conn . setSSLSocketFactory ( sslFactory ) ; conn . setRequestMethod ( "GET" ) ; } InputStream inputStream = urlConnection . getInputStream ( ) ; FileOutputStream cache = new FileOutputStream ( file ) ; return new TeeInputStream ( inputStream , cache ) ; } }
Cache input stream.

@param url  the url
@param file the file
@return the input stream
@throws IOException              the io exception
@throws NoSuchAlgorithmException the no such algorithm exception
@throws KeyStoreException        the key store exception
@throws KeyManagementException   the key management exception