// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.pgm.util;

import com.google.common.collect.Sets;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Set;

public final class IoUtil {
  public static void copyWithThread(final InputStream src,
      final OutputStream dst) {
    new Thread("IoUtil-Copy") {
      @Override
      public void run() {
        try {
          final byte[] buf = new byte[256];
          int n;
          while (0 < (n = src.read(buf))) {
            dst.write(buf, 0, n);
          }
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            src.close();
          } catch (IOException e2) {
          }
        }
      }
    }.start();
  }

  public static void loadJARs(File... jars) {
    ClassLoader cl = IoUtil.class.getClassLoader();
    if (!(cl instanceof URLClassLoader)) {
      throw noAddURL("Not loaded by URLClassLoader", null);
    }

    @SuppressWarnings("resource")
    URLClassLoader urlClassLoader = (URLClassLoader) cl;

    Method addURL;
    try {
      addURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
      addURL.setAccessible(true);
    } catch (SecurityException e) {
      throw noAddURL("Method addURL not available", e);
    } catch (NoSuchMethodException e) {
      throw noAddURL("Method addURL not available", e);
    }

    Set<URL> have = Sets.newHashSet(Arrays.asList(urlClassLoader.getURLs()));
    for (File path : jars) {
      try {
        URL url = path.toURI().toURL();
        if (have.add(url)) {
          addURL.invoke(cl, url);
        }
      } catch (MalformedURLException e) {
        throw noAddURL("addURL " + path + " failed", e);
      } catch (IllegalArgumentException e) {
        throw noAddURL("addURL " + path + " failed", e);
      } catch (IllegalAccessException e) {
        throw noAddURL("addURL " + path + " failed", e);
      } catch (InvocationTargetException e) {
        throw noAddURL("addURL " + path + " failed", e.getCause());
      }
    }
  }

  private static UnsupportedOperationException noAddURL(String m, Throwable why) {
    String prefix = "Cannot extend classpath: ";
    return new UnsupportedOperationException(prefix + m, why);
  }
  private IoUtil() {
  }
}

@@ -1797,42 +1797,13 @@
     
-@SuppressWarnings(%22resource%22)%0A    
+try (
 URLC
@@ -1849,19 +1849,22 @@
 ader) cl
-;%0A%0A
+) %7B%0A  
     Meth
@@ -1874,30 +1874,34 @@
 addURL;%0A    
+  
 try %7B%0A
+  
       addURL
@@ -1964,16 +1964,18 @@
 class);%0A
+  
       ad
@@ -1996,32 +1996,34 @@
 ible(true);%0A    
+  
 %7D catch (Securit
@@ -2037,82 +2037,10 @@
 ion 
-e) %7B%0A      throw noAddURL(%22Method addURL not available%22, e);%0A    %7D catch (
+%7C 
 NoSu
@@ -2060,32 +2060,34 @@
 tion e) %7B%0A      
+ 
+ 
 throw noAddURL(%22
@@ -2120,27 +2120,31 @@
 e%22, e);%0A    
+  
 %7D%0A%0A
+  
     Set%3CURL%3E
@@ -2213,16 +2213,18 @@
 ));%0A    
+ 
+ 
 for (Fil
@@ -2236,24 +2236,26 @@
 h : jars) %7B%0A
+  
       try %7B%0A
@@ -2262,16 +2262,18 @@
         
+  
 URL url 
@@ -2300,24 +2300,26 @@
 ();%0A        
+ 
+ 
 if (have.add
@@ -2323,24 +2323,26 @@
 add(url)) %7B%0A
+  
           ad
@@ -2359,24 +2359,26 @@
 e(cl, url);%0A
+  
         %7D%0A  
@@ -2377,24 +2377,26 @@
     %7D%0A      
+  
 %7D catch (Mal
@@ -2418,93 +2418,57 @@
 ion 
-e) %7B%0A        throw noAddURL(%22addURL %22 + path + %22 failed%22, e);%0A      %7D catch (
+%7C IllegalArgumentException %7C%0A            
 IllegalA
 rgum
@@ -2455,39 +2455,37 @@
         IllegalA
-rgument
+ccess
 Exception e) %7B%0A 
@@ -2475,32 +2475,34 @@
 sException e) %7B%0A
+  
         throw no
@@ -2540,32 +2540,34 @@
 led%22, e);%0A      
+  
 %7D catch (Illegal
@@ -2560,28 +2560,31 @@
 catch (I
-llegalAccess
+nvocationTarget
 Exceptio
@@ -2582,32 +2582,34 @@
 tException e) %7B%0A
+  
         throw no
@@ -2650,42 +2650,55 @@
 %22, e
-);%0A      %7D catch (InvocationTarget
+.getCause());%0A        %7D%0A      %7D%0A    %7D catch (IO
 Exce
@@ -2695,39 +2695,39 @@
 ch (IOException 
+io
 e) %7B%0A
-  
       throw noAd
@@ -2736,59 +2736,44 @@
 RL(%22
-addURL %22 + path + %22 failed%22, e.getCause());%0A      %7D
+Error closing URLClassLoader%22, ioe);
 %0A   

Use Java 7 style exception handling in IoUtil