-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharsetYamlConfiguration.java
111 lines (101 loc) · 3.2 KB
/
CharsetYamlConfiguration.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package net.codersky.mcutils.files.yaml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import com.google.common.io.Files;
/**
* A {@link YamlConfiguration} class with {@link Charset} support.
*
* @author xDec0de_
*
* @since MCUtils 1.0.0
*/
public class CharsetYamlConfiguration extends YamlConfiguration {
private final Charset charset;
/**
* Creates a new {@link YamlConfiguration} with {@link Charset} support.
* If <b>charset</b> is null {@link StandardCharsets#UTF_8} will be used.
*
* @param charset the charset to use.
*
* @since MCUtils 1.0.0
*/
public CharsetYamlConfiguration(@Nonnull Charset charset) {
this.charset = charset == null ? StandardCharsets.UTF_8 : charset;
}
/**
* Gets the {@link Charset} being used by this {@link CharsetYamlConfiguration}.
*
* @return the charset being used.
*
* @since MCUtils 1.0.0
*/
@Nonnull
public Charset getCharset() {
return charset;
}
/**
* Saves this {@link CharsetYamlConfiguration} to the specified location.
* <p>
* If the file does not exist, it will be created. If already exists, it
* will be overwritten. If it cannot be overwritten or created, an
* exception will be thrown.
* <p>
* This method will save using {@link #getCharset()}.
*
* @param file File to save to, if null, nothing will be done.
*
* @throws IOException Thrown when the given file cannot be written to for any reason.
*
* @since MCUtils 1.0.0
*/
@Override
public void save(@Nullable File file) throws IOException {
if (file == null)
return;
Files.createParentDirs(file);
String data = this.saveToString();
Writer writer = new OutputStreamWriter(new FileOutputStream(file), charset);
try {
writer.write(data);
} finally {
writer.close();
}
}
/**
* Loads this {@link CharsetYamlConfiguration} from the specified location.
* <p>
* All the values contained within this configuration will be removed,
* leaving only settings and defaults, and the new values will be loaded
* from the given file.
* <p>
* If the file cannot be loaded for any reason, an exception will be
* thrown.
* <p>
* This method will load using {@link #getCharset()}.
*
* @param file File to load from.
* @throws FileNotFoundException Thrown when the given file cannot be opened or <b>file</b> is null.
* @throws IOException Thrown when the given file cannot be read.
* @throws InvalidConfigurationException Thrown when the given file is not a valid Configuration
*
* @since MCUtils 1.0.0
*/
@Override
public void load(@Nonnull File file) throws FileNotFoundException, IOException, InvalidConfigurationException {
if (file == null)
throw new FileNotFoundException("File is null");
this.load(new InputStreamReader(new FileInputStream(file), charset));
}
}