-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNEURepoFile.java
More file actions
60 lines (50 loc) · 2 KB
/
NEURepoFile.java
File metadata and controls
60 lines (50 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package io.github.moulberry.repo;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@AllArgsConstructor(access = AccessLevel.PACKAGE)
public class NEURepoFile {
public String getPath() {
return repository.baseFolder.relativize(fsPath).toString().replace("\\", "/");
}
@Getter
private NEURepository repository;
@Getter
private Path fsPath;
public <T> T json(TypeToken<T> type) throws NEURepositoryException {
try (Reader reader = Files.newBufferedReader(fsPath, StandardCharsets.UTF_8)) {
return repository.gson.fromJson(reader, type.getType());
} catch (IOException | SecurityException e) {
throw new NEURepositoryException(getPath(), "Could not read file", e);
} catch (JsonSyntaxException e) {
throw new NEURepositoryException(getPath(), "Invalid Json Syntax", e);
}
}
public <T> T json(Class<T> $class) throws NEURepositoryException {
try (Reader reader = Files.newBufferedReader(fsPath, StandardCharsets.UTF_8)) {
return repository.gson.fromJson(reader, $class);
} catch (IOException | SecurityException e) {
throw new NEURepositoryException(getPath(), "Could not read file", e);
} catch (JsonSyntaxException e) {
throw new NEURepositoryException(getPath(), "Invalid Json Syntax", e);
}
}
public InputStream stream() throws NEURepositoryException {
try {
return Files.newInputStream(fsPath);
} catch (IOException | SecurityException e) {
throw new NEURepositoryException(getPath(), "Could not read file", e);
}
}
public boolean isFile() {
return Files.isRegularFile(fsPath);
}
}