• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 package com.android.tools.r8.naming;
5 
6 import com.google.common.collect.ImmutableList;
7 import com.google.common.collect.ImmutableList.Builder;
8 import java.io.BufferedReader;
9 import java.io.IOException;
10 import java.nio.file.Files;
11 import java.nio.file.Path;
12 import java.util.List;
13 
14 public class DictionaryReader implements AutoCloseable {
15 
16   private BufferedReader reader;
17 
DictionaryReader(Path path)18   public DictionaryReader(Path path) throws IOException {
19     this.reader = Files.newBufferedReader(path);
20   }
21 
readName()22   public String readName() throws IOException {
23     assert reader != null;
24 
25     StringBuilder name = new StringBuilder();
26     int readCharAsInt;
27 
28     while ((readCharAsInt = reader.read()) != -1) {
29       char readChar = (char) readCharAsInt;
30 
31       if ((name.length() != 0 && Character.isJavaIdentifierPart(readChar))
32           || (name.length() == 0 && Character.isJavaIdentifierStart(readChar))) {
33         name.append(readChar);
34       } else {
35         if (readChar == '#') {
36           reader.readLine();
37         }
38 
39         if (name.length() != 0) {
40           return name.toString();
41         }
42       }
43     }
44 
45     return name.toString();
46   }
47 
48   @Override
close()49   public void close() throws IOException {
50     if (reader != null) {
51       reader.close();
52     }
53   }
54 
readAllNames(Path path)55   public static List<String> readAllNames(Path path) {
56     if (path != null) {
57       Builder<String> namesBuilder = new ImmutableList.Builder<String>();
58       try (DictionaryReader reader = new DictionaryReader(path);) {
59         String name = reader.readName();
60         while (!name.isEmpty()) {
61           namesBuilder.add(name);
62           name = reader.readName();
63         }
64       } catch (IOException e) {
65         System.err.println("Unable to create dictionary from file " + path.toString());
66       }
67       return namesBuilder.build();
68     } else {
69       return ImmutableList.of();
70     }
71   }
72 }
73