Home | History | Annotate | Download | only in support
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package tests.support;
     19 
     20 import java.io.ByteArrayInputStream;
     21 import java.io.ByteArrayOutputStream;
     22 import java.io.File;
     23 import java.io.FileOutputStream;
     24 import java.io.IOException;
     25 import java.io.InputStream;
     26 import java.net.JarURLConnection;
     27 import java.net.MalformedURLException;
     28 import java.net.URL;
     29 import java.util.Hashtable;
     30 
     31 import tests.support.resource.Support_Resources;
     32 
     33 public class Support_GetLocal {
     34 
     35     static Hashtable<String, File> cache = new Hashtable<String, File>(20);
     36 
     37     public static File getLocalFile(String url) throws IOException,
     38             MalformedURLException {
     39         url = Support_Resources.RESOURCE_PACKAGE + url;
     40         File temp = cache.get(url);
     41         if (temp == null) {
     42             InputStream in = Support_GetLocal.class.getResourceAsStream(url);
     43             temp = File.createTempFile("hyts_local", ".tmp", null);
     44             temp.deleteOnExit();
     45             FileOutputStream out = new FileOutputStream(temp);
     46             int result;
     47             byte[] buf = new byte[4096];
     48             while ((result = in.read(buf)) != -1) {
     49                 out.write(buf, 0, result);
     50             }
     51             in.close();
     52             out.close();
     53             cache.put(url, temp);
     54         }
     55         return temp;
     56     }
     57 
     58     public static File getExternalLocalFile(String url) throws IOException,
     59             MalformedURLException {
     60         File temp = cache.get(url);
     61         if (temp == null) {
     62             InputStream in = new URL(url).openStream();
     63             temp = File.createTempFile("hyts_local", ".tmp", null);
     64             temp.deleteOnExit();
     65             FileOutputStream out = new FileOutputStream(temp);
     66             int result;
     67             byte[] buf = new byte[4096];
     68             while ((result = in.read(buf)) != -1) {
     69                 out.write(buf, 0, result);
     70             }
     71             in.close();
     72             out.close();
     73             cache.put(url, temp);
     74         }
     75         return temp;
     76     }
     77 
     78     static ByteArrayInputStream getStream(String url) throws IOException,
     79             MalformedURLException {
     80         InputStream in = new URL(url).openStream();
     81         ByteArrayOutputStream out = new ByteArrayOutputStream(256);
     82         int result;
     83         byte[] buf = new byte[256];
     84         while ((result = in.read(buf)) != -1) {
     85             out.write(buf, 0, result);
     86         }
     87         return new ByteArrayInputStream(out.toByteArray());
     88     }
     89 
     90     public static File createTempFile(String suffix) throws IOException {
     91         return File.createTempFile("hyts_", suffix, null);
     92     }
     93 
     94     public static JarURLConnection getJarURLConnection() throws IOException {
     95         JarURLConnection con1 = null;
     96         File file = getLocalFile("hyts_att.jar");
     97         URL fUrl1 = new URL("jar:file:" + file.getPath() + "!/");
     98         con1 = (JarURLConnection) fUrl1.openConnection();
     99         return con1;
    100     }
    101 }