Skip to content

Commit

Permalink
Modernize use of NIO API
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Jan 28, 2025
1 parent b37c286 commit 7747f03
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- Modernize use of NIO API


[Unreleased]: https://github.com/nbbrd/jdemetra-sa-advanced/compare/v2.2.5...HEAD

Expand Down
9 changes: 5 additions & 4 deletions jd_stl/src/main/java/be/nbb/demetra/stl/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
import ec.tstoolkit.data.AutoRegressiveSpectrum;
import ec.tstoolkit.data.DataBlock;
import ec.tstoolkit.maths.matrices.Matrix;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Locale;

/**
Expand Down Expand Up @@ -92,7 +93,7 @@ private static boolean decodeArgs(String[] args) {
return false;
}
try {
data = MatrixSerializer.read(new File(str));
data = MatrixSerializer.read(Paths.get(str).toFile());
} catch (IOException ex) {
System.out.println("Invalid data");
return false;
Expand Down Expand Up @@ -335,11 +336,11 @@ private static DataBlock y(int col) {
}

private static File generateFile(String name, int col) {
File path = new File(output == null ? "." : output);
File path = Paths.get(output == null ? "." : output).toFile();
if (!path.exists()) {
path.mkdirs();
}
return new File(path, name + ("-") + (col + 1) + ".txt");
return path.toPath().resolve(name + ("-") + (col + 1) + ".txt").toFile();
}

private static int periodicity() {
Expand Down
9 changes: 5 additions & 4 deletions jdr/src/main/java/ec/tstoolkit/jdr/ws/Workspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import ec.tstoolkit.timeseries.simplets.TsData;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -49,8 +50,8 @@ public class Workspace {

public static Workspace open(String fileName) throws IOException {
// try {
File file = new File(fileName);
FileWorkspace fws = FileWorkspace.open(file.toPath());
Path file = java.nio.file.Paths.get(fileName);
FileWorkspace fws = FileWorkspace.open(file);
Collection<WorkspaceItem> items = fws.getItems();
ProcessingContext context = new ProcessingContext();
Map<WorkspaceItem, GregorianCalendarManager> cal = FileRepository.loadAllCalendars(fws, context);
Expand Down Expand Up @@ -112,9 +113,9 @@ public void compute(String name) {
}

public boolean save(String fileName) {
File file = new File(fileName);
Path file = java.nio.file.Paths.get(fileName);
try {
try (FileWorkspace fws = FileWorkspace.create(file.toPath(), FileFormat.GENERIC)) {
try (FileWorkspace fws = FileWorkspace.create(file, FileFormat.GENERIC)) {
fws.setName(Paths.getBaseName(fileName));
for (MultiProcessing p : multiProcessing) {
WorkspaceItem cur = WorkspaceItem.builder()
Expand Down
3 changes: 2 additions & 1 deletion jdr/src/main/java/jd2/workspace/io/Jaxb.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.*;
import java.nio.file.Files;
import java.util.Objects;

/**
Expand Down Expand Up @@ -142,7 +143,7 @@ private static <T> T parseStream(Unmarshaller engine, InputStream resource) thro
}

private static <T> T parseFileXXE(Unmarshaller engine, File source, XMLInputFactory xxe) throws IOException {
try (FileInputStream resource = new FileInputStream(source)) {
try (InputStream resource = Files.newInputStream(source.toPath())) {
XMLStreamReader reader = xxe.createXMLStreamReader(resource);
try {
return (T) engine.unmarshal(reader);
Expand Down
12 changes: 4 additions & 8 deletions jdr/src/main/java/jd2/workspace/io/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
*/
package jd2.workspace.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.NonNull;

/**
* Set of utilities related to XML.
Expand All @@ -47,7 +43,7 @@ default T parseChars(@NonNull CharSequence source) throws IOException {

@NonNull
default T parseFile(@NonNull File source) throws IOException {
return parseStream(() -> new FileInputStream(source));
return parseStream(() -> Files.newInputStream(source.toPath()));
}

@NonNull
Expand Down
2 changes: 1 addition & 1 deletion jdr/src/main/java/jd2/workspace/util/Paths.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static String getBaseName(String file) {

public static String getFullPath(String file) {
try {
File tmp = new File(file);
File tmp = java.nio.file.Paths.get(file).toFile();
return splitFile(tmp.getCanonicalPath())[0];
} catch (IOException ex) {
return null;
Expand Down
9 changes: 5 additions & 4 deletions jdr/src/test/java/jd2/workspace/file/FileWorkspaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Map;

Expand All @@ -39,17 +40,17 @@ public FileWorkspaceTest() {

//@Test
public void testSomeMethod() throws IOException {
File file = new File("c:\\sarepository\\mytest.xml");
FileWorkspace ws = FileWorkspace.open(file.toPath());
Path file = Paths.get("c:\\sarepository\\mytest.xml");
FileWorkspace ws = FileWorkspace.open(file);
Collection<WorkspaceItem> items = ws.getItems();
items.forEach(item -> System.out.println(item.getLabel()));
ProcessingContext context = new ProcessingContext();
Map<WorkspaceItem, GregorianCalendarManager> cal = FileRepository.loadAllCalendars(ws, context);
Map<WorkspaceItem, TsVariables> vars = FileRepository.loadAllVariables(ws, context);
Map<WorkspaceItem, SaProcessingType> sa = FileRepository.loadAllSaProcessing(ws, context);

File file2 = new File("c:\\sarepository\\mytest2.xml");
FileWorkspace ws2 = FileWorkspace.create(file2.toPath(), FileFormat.GENERIC);
Path file2 = Paths.get("c:\\sarepository\\mytest2.xml");
FileWorkspace ws2 = FileWorkspace.create(file2, FileFormat.GENERIC);
ws.copyTo(ws2);
ws2.setName("myTest2");
ws.close();
Expand Down

0 comments on commit 7747f03

Please sign in to comment.