1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tradefed.util; 18 19 import com.android.tradefed.log.LogUtil.CLog; 20 21 import com.google.common.base.Strings; 22 23 import java.io.File; 24 import java.io.FileInputStream; 25 import java.io.IOException; 26 import java.io.InputStream; 27 import java.util.HashMap; 28 import java.util.Map; 29 import java.util.Properties; 30 31 /** Utility for reading configuration resources. */ 32 public class ResourceUtil { 33 34 /** Extracts a given resource to an output file. */ extractResourceToFile(String resource, File output)35 public static void extractResourceToFile(String resource, File output) throws IOException { 36 try (InputStream resStream = ResourceUtil.class.getResourceAsStream(resource)) { 37 FileUtil.writeToFile(resStream, output); 38 } 39 } 40 41 /** DEPRECATED: Use extractResourceToFile Extracts a given resource to an output file. */ 42 @Deprecated 43 /** Extracts a given resource to an output file. Returns false if it fails. */ extractResourceAsFile(String resource, File output)44 public static boolean extractResourceAsFile(String resource, File output) { 45 try (InputStream resStream = ResourceUtil.class.getResourceAsStream(resource)) { 46 FileUtil.writeToFile(resStream, output); 47 return true; 48 } catch (IOException | RuntimeException e) { 49 CLog.e(e); 50 } 51 return false; 52 } 53 54 /** 55 * Extracts a given resource to an output file with fallback alternative path. Returns false if 56 * it fails. 57 */ extractResourceWithAltAsFile( String resource, String altResourcePath, File output)58 public static boolean extractResourceWithAltAsFile( 59 String resource, String altResourcePath, File output) { 60 if (extractResourceAsFile(resource, output)) { 61 return true; 62 } 63 return extractResourceAsFile(altResourcePath, output); 64 } 65 66 /** 67 * Read a property configuration from the resources. Configuration must follow a "key=value" or 68 * "key:value" format. Method is safe and will return an empty map in case of error. 69 * 70 * @param resource The path of the resource to read. 71 * @return a {@link Map} of the loaded resources. 72 * @see Properties 73 */ readConfigurationFromResource(String resource)74 public static Map<String, String> readConfigurationFromResource(String resource) { 75 try (InputStream resStream = ResourceUtil.class.getResourceAsStream(resource)) { 76 return readConfigurationFromStream(resStream); 77 } catch (IOException e) { 78 CLog.e(e); 79 } 80 return new HashMap<>(); 81 } 82 83 /** 84 * Read a property configuration from a file. Configuration must follow a "key=value" or 85 * "key:value" format. Method is safe and will return an empty map in case of error. 86 * 87 * @param propertyFile The path of the resource to read. 88 * @return a {@link Map} of the loaded resources. 89 * @see Properties 90 */ readConfigurationFromFile(File propertyFile)91 public static Map<String, String> readConfigurationFromFile(File propertyFile) { 92 try (InputStream resStream = new FileInputStream(propertyFile)) { 93 return readConfigurationFromStream(resStream); 94 } catch (IOException e) { 95 CLog.e(e); 96 } 97 return new HashMap<>(); 98 } 99 100 /** 101 * Read a property configuration from a stream. Configuration must follow a "key=value" or 102 * "key:value" format. Method is safe and will return an empty map in case of error. 103 * 104 * @param propertyStream The path of the resource to read. 105 * @return a {@link Map} of the loaded resources. 106 * @see Properties 107 */ readConfigurationFromStream(InputStream propertyStream)108 public static Map<String, String> readConfigurationFromStream(InputStream propertyStream) { 109 Map<String, String> resources = new HashMap<>(); 110 try { 111 if (propertyStream == null) { 112 CLog.w("No configuration stream"); 113 return resources; 114 } 115 Properties properties = new Properties(); 116 properties.load(propertyStream); 117 for (Object key : properties.keySet()) { 118 String keyString = (String) key; 119 String valueString = (String) properties.get(key); 120 if (Strings.isNullOrEmpty(valueString)) { 121 continue; 122 } 123 resources.put(keyString, valueString); 124 } 125 } catch (IOException e) { 126 CLog.e(e); 127 } 128 return resources; 129 } 130 } 131