Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/file/GetHeaderFields.java
41159 views
1
/*
2
* Copyright (c) 2020, 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 8241389
27
* @summary URLConnection::getHeaderFields returns result inconsistent with getHeaderField/Key for FileURLConnection, FtpURLConnection
28
*/
29
30
import java.net.*;
31
import java.io.*;
32
import java.util.*;
33
34
public class GetHeaderFields {
35
36
public static void main(String[] args) {
37
HashMap<String,List<String>> map1 = new HashMap<>();
38
Map<String,List<String>> headers = null;
39
40
try {
41
File f = File.createTempFile("test", null);
42
f.deleteOnExit();
43
String s = f.getAbsolutePath();
44
s = s.startsWith("/") ? s : "/" + s;
45
URL url = new URL("file://localhost"+s);
46
URLConnection urlc = url.openConnection();
47
InputStream in = urlc.getInputStream();
48
headers = urlc.getHeaderFields();
49
Set<String> keys = headers.keySet();
50
51
int i = 0;
52
while (true) {
53
String key = urlc.getHeaderFieldKey(i);
54
if (key == null)
55
break;
56
String val = urlc.getHeaderField(i);
57
putValue(map1,key,val);
58
i++;
59
}
60
System.out.println("headers = " + headers.toString());
61
System.out.println("map1 = " + map1.toString());
62
in.close();
63
} catch (java.io.FileNotFoundException fnfe) {
64
throw new RuntimeException("failed to recognize localhost");
65
} catch (Exception ex) {
66
throw new RuntimeException("Unexpected exception: " + ex);
67
}
68
if (!map1.equals(headers)) {
69
throw new RuntimeException("headers != map1");
70
}
71
72
// check connection to non-existent resource returns
73
// an empty map
74
75
URLConnection urlc = null;
76
77
try {
78
URL url = new URL("file:///dev/non/existing/ABCD/123/foobar/sqisjjlk");
79
urlc = url.openConnection();
80
urlc.getInputStream();
81
} catch (IOException e) {}
82
83
headers = urlc.getHeaderFields();
84
if (headers == null) {
85
throw new RuntimeException("expected empty map");
86
}
87
if (!headers.isEmpty()) {
88
throw new RuntimeException("expected empty map");
89
}
90
91
}
92
93
static void putValue(HashMap<String,List<String>> map, String key, String val) {
94
List<String> l = map.get(key);
95
if (l == null) {
96
l = new LinkedList<String>();
97
map.put(key, l);
98
}
99
l.add(val);
100
}
101
}
102
103