Skip to content

Commit 86cb767

Browse files
author
Darragh Clarke
committed
8326568: jdk/test/com/sun/net/httpserver/bugs/B6431193.java should use try-with-resource and try-finally
Reviewed-by: dfuchs, jpai
1 parent b49ba42 commit 86cb767

File tree

1 file changed

+30
-41
lines changed

1 file changed

+30
-41
lines changed
Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2006, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -40,59 +40,48 @@
4040

4141
public class B6431193 {
4242

43-
static boolean error = false;
43+
static boolean handlerIsDaemon = true;
4444

45-
public static void read (InputStream i) throws IOException {
46-
while (i.read() != -1);
47-
i.close();
48-
}
49-
50-
/**
51-
* @param args
52-
*/
53-
public static void main(String[] args) {
45+
public static void main(String[] args) throws IOException {
5446
class MyHandler implements HttpHandler {
5547
public void handle(HttpExchange t) throws IOException {
56-
InputStream is = t.getRequestBody();
57-
read(is);
58-
// .. read the request body
48+
try (InputStream is = t.getRequestBody();
49+
OutputStream os = t.getResponseBody()) {
50+
is.readAllBytes();
51+
// .. read the request body
5952
String response = "This is the response";
60-
t.sendResponseHeaders(200, response.length());
61-
OutputStream os = t.getResponseBody();
62-
os.write(response.getBytes());
63-
os.close();
64-
error = Thread.currentThread().isDaemon();
53+
t.sendResponseHeaders(200, response.length());
54+
os.write(response.getBytes());
55+
} finally {
56+
handlerIsDaemon = Thread.currentThread().isDaemon();
57+
}
6558
}
6659
}
6760

61+
InetAddress loopback = InetAddress.getLoopbackAddress();
62+
HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
63+
server.createContext("/apps", new MyHandler());
64+
server.setExecutor(null);
65+
server.start();
6866

69-
HttpServer server;
7067
try {
71-
InetAddress loopback = InetAddress.getLoopbackAddress();
72-
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
73-
74-
server.createContext("/apps", new MyHandler());
75-
server.setExecutor(null);
76-
// creates a default executor
77-
server.start();
7868
int port = server.getAddress().getPort();
79-
String s = "http://localhost:"+port+"/apps/foo";
8069
URL url = URIBuilder.newBuilder()
81-
.scheme("http")
82-
.loopback()
83-
.port(port)
84-
.path("/apps/foo")
85-
.toURL();
86-
InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream();
87-
read (is);
88-
server.stop(0);
89-
if (error) {
90-
throw new RuntimeException ("error in test");
70+
.scheme("http")
71+
.loopback()
72+
.port(port)
73+
.path("/apps/foo")
74+
.toURL();
75+
try (InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream()) {
76+
is.readAllBytes();
9177
}
92-
93-
}
94-
catch (Exception e) {
78+
if (handlerIsDaemon) {
79+
throw new RuntimeException("request was handled by a daemon thread");
80+
}
81+
} catch (Exception e) {
9582
throw new AssertionError("Unexpected exception: " + e, e);
83+
} finally {
84+
server.stop(0);
9685
}
9786
}
9887
}

0 commit comments

Comments
 (0)