Skip to content
This repository was archived by the owner on Nov 25, 2022. It is now read-only.

Commit 22c96a2

Browse files
committed
fix sonar issue
1 parent 173bbc3 commit 22c96a2

2 files changed

Lines changed: 42 additions & 36 deletions

File tree

src/main/java/xdean/css/editor/feature/LastFileFeature.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static xdean.jex.util.lang.ExceptionUtil.uncheck;
55

66
import java.io.IOException;
7+
import java.nio.file.DirectoryStream;
78
import java.nio.file.Files;
89
import java.nio.file.Path;
910
import java.nio.file.Paths;
@@ -45,24 +46,26 @@ public void bind(Stage stage) {
4546
public void open() {
4647
try {
4748
FileUtil.createDirectory(LAST_FILE_PATH);
48-
Files.newDirectoryStream(LAST_FILE_PATH, "*.tmp").forEach(p -> uncheck(() -> {
49-
List<String> lines = Files.readAllLines(p, options.charset().getValue());
50-
if (lines.isEmpty()) {
51-
return;
52-
}
53-
String head = lines.get(0);
54-
CssEditor editor = editorFactory.get();
55-
editor.fileProperty().set(Try.to(() -> Integer.valueOf(head)).map(i -> FileWrapper.newFile(i))
56-
.getOrElse(() -> FileWrapper.existFile(Paths.get(head))));
57-
lines.stream()
58-
.skip(1)
59-
.reduce((a, b) -> String.join(System.lineSeparator(), a, b))
60-
.ifPresent(t -> {
61-
editor.replaceText(t);
62-
editor.getUndoManager().forgetHistory();
63-
});
64-
contextService.editorList().add(editor);
65-
}));
49+
try (DirectoryStream<Path> stream = Files.newDirectoryStream(LAST_FILE_PATH, "*.tmp")) {
50+
stream.forEach(p -> uncheck(() -> {
51+
List<String> lines = Files.readAllLines(p, options.charset().getValue());
52+
if (lines.isEmpty()) {
53+
return;
54+
}
55+
String head = lines.get(0);
56+
CssEditor editor = editorFactory.get();
57+
editor.fileProperty().set(Try.to(() -> Integer.valueOf(head)).map(i -> FileWrapper.newFile(i))
58+
.getOrElse(() -> FileWrapper.existFile(Paths.get(head))));
59+
lines.stream()
60+
.skip(1)
61+
.reduce((a, b) -> String.join(System.lineSeparator(), a, b))
62+
.ifPresent(t -> {
63+
editor.replaceText(t);
64+
editor.getUndoManager().forgetHistory();
65+
});
66+
contextService.editorList().add(editor);
67+
}));
68+
}
6669
} catch (IOException e) {
6770
dialogService.errorDialog(e).content("Fail to open last files.").show();
6871
}

src/main/java/xdean/css/editor/service/SkinService.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package xdean.css.editor.service;
22

3+
import java.nio.file.DirectoryStream;
34
import java.nio.file.Files;
45
import java.nio.file.Path;
56

@@ -49,25 +50,27 @@ public void init() throws Exception {
4950
if (Files.notExists(skinFolder)) {
5051
Files.createDirectory(skinFolder);
5152
} else {
52-
Files.newDirectoryStream(skinFolder).forEach(path -> {
53-
String fileName = path.getFileName().toString();
54-
if (!Files.isDirectory(path) && (fileName.endsWith(".css") || fileName.endsWith(".bss"))) {
55-
String url = path.toUri().toString();
56-
String name = StringUtil.upperFirst(fileName.substring(0, fileName.length() - 4));
57-
sub.load("Loading " + name);
58-
addSkin(new SkinStyle() {
59-
@Override
60-
public String getURL() {
61-
return url;
62-
}
53+
try (DirectoryStream<Path> stream = Files.newDirectoryStream(skinFolder)) {
54+
stream.forEach(path -> {
55+
String fileName = path.getFileName().toString();
56+
if (!Files.isDirectory(path) && (fileName.endsWith(".css") || fileName.endsWith(".bss"))) {
57+
String url = path.toUri().toString();
58+
String name = StringUtil.upperFirst(fileName.substring(0, fileName.length() - 4));
59+
sub.load("Loading " + name);
60+
addSkin(new SkinStyle() {
61+
@Override
62+
public String getURL() {
63+
return url;
64+
}
6365

64-
@Override
65-
public String getName() {
66-
return name;
67-
}
68-
});
69-
}
70-
});
66+
@Override
67+
public String getName() {
68+
return name;
69+
}
70+
});
71+
}
72+
});
73+
}
7174
}
7275
throw new Exception();
7376
} catch (Exception e) {

0 commit comments

Comments
 (0)