Skip to content

Commit 4388959

Browse files
committed
8268056: Update java.net and java.nio to use switch expressions
Reviewed-by: dfuchs, michaelm, chegar, iris, alanb
1 parent 9cfd560 commit 4388959

File tree

2 files changed

+30
-41
lines changed

2 files changed

+30
-41
lines changed

src/java.base/share/classes/java/net/SocksSocketImpl.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,17 @@ private void connectV4(InputStream in, OutputStream out,
211211
throw new SocketException("Reply from SOCKS server has bad length: " + n);
212212
if (data[0] != 0 && data[0] != 4)
213213
throw new SocketException("Reply from SOCKS server has bad version");
214-
SocketException ex = null;
215-
switch (data[1]) {
216-
case 90:
217-
// Success!
218-
external_address = endpoint;
219-
break;
220-
case 91:
221-
ex = new SocketException("SOCKS request rejected");
222-
break;
223-
case 92:
224-
ex = new SocketException("SOCKS server couldn't reach destination");
225-
break;
226-
case 93:
227-
ex = new SocketException("SOCKS authentication failed");
228-
break;
229-
default:
230-
ex = new SocketException("Reply from SOCKS server contains bad status");
231-
break;
232-
}
214+
SocketException ex = switch (data[1]) {
215+
case 90 -> {
216+
// Success!
217+
external_address = endpoint;
218+
yield null;
219+
}
220+
case 91 -> new SocketException("SOCKS request rejected");
221+
case 92 -> new SocketException("SOCKS server couldn't reach destination");
222+
case 93 -> new SocketException("SOCKS authentication failed");
223+
default -> new SocketException("Reply from SOCKS server contains bad status");
224+
};
233225
if (ex != null) {
234226
in.close();
235227
out.close();

src/java.base/share/classes/java/nio/file/Files.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,40 +2803,37 @@ public static Path walkFileTree(Path start,
28032803
try (FileTreeWalker walker = new FileTreeWalker(options, maxDepth)) {
28042804
FileTreeWalker.Event ev = walker.walk(start);
28052805
do {
2806-
FileVisitResult result;
2807-
switch (ev.type()) {
2808-
case ENTRY :
2806+
FileVisitResult result = switch (ev.type()) {
2807+
case ENTRY -> {
28092808
IOException ioe = ev.ioeException();
28102809
if (ioe == null) {
28112810
assert ev.attributes() != null;
2812-
result = visitor.visitFile(ev.file(), ev.attributes());
2811+
yield visitor.visitFile(ev.file(), ev.attributes());
28132812
} else {
2814-
result = visitor.visitFileFailed(ev.file(), ioe);
2813+
yield visitor.visitFileFailed(ev.file(), ioe);
28152814
}
2816-
break;
2817-
2818-
case START_DIRECTORY :
2819-
result = visitor.preVisitDirectory(ev.file(), ev.attributes());
2815+
}
2816+
case START_DIRECTORY -> {
2817+
var res = visitor.preVisitDirectory(ev.file(), ev.attributes());
28202818

28212819
// if SKIP_SIBLINGS and SKIP_SUBTREE is returned then
28222820
// there shouldn't be any more events for the current
28232821
// directory.
2824-
if (result == FileVisitResult.SKIP_SUBTREE ||
2825-
result == FileVisitResult.SKIP_SIBLINGS)
2822+
if (res == FileVisitResult.SKIP_SUBTREE ||
2823+
res == FileVisitResult.SKIP_SIBLINGS)
28262824
walker.pop();
2827-
break;
2828-
2829-
case END_DIRECTORY :
2830-
result = visitor.postVisitDirectory(ev.file(), ev.ioeException());
2825+
yield res;
2826+
}
2827+
case END_DIRECTORY -> {
2828+
var res = visitor.postVisitDirectory(ev.file(), ev.ioeException());
28312829

28322830
// SKIP_SIBLINGS is a no-op for postVisitDirectory
2833-
if (result == FileVisitResult.SKIP_SIBLINGS)
2834-
result = FileVisitResult.CONTINUE;
2835-
break;
2836-
2837-
default :
2838-
throw new AssertionError("Should not get here");
2839-
}
2831+
if (res == FileVisitResult.SKIP_SIBLINGS)
2832+
res = FileVisitResult.CONTINUE;
2833+
yield res;
2834+
}
2835+
default -> throw new AssertionError("Should not get here");
2836+
};
28402837

28412838
if (Objects.requireNonNull(result) != FileVisitResult.CONTINUE) {
28422839
if (result == FileVisitResult.TERMINATE) {

0 commit comments

Comments
 (0)