1 /* 2 * Copyright 2008 Google LLC 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 package com.google.auto.service.processor; 17 18 import static com.google.common.base.Charsets.UTF_8; 19 20 import com.google.common.io.Closer; 21 import java.io.BufferedReader; 22 import java.io.BufferedWriter; 23 import java.io.IOException; 24 import java.io.InputStream; 25 import java.io.InputStreamReader; 26 import java.io.OutputStream; 27 import java.io.OutputStreamWriter; 28 import java.util.Collection; 29 import java.util.HashSet; 30 import java.util.Set; 31 32 /** 33 * A helper class for reading and writing Services files. 34 */ 35 final class ServicesFiles { 36 public static final String SERVICES_PATH = "META-INF/services"; 37 ServicesFiles()38 private ServicesFiles() {} 39 40 /** 41 * Returns an absolute path to a service file given the class 42 * name of the service. 43 * 44 * @param serviceName not {@code null} 45 * @return SERVICES_PATH + serviceName 46 */ getPath(String serviceName)47 static String getPath(String serviceName) { 48 return SERVICES_PATH + "/" + serviceName; 49 } 50 51 /** 52 * Reads the set of service classes from a service file. 53 * 54 * @param input not {@code null}. Closed after use. 55 * @return a not {@code null Set} of service class names. 56 * @throws IOException 57 */ readServiceFile(InputStream input)58 static Set<String> readServiceFile(InputStream input) throws IOException { 59 HashSet<String> serviceClasses = new HashSet<String>(); 60 Closer closer = Closer.create(); 61 try { 62 // TODO(gak): use CharStreams 63 BufferedReader r = closer.register(new BufferedReader(new InputStreamReader(input, UTF_8))); 64 String line; 65 while ((line = r.readLine()) != null) { 66 int commentStart = line.indexOf('#'); 67 if (commentStart >= 0) { 68 line = line.substring(0, commentStart); 69 } 70 line = line.trim(); 71 if (!line.isEmpty()) { 72 serviceClasses.add(line); 73 } 74 } 75 return serviceClasses; 76 } catch (Throwable t) { 77 throw closer.rethrow(t); 78 } finally { 79 closer.close(); 80 } 81 } 82 83 /** 84 * Writes the set of service class names to a service file. 85 * 86 * @param output not {@code null}. Not closed after use. 87 * @param services a not {@code null Collection} of service class names. 88 * @throws IOException 89 */ writeServiceFile(Collection<String> services, OutputStream output)90 static void writeServiceFile(Collection<String> services, OutputStream output) 91 throws IOException { 92 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, UTF_8)); 93 for (String service : services) { 94 writer.write(service); 95 writer.newLine(); 96 } 97 writer.flush(); 98 } 99 } 100