4
4
import dev .ultreon .ubo .util .DataTypeVisitor ;
5
5
6
6
import java .io .*;
7
+ import java .net .URI ;
7
8
import java .net .URL ;
8
9
import java .nio .file .Files ;
10
+ import java .nio .file .OpenOption ;
11
+ import java .nio .file .Path ;
9
12
import java .util .zip .GZIPInputStream ;
10
13
import java .util .zip .GZIPOutputStream ;
11
14
12
15
public class DataIo {
13
- private static final short VERSION = 3 ;
16
+ private static final short VERSION = 4 ;
14
17
private static final int HEADER = 0xff804269 ;
15
18
private static final int BUFFER_SIZE = 4096 ;
16
19
@@ -20,6 +23,20 @@ public static <T extends DataType<?>> T read(File file, T... type) throws IOExce
20
23
return read (stream , type );
21
24
}
22
25
}
26
+
27
+ @ SafeVarargs
28
+ public static <T extends DataType <?>> T read (Path path , T ... type ) throws IOException {
29
+ try (InputStream stream = new BufferedInputStream (Files .newInputStream (path ), BUFFER_SIZE )) {
30
+ return read (stream , type );
31
+ }
32
+ }
33
+
34
+ @ SafeVarargs
35
+ public static <T extends DataType <?>> T read (Path path , OpenOption [] options , T ... type ) throws IOException {
36
+ try (InputStream stream = new BufferedInputStream (Files .newInputStream (path , options ), BUFFER_SIZE )) {
37
+ return read (stream , type );
38
+ }
39
+ }
23
40
24
41
@ SafeVarargs
25
42
public static <T extends DataType <?>> T read (URL url , T ... type ) throws IOException {
@@ -28,6 +45,13 @@ public static <T extends DataType<?>> T read(URL url, T... type) throws IOExcept
28
45
}
29
46
}
30
47
48
+ @ SafeVarargs
49
+ public static <T extends DataType <?>> T read (URI uri , T ... type ) throws IOException {
50
+ try (InputStream stream = new BufferedInputStream (uri .toURL ().openStream (), BUFFER_SIZE )) {
51
+ return read (stream , type );
52
+ }
53
+ }
54
+
31
55
/**
32
56
* @throws IOException when an I/O error occurs.
33
57
* @throws DataTypeException when the read data type is invalid.
@@ -45,7 +69,6 @@ public static <T extends DataType<?>> T read(InputStream stream, T... type) thro
45
69
* @throws DataTypeException when the read data type is invalid.
46
70
*/
47
71
@ SafeVarargs
48
- @ SuppressWarnings ("unchecked" )
49
72
public static <T extends DataType <?>> T read (DataInput input , T ... type ) throws IOException {
50
73
int magic = input .readInt ();
51
74
if (magic != HEADER ) {
@@ -74,6 +97,20 @@ public static <T extends DataType<?>> T readCompressed(File file, T... type) thr
74
97
return readCompressed (stream , type );
75
98
}
76
99
}
100
+
101
+ @ SafeVarargs
102
+ public static <T extends DataType <?>> T readCompressed (Path path , T ... type ) throws IOException {
103
+ try (InputStream stream = new BufferedInputStream (Files .newInputStream (path ), BUFFER_SIZE )) {
104
+ return readCompressed (stream , type );
105
+ }
106
+ }
107
+
108
+ @ SafeVarargs
109
+ public static <T extends DataType <?>> T readCompressed (Path path , OpenOption [] options , T ... type ) throws IOException {
110
+ try (InputStream stream = new BufferedInputStream (Files .newInputStream (path , options ), BUFFER_SIZE )) {
111
+ return readCompressed (stream , type );
112
+ }
113
+ }
77
114
78
115
@ SafeVarargs
79
116
public static <T extends DataType <?>> T readCompressed (URL url , T ... type ) throws IOException {
@@ -82,24 +119,133 @@ public static <T extends DataType<?>> T readCompressed(URL url, T... type) throw
82
119
}
83
120
}
84
121
122
+ @ SafeVarargs
123
+ public static <T extends DataType <?>> T readCompressed (URI uri , T ... type ) throws IOException {
124
+ try (InputStream stream = new BufferedInputStream (uri .toURL ().openConnection ().getInputStream ())) {
125
+ return readCompressed (stream , type );
126
+ }
127
+ }
128
+
85
129
@ SafeVarargs
86
130
public static <T extends DataType <?>> T readCompressed (InputStream stream , T ... type ) throws IOException {
87
131
GZIPInputStream gzipStream = new GZIPInputStream (stream );
88
132
return read (gzipStream , type );
89
133
}
90
134
135
+ public static <T extends DataType <?>> T read (File file , Class <T > type ) throws IOException {
136
+ try (InputStream stream = new BufferedInputStream (Files .newInputStream (file .toPath ()), BUFFER_SIZE )) {
137
+ return read (stream , type );
138
+ }
139
+ }
140
+
141
+ public static <T extends DataType <?>> T read (Path path , Class <T > type , OpenOption ... options ) throws IOException {
142
+ try (InputStream stream = new BufferedInputStream (Files .newInputStream (path , options ), BUFFER_SIZE )) {
143
+ return read (stream , type );
144
+ }
145
+ }
146
+
147
+ public static <T extends DataType <?>> T read (URL url , Class <T > type ) throws IOException {
148
+ try (InputStream stream = new BufferedInputStream (url .openStream (), BUFFER_SIZE )) {
149
+ return read (stream , type );
150
+ }
151
+ }
152
+
153
+ public static <T extends DataType <?>> T read (URI uri , Class <T > type ) throws IOException {
154
+ try (InputStream stream = new BufferedInputStream (uri .toURL ().openStream (), BUFFER_SIZE )) {
155
+ return read (stream , type );
156
+ }
157
+ }
158
+
159
+ /**
160
+ * @throws IOException when an I/O error occurs.
161
+ * @throws DataTypeException when the read data type is invalid.
162
+ */
163
+ public static <T extends DataType <?>> T read (InputStream stream , Class <T > type ) throws IOException {
164
+ if (stream instanceof DataInput ) {
165
+ return read ((DataInput ) stream , type );
166
+ }
167
+ return read ((DataInput ) new DataInputStream (stream ), type );
168
+ }
169
+
170
+ /**
171
+ * @throws IOException when an I/O error occurs.
172
+ * @throws DataTypeException when the read data type is invalid.
173
+ */
174
+ public static <T extends DataType <?>> T read (DataInput input , Class <T > type ) throws IOException {
175
+ int magic = input .readInt ();
176
+ if (magic != HEADER ) {
177
+ throw new StreamCorruptedException (String .format ("Invalid header got 0x%08X (expected 0xFF804269)" , magic ));
178
+ }
179
+
180
+ short readVersion = input .readShort ();
181
+ if (readVersion > VERSION ) {
182
+ throw new FutureVersionException (readVersion , VERSION );
183
+ }
184
+
185
+ Class <T > componentType = (Class <T >) type .getClass ().getComponentType ();
186
+ int componentId = DataTypeRegistry .getId (componentType );
187
+ int id = input .readUnsignedByte ();
188
+
189
+ if (componentId != id ) {
190
+ throw new DataTypeException ("The read data id " + id + " is different from the expected id: " + componentId );
191
+ }
192
+
193
+ return (T ) DataTypeRegistry .read (id , input );
194
+ }
195
+
196
+ public static <T extends DataType <?>> T readCompressed (File file , Class <T > type ) throws IOException {
197
+ try (InputStream stream = new BufferedInputStream (Files .newInputStream (file .toPath ()), BUFFER_SIZE )) {
198
+ return readCompressed (stream , type );
199
+ }
200
+ }
201
+
202
+ public static <T extends DataType <?>> T readCompressed (Path path , Class <T > type , OpenOption ... options ) throws IOException {
203
+ try (InputStream stream = new BufferedInputStream (Files .newInputStream (path , options ), BUFFER_SIZE )) {
204
+ return readCompressed (stream , type );
205
+ }
206
+ }
207
+
208
+ public static <T extends DataType <?>> T readCompressed (URL url , Class <T > type ) throws IOException {
209
+ try (InputStream stream = new BufferedInputStream (url .openStream ())) {
210
+ return readCompressed (stream , type );
211
+ }
212
+ }
213
+
214
+ public static <T extends DataType <?>> T readCompressed (URI uri , Class <T > type ) throws IOException {
215
+ try (InputStream stream = new BufferedInputStream (uri .toURL ().openConnection ().getInputStream ())) {
216
+ return readCompressed (stream , type );
217
+ }
218
+ }
219
+
220
+ public static <T extends DataType <?>> T readCompressed (InputStream stream , Class <T > type ) throws IOException {
221
+ GZIPInputStream gzipStream = new GZIPInputStream (stream );
222
+ return read (gzipStream , type );
223
+ }
224
+
91
225
public static void write (DataType <?> dataType , File file ) throws IOException {
92
226
try (OutputStream stream = new BufferedOutputStream (Files .newOutputStream (file .toPath ()), BUFFER_SIZE )) {
93
227
write (dataType , stream );
94
228
}
95
229
}
230
+
231
+ public static void write (DataType <?> dataType , Path path , OpenOption ... options ) throws IOException {
232
+ try (OutputStream stream = new BufferedOutputStream (Files .newOutputStream (path , options ), BUFFER_SIZE )) {
233
+ write (dataType , stream );
234
+ }
235
+ }
96
236
97
237
public static void write (DataType <?> dataType , URL file ) throws IOException {
98
238
try (OutputStream stream = new BufferedOutputStream (file .openConnection ().getOutputStream (), BUFFER_SIZE )) {
99
239
write (dataType , stream );
100
240
}
101
241
}
102
242
243
+ public static void wrote (DataType <?> dataType , URI uri ) throws IOException {
244
+ try (OutputStream stream = new BufferedOutputStream (uri .toURL ().openConnection ().getOutputStream (), BUFFER_SIZE )) {
245
+ write (dataType , stream );
246
+ }
247
+ }
248
+
103
249
public static void write (DataType <?> dataType , OutputStream stream ) throws IOException {
104
250
if (stream instanceof DataOutput ) {
105
251
write (dataType , (DataOutput ) stream );
@@ -120,11 +266,23 @@ public static void writeCompressed(DataType<?> dataType, URL file) throws IOExce
120
266
}
121
267
}
122
268
269
+ public static void writeCompressed (DataType <?> dataType , URI uri ) throws IOException {
270
+ try (OutputStream stream = new BufferedOutputStream (uri .toURL ().openConnection ().getOutputStream (), BUFFER_SIZE )) {
271
+ writeCompressed (dataType , stream );
272
+ }
273
+ }
274
+
123
275
public static void writeCompressed (DataType <?> dataType , File file ) throws IOException {
124
276
try (OutputStream stream = new BufferedOutputStream (Files .newOutputStream (file .toPath ()), BUFFER_SIZE )) {
125
277
writeCompressed (dataType , stream );
126
278
}
127
279
}
280
+
281
+ public static void writeCompressed (DataType <?> dataType , Path path , OpenOption ... options ) throws IOException {
282
+ try (OutputStream stream = new BufferedOutputStream (Files .newOutputStream (path , options ), BUFFER_SIZE )) {
283
+ writeCompressed (dataType , stream );
284
+ }
285
+ }
128
286
129
287
public static void writeCompressed (DataType <?> dataType , OutputStream stream ) throws IOException {
130
288
GZIPOutputStream gzipStream = new GZIPOutputStream (stream );
@@ -141,15 +299,22 @@ public static <T> T visit(DataTypeVisitor<T> visitor, DataType<?> dataType) {
141
299
return dataType .accept (visitor );
142
300
}
143
301
144
- @ SuppressWarnings ("unchecked" )
145
302
@ SafeVarargs
303
+ @ Deprecated
146
304
public static <T extends DataType <?>> T fromUso (String value , T ... type ) throws IOException {
147
305
try (BufferedReader reader = new BufferedReader (new StringReader (value ))) {
148
306
DataType <?> iDataType = readUso (reader .readLine ());
149
307
return (T ) iDataType ;
150
308
}
151
309
}
152
310
311
+ public static <T extends DataType <?>> T fromUso (String value ) throws IOException {
312
+ try (BufferedReader reader = new BufferedReader (new StringReader (value ))) {
313
+ DataType <?> iDataType = readUso (reader .readLine ());
314
+ return (T ) iDataType ;
315
+ }
316
+ }
317
+
153
318
private static DataType <?> readUso (String value ) throws IOException {
154
319
return new UsoParser (value ).parse ();
155
320
}
0 commit comments