• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 java.nio.charset.StandardCharsets.UTF_8;
19 
20 import java.io.BufferedReader;
21 import java.io.BufferedWriter;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.OutputStream;
26 import java.io.OutputStreamWriter;
27 import java.util.Collection;
28 import java.util.HashSet;
29 import java.util.Set;
30 
31 /**
32  * A helper class for reading and writing Services files.
33  */
34 final class ServicesFiles {
35   public static final String SERVICES_PATH = "META-INF/services";
36 
ServicesFiles()37   private ServicesFiles() {}
38 
39   /**
40    * Returns an absolute path to a service file given the class
41    * name of the service.
42    *
43    * @param serviceName not {@code null}
44    * @return SERVICES_PATH + serviceName
45    */
getPath(String serviceName)46   static String getPath(String serviceName) {
47     return SERVICES_PATH + "/" + serviceName;
48   }
49 
50   /**
51    * Reads the set of service classes from a service file.
52    *
53    * @param input not {@code null}. Closed after use.
54    * @return a not {@code null Set} of service class names.
55    * @throws IOException
56    */
readServiceFile(InputStream input)57   static Set<String> readServiceFile(InputStream input) throws IOException {
58     HashSet<String> serviceClasses = new HashSet<String>();
59     try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, UTF_8))) {
60       String line;
61       while ((line = reader.readLine()) != null) {
62         int commentStart = line.indexOf('#');
63         if (commentStart >= 0) {
64           line = line.substring(0, commentStart);
65         }
66         line = line.trim();
67         if (!line.isEmpty()) {
68           serviceClasses.add(line);
69         }
70       }
71       return serviceClasses;
72     }
73   }
74 
75   /**
76    * Writes the set of service class names to a service file.
77    *
78    * @param output not {@code null}. Not closed after use.
79    * @param services a not {@code null Collection} of service class names.
80    * @throws IOException
81    */
writeServiceFile(Collection<String> services, OutputStream output)82   static void writeServiceFile(Collection<String> services, OutputStream output)
83       throws IOException {
84     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, UTF_8));
85     for (String service : services) {
86       writer.write(service);
87       writer.newLine();
88     }
89     writer.flush();
90   }
91 }
92