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.resource; 19 20 import java.io.File; 21 import java.io.FileInputStream; 22 import java.io.FileNotFoundException; 23 import java.io.FileOutputStream; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.io.OutputStream; 27 import java.net.MalformedURLException; 28 import java.net.URL; 29 import tests.support.Support_Configuration; 30 31 public class Support_Resources { 32 33 public static final String RESOURCE_PACKAGE = "/tests/resources/"; 34 35 public static final String RESOURCE_PACKAGE_NAME = "tests.resources"; 36 getStream(String name)37 public static InputStream getStream(String name) { 38 // If we have the resources packaged up in our jar file, get them that way. 39 String path = RESOURCE_PACKAGE + name; 40 InputStream result = Support_Resources.class.getResourceAsStream(path); 41 if (result != null) { 42 return result; 43 } 44 // Otherwise, if we're in an Android build tree, get the files directly. 45 String ANDROID_BUILD_TOP = System.getenv("ANDROID_BUILD_TOP"); 46 if (ANDROID_BUILD_TOP != null) { 47 File resource = new File(ANDROID_BUILD_TOP + "/libcore/support/src/test/java" + path); 48 if (resource.exists()) { 49 try { 50 return new FileInputStream(resource); 51 } catch (IOException ex) { 52 throw new IllegalArgumentException("Couldn't open: " + resource, ex); 53 } 54 } 55 } 56 throw new IllegalArgumentException("No such resource: " + path); 57 } 58 getURL(String name)59 public static String getURL(String name) { 60 String folder = null; 61 String fileName = name; 62 File resources = createTempFolder(); 63 int index = name.lastIndexOf("/"); 64 if (index != -1) { 65 folder = name.substring(0, index); 66 name = name.substring(index + 1); 67 } 68 copyFile(resources, folder, name); 69 URL url = null; 70 String resPath = resources.toString(); 71 if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') { 72 resPath = resPath.substring(1); 73 } 74 try { 75 url = new URL("file:/" + resPath + "/" + fileName); 76 } catch (MalformedURLException e) { 77 // TODO Auto-generated catch block 78 e.printStackTrace(); 79 } 80 return url.toString(); 81 } 82 createTempFolder()83 public static File createTempFolder() { 84 85 File folder = null; 86 try { 87 folder = File.createTempFile("hyts_resources", "", null); 88 folder.delete(); 89 folder.mkdirs(); 90 } catch (IOException e) { 91 // TODO Auto-generated catch block 92 e.printStackTrace(); 93 } 94 folder.deleteOnExit(); 95 return folder; 96 } 97 copyFile(File root, String folder, String file)98 public static File copyFile(File root, String folder, String file) { 99 File f; 100 if (folder != null) { 101 f = new File(root.toString() + "/" + folder); 102 if (!f.exists()) { 103 f.mkdirs(); 104 f.deleteOnExit(); 105 } 106 } else { 107 f = root; 108 } 109 110 File dest = new File(f.toString() + "/" + file); 111 112 InputStream in = Support_Resources.getStream(folder == null ? file 113 : folder + "/" + file); 114 try { 115 copyLocalFileto(dest, in); 116 } catch (FileNotFoundException e) { 117 // TODO Auto-generated catch block 118 e.printStackTrace(); 119 } catch (IOException e) { 120 // TODO Auto-generated catch block 121 e.printStackTrace(); 122 } 123 124 return dest; 125 } 126 createTempFile(String suffix)127 public static File createTempFile(String suffix) throws IOException { 128 return File.createTempFile("hyts_", suffix, null); 129 } 130 copyLocalFileto(File dest, InputStream in)131 public static void copyLocalFileto(File dest, InputStream in) throws IOException { 132 if (!dest.exists()) { 133 FileOutputStream out = new FileOutputStream(dest); 134 copy(in, out); 135 out.close(); 136 dest.deleteOnExit(); 137 } 138 in.close(); 139 } 140 copy(InputStream in, OutputStream out)141 private static int copy(InputStream in, OutputStream out) throws IOException { 142 int total = 0; 143 byte[] buffer = new byte[8192]; 144 int c; 145 while ((c = in.read(buffer)) != -1) { 146 total += c; 147 out.write(buffer, 0, c); 148 } 149 return total; 150 } 151 getExternalLocalFile(String url)152 public static File getExternalLocalFile(String url) throws IOException { 153 File resources = createTempFolder(); 154 InputStream in = new URL(url).openStream(); 155 File temp = new File(resources.toString() + "/local.tmp"); 156 copyLocalFileto(temp, in); 157 return temp; 158 } 159 getResourceURL(String resource)160 public static String getResourceURL(String resource) { 161 return "http://" + Support_Configuration.TestResources + resource; 162 } 163 164 /** 165 * Util method to load resource files 166 * 167 * @param name - name of resource file 168 * @return - resource input stream 169 */ getResourceStream(String name)170 public static InputStream getResourceStream(String name) { 171 InputStream is = Support_Resources.class.getResourceAsStream(name); 172 173 if (is == null) { 174 name = RESOURCE_PACKAGE + name; 175 is = Support_Resources.class.getResourceAsStream(name); 176 if (is == null) { 177 throw new RuntimeException("Failed to load resource: " + name); 178 } 179 } 180 181 return is; 182 } 183 resourceToTempFile(String path)184 public static File resourceToTempFile(String path) throws IOException { 185 File f = File.createTempFile("out", ".xml"); 186 f.deleteOnExit(); 187 FileOutputStream out = new FileOutputStream(f); 188 189 InputStream xml = Support_Resources.class.getResourceAsStream(path); 190 int b; 191 while ((b = xml.read()) != -1) { 192 out.write(b); 193 } 194 out.flush(); 195 out.close(); 196 xml.close(); 197 return f; 198 } 199 copyLocalFileTo(File dest, InputStream in)200 public static void copyLocalFileTo(File dest, InputStream in) throws IOException { 201 if (!dest.exists()) { 202 FileOutputStream out = new FileOutputStream(dest); 203 int result; 204 byte[] buf = new byte[4096]; 205 while ((result = in.read(buf)) != -1) { 206 out.write(buf, 0, result); 207 } 208 in.close(); 209 out.close(); 210 dest.deleteOnExit(); 211 } 212 } 213 } 214