Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/net/NetProperties.java
41152 views
1
/*
2
* Copyright (c) 2004, 2021, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package sun.net;
26
27
import jdk.internal.util.StaticProperty;
28
29
import java.io.*;
30
import java.security.AccessController;
31
import java.security.PrivilegedAction;
32
import java.util.Properties;
33
34
/*
35
* This class allows for centralized access to Networking properties.
36
* Default values are loaded from the file jre/lib/net.properties
37
*
38
*
39
* @author Jean-Christophe Collet
40
*
41
*/
42
43
@SuppressWarnings("removal")
44
public class NetProperties {
45
private static Properties props = new Properties();
46
static {
47
AccessController.doPrivileged(
48
new PrivilegedAction<Void>() {
49
public Void run() {
50
loadDefaultProperties();
51
return null;
52
}});
53
}
54
55
private NetProperties() { };
56
57
58
/*
59
* Loads the default networking system properties
60
* the file is in jre/lib/net.properties
61
*/
62
private static void loadDefaultProperties() {
63
String fname = StaticProperty.javaHome();
64
if (fname == null) {
65
throw new Error("Can't find java.home ??");
66
}
67
try {
68
File f = new File(fname, "conf");
69
f = new File(f, "net.properties");
70
fname = f.getCanonicalPath();
71
InputStream in = new FileInputStream(fname);
72
BufferedInputStream bin = new BufferedInputStream(in);
73
props.load(bin);
74
bin.close();
75
} catch (Exception e) {
76
// Do nothing. We couldn't find or access the file
77
// so we won't have default properties...
78
}
79
}
80
81
/**
82
* Get a networking system property. If no system property was defined
83
* returns the default value, if it exists, otherwise returns
84
* <code>null</code>.
85
* @param key the property name.
86
* @throws SecurityException if a security manager exists and its
87
* <code>checkPropertiesAccess</code> method doesn't allow access
88
* to the system properties.
89
* @return the <code>String</code> value for the property,
90
* or <code>null</code>
91
*/
92
public static String get(String key) {
93
String def = props.getProperty(key);
94
try {
95
return System.getProperty(key, def);
96
} catch (IllegalArgumentException e) {
97
} catch (NullPointerException e) {
98
}
99
return null;
100
}
101
102
/**
103
* Get an Integer networking system property. If no system property was
104
* defined returns the default value, if it exists, otherwise returns
105
* <code>null</code>.
106
* @param key the property name.
107
* @param defval the default value to use if the property is not found
108
* @throws SecurityException if a security manager exists and its
109
* <code>checkPropertiesAccess</code> method doesn't allow access
110
* to the system properties.
111
* @return the <code>Integer</code> value for the property,
112
* or <code>null</code>
113
*/
114
public static Integer getInteger(String key, int defval) {
115
String val = null;
116
117
try {
118
val = System.getProperty(key, props.getProperty(key));
119
} catch (IllegalArgumentException e) {
120
} catch (NullPointerException e) {
121
}
122
123
if (val != null) {
124
try {
125
return Integer.decode(val);
126
} catch (NumberFormatException ex) {
127
}
128
}
129
return defval;
130
}
131
132
/**
133
* Get a Boolean networking system property. If no system property was
134
* defined returns the default value, if it exists, otherwise returns
135
* <code>null</code>.
136
* @param key the property name.
137
* @throws SecurityException if a security manager exists and its
138
* <code>checkPropertiesAccess</code> method doesn't allow access
139
* to the system properties.
140
* @return the <code>Boolean</code> value for the property,
141
* or <code>null</code>
142
*/
143
public static Boolean getBoolean(String key) {
144
String val = null;
145
146
try {
147
val = System.getProperty(key, props.getProperty(key));
148
} catch (IllegalArgumentException e) {
149
} catch (NullPointerException e) {
150
}
151
152
if (val != null) {
153
try {
154
return Boolean.valueOf(val);
155
} catch (NumberFormatException ex) {
156
}
157
}
158
return null;
159
}
160
161
}
162
163