Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Properties/EscapeSpace.java
41149 views
1
/*
2
* Copyright (c) 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
24
/*
25
* @test
26
* @bug 4219644
27
* @summary Escaping of spaces required only for leading spaces in the value
28
* part of the property.
29
*/
30
31
import java.io.BufferedInputStream;
32
import java.io.BufferedOutputStream;
33
import java.io.ByteArrayInputStream;
34
import java.io.FileInputStream;
35
import java.io.FileOutputStream;
36
import java.io.IOException;
37
import java.util.Properties;
38
39
public class EscapeSpace {
40
41
static String props =
42
"key1=\\ \\ Value1, has leading and trailing spaces\\ \n" +
43
"key2=Value2,\\ does not have\\ leading or trailing\\ spaces\n" +
44
"key3=Value3,has,no,spaces\n" +
45
"key4=Value4, does not have leading spaces\\ \n" +
46
"key5=\\t\\ \\ Value5, has leading tab and no trailing spaces\n" +
47
"key6=\\ \\ Value6,doesnothaveembeddedspaces\\ \\ \n" +
48
"\\ key1\\ test\\ =key1, has leading and trailing spaces \n" +
49
"key2\\ test=key2, does not have leading or trailing spaces\n" +
50
"key3test=key3,has,no,spaces\n" +
51
"key4\\ test\\ =key4, does not have leading spaces \n" +
52
"\\t\\ key5\\ test=key5, has leading tab and no trailing spaces\n" +
53
"\\ \\ key6\\ \\ =\\ key6,doesnothaveembeddedspaces ";
54
static void load(Properties p, String file) throws Exception
55
{
56
FileInputStream fis = null;
57
BufferedInputStream bis = null;
58
59
try {
60
fis = new FileInputStream(file);
61
bis = new BufferedInputStream( fis );
62
63
p.load(bis);
64
}
65
catch (IOException e) {
66
throw new RuntimeException(e.getMessage());
67
} finally {
68
if (fis != null)
69
fis.close();
70
}
71
}
72
73
static void store(Properties p, String file) throws Exception
74
{
75
76
FileOutputStream fos = null;
77
BufferedOutputStream bos = null;
78
79
try {
80
fos = new FileOutputStream(file);
81
bos = new BufferedOutputStream( fos );
82
83
p.store( bos, "Omitting escape characters for non leading space \" \" in properties");
84
}
85
catch (IOException e) {
86
throw new RuntimeException(e.getMessage());
87
} finally {
88
if (fos != null)
89
fos.close();
90
}
91
}
92
93
public static void main( String args[] ) throws Exception
94
{
95
ByteArrayInputStream bais = new ByteArrayInputStream(props.getBytes());
96
Properties props0 = new Properties();
97
// Load properties with escape character '\' before space characters
98
try {
99
props0.load(bais);
100
} catch (IOException e) {
101
throw new RuntimeException(e.getMessage());
102
}
103
104
Properties props1 = new Properties();
105
/*
106
* Put the same properties, but without the escape char for space in
107
* value part.
108
*/
109
props1.put( "key1", " Value1, has leading and trailing spaces " );
110
props1.put( "key2",
111
"Value2, does not have leading or trailing spaces" );
112
props1.put( "key3", "Value3,has,no,spaces" );
113
props1.put( "key4", "Value4, does not have leading spaces " );
114
props1.put( "key5",
115
"\t Value5, has leading tab and no trailing spaces" );
116
props1.put( "key6", " Value6,doesnothaveembeddedspaces " );
117
props1.put( " key1 test ", "key1, has leading and trailing spaces " );
118
props1.put( "key2 test",
119
"key2, does not have leading or trailing spaces" );
120
props1.put( "key3test", "key3,has,no,spaces" );
121
props1.put( "key4 test ", "key4, does not have leading spaces " );
122
props1.put( "\t key5 test",
123
"key5, has leading tab and no trailing spaces" );
124
props1.put( " key6 ", " key6,doesnothaveembeddedspaces " );
125
126
// Check if both the properties match
127
if (!props0.equals(props1))
128
throw new RuntimeException("Test failed");
129
130
131
// Also store the new properties to a file
132
store(props1, "out1.props");
133
134
Properties props2 = new Properties();
135
// Reread the properties from the file
136
load(props2, "out1.props");
137
138
// Make sure that the properties match
139
if (!props1.equals(props2))
140
throw new RuntimeException("Test failed");
141
}
142
}
143
144