Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/CommentedFileReader.java
41153 views
1
/*
2
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
package vm.share;
24
25
import java.io.*;
26
import java.util.LinkedList;
27
28
/**
29
* Utility class intended to read file line by line and skip comments.
30
*/
31
public class CommentedFileReader {
32
33
/**
34
* Type of comments that should be removed from file.
35
*/
36
public static enum CommentStyle {
37
/**
38
* Comments started with <i>#</i>.
39
*/
40
BASH,
41
/**
42
* Comments started with <i>//</i>.
43
*/
44
JAVA
45
}
46
47
/**
48
* Get lines from specified file and filter out comments.
49
* Only comments in BASH style will be filtered out.
50
*
51
* @param path to file that should be readed
52
* @return filtered lines from file
53
*/
54
public static String[] readFile(String path) throws IOException {
55
return readFile(new File(path), CommentStyle.BASH);
56
}
57
58
/**
59
* Get lines from specified file and filter out comments.
60
* Only comments in BASH style will be filtered out.
61
*
62
* @param file that should be readed
63
* @return filtered lines from file
64
*/
65
public static String[] readFile(File file) throws IOException {
66
return readFile(file, CommentStyle.BASH);
67
}
68
69
/**
70
* Get lines from specified file without comments.
71
*
72
* @param path to file that should be readed
73
* @param commentStyle describes what strings will be treated as comments
74
* @return filtered lines from file
75
*/
76
public static String[] readFile(String path, CommentStyle commentStyle) throws IOException {
77
return readFile(new File(path), commentStyle);
78
}
79
80
/**
81
* Get lines from specified file without comments.
82
*
83
* @param file that should be readed
84
* @param commentStyle describes what strings will be treated as comments
85
* @return filtered lines from file
86
*/
87
public static String[] readFile(File file, CommentStyle commentStyle) throws IOException {
88
LinkedList<String> entries = new LinkedList<String>();
89
BufferedReader reader = new BufferedReader(new FileReader(file));
90
String commentBeginning;
91
92
switch (commentStyle) {
93
case BASH:
94
commentBeginning = "#";
95
break;
96
case JAVA:
97
commentBeginning = "//";
98
break;
99
default:
100
throw new IllegalArgumentException("Unknown comment style");
101
}
102
103
while (true) {
104
String entry = reader.readLine();
105
if (entry == null) {
106
break;
107
}
108
109
entry = entry.replaceAll(commentBeginning + ".*", "").trim();
110
111
if (entry.length() > 0) {
112
entries.add(entry);
113
}
114
}
115
116
return entries.toArray(new String[entries.size()]);
117
}
118
119
}
120
121