4
4
5
5
import java .io .File ;
6
6
import java .io .IOException ;
7
+ import java .io .OutputStream ;
8
+ import java .nio .charset .Charset ;
7
9
import java .nio .file .Files ;
8
10
import java .nio .file .Path ;
9
11
import java .util .Map ;
@@ -77,7 +79,7 @@ public String writeToString() {
77
79
78
80
for (LevelObject block : blocks )
79
81
if (blockMap .containsKey (block .getBlock ()))
80
- blockMap .put (block .getBlock (), blockMap .get (block .getBlock ()) + "*" + block .getCoordinate (). toString () );
82
+ blockMap .put (block .getBlock (), blockMap .get (block .getBlock ()) + "*" + block .getCoordinate ());
81
83
else
82
84
blockMap .put (block .getBlock (), block .getCoordinate ().toString ());
83
85
@@ -89,12 +91,50 @@ public String writeToString() {
89
91
return builder .toString ();
90
92
}
91
93
94
+ /**
95
+ * <p>Writes to a byte array using the default charset.</p>
96
+ * <p>The exporter will only write to the byte buffer if there is enough space, stopping if the length if too small.</p>
97
+ * @param data Byte Array to encode into
98
+ * @throws IllegalArgumentException if the data is null
99
+ */
100
+ public void writeToByteArray (byte [] data ) throws IllegalArgumentException {
101
+ writeToByteArray (data , Charset .defaultCharset ());
102
+ }
103
+
104
+ /**
105
+ * <p>Writes to a byte array.</p>
106
+ * <p>The exporter will only write to the byte buffer if there is enough space, stopping if the length if too small.</p>
107
+ * @param data Byte Array to encode into
108
+ * @param charset Charset to encode bytes with
109
+ * @throws IllegalArgumentException if the data or charset is null
110
+ */
111
+ public void writeToByteArray (byte [] data , @ NotNull Charset charset ) throws IllegalArgumentException {
112
+ if (data == null ) throw new IllegalArgumentException ("Data cannot be null" );
113
+ if (charset == null ) throw new IllegalArgumentException ("Charset cannot be null" );
114
+
115
+ String string = writeToString ();
116
+ byte [] bytes = string .getBytes (charset );
117
+ System .arraycopy (bytes , 0 , data , 0 , Math .min (bytes .length , data .length ));
118
+ }
119
+
120
+ /**
121
+ * Writes to a file using the default charset. This will create the file if it does not exist.
122
+ * @param file File
123
+ * @throws IllegalArgumentException if the file is null
124
+ */
125
+ public void writeToFile (@ NotNull File file ) throws IllegalArgumentException {
126
+ writeToFile (file , Charset .defaultCharset ());
127
+ }
128
+
92
129
/**
93
130
* Writes to a file. This will create the file if it does not exist.
94
131
* @param file File
132
+ * @param charset Charset to encode bytes with
133
+ * @throws IllegalArgumentException if the file or charset is null
95
134
*/
96
- public void writeToFile (@ NotNull File file ) {
135
+ public void writeToFile (@ NotNull File file , @ NotNull Charset charset ) throws IllegalArgumentException {
97
136
if (file == null ) throw new IllegalArgumentException ("File cannot be null" );
137
+ if (charset == null ) throw new IllegalArgumentException ("Charset cannot be null" );
98
138
99
139
if (!file .exists ())
100
140
try {
@@ -103,19 +143,58 @@ public void writeToFile(@NotNull File file) {
103
143
throw new RuntimeException (e );
104
144
}
105
145
106
- writeToPath (file .toPath ());
146
+ writeToPath (file .toPath (), charset );
147
+ }
148
+
149
+ /**
150
+ * Writes to a file path using the default charset.
151
+ * @param file File Path
152
+ * @throws IllegalArgumentException if the path is null
153
+ */
154
+ public void writeToPath (@ NotNull Path file ) throws IllegalArgumentException {
155
+ writeToPath (file , Charset .defaultCharset ());
107
156
}
108
157
109
158
/**
110
159
* Writes to a file path.
111
160
* @param file File Path
161
+ * @param charset Charset to encode bytes with
162
+ * @throws IllegalArgumentException if the path or charset is null
112
163
*/
113
- public void writeToPath (@ NotNull Path file ) {
164
+ public void writeToPath (@ NotNull Path file , @ NotNull Charset charset ) throws IllegalArgumentException {
114
165
if (file == null ) throw new IllegalArgumentException ("Path cannot be null" );
166
+ if (charset == null ) throw new IllegalArgumentException ("Charset cannot be null" );
167
+
168
+ String data = writeToString ();
169
+ try {
170
+ Files .write (file , data .getBytes (charset ));
171
+ } catch (IOException e ) {
172
+ throw new RuntimeException (e );
173
+ }
174
+ }
175
+
176
+ /**
177
+ * Writes this level to an output stream using the default charset.
178
+ * @param stream Output Stream
179
+ * @throws IllegalArgumentException if the stream is null
180
+ */
181
+ public void writeToStream (@ NotNull OutputStream stream ) throws IllegalArgumentException {
182
+ writeToStream (stream , Charset .defaultCharset ());
183
+ }
184
+
185
+ /**
186
+ * Writes this level to an output stream.
187
+ * @param stream Output Stream
188
+ * @param charset Charset to encode bytes with
189
+ * @throws IllegalArgumentException if the stream or charset is null
190
+ */
191
+ public void writeToStream (@ NotNull OutputStream stream , @ NotNull Charset charset ) throws IllegalArgumentException {
192
+ if (stream == null ) throw new IllegalArgumentException ("Stream cannot be null" );
193
+ if (charset == null ) throw new IllegalArgumentException ("Charset cannot be null" );
115
194
116
195
String data = writeToString ();
117
196
try {
118
- Files .write (file , data .getBytes ());
197
+ stream .write (data .getBytes (charset ));
119
198
} catch (IOException e ) {
120
199
throw new RuntimeException (e );
121
200
}
0 commit comments