1
1
package dev .felnull .fnjl .util ;
2
2
3
+ import dev .felnull .fnjl .io .FileWatcher ;
3
4
import dev .felnull .fnjl .io .ProgressWriter ;
4
5
5
6
import java .io .*;
6
7
import java .net .HttpURLConnection ;
7
8
import java .net .URL ;
9
+ import java .nio .file .Path ;
10
+ import java .nio .file .WatchEvent ;
8
11
import java .security .MessageDigest ;
9
12
import java .security .NoSuchAlgorithmException ;
10
13
import java .util .concurrent .atomic .AtomicInteger ;
14
+ import java .util .function .BiConsumer ;
11
15
import java .util .function .Consumer ;
12
16
import java .util .zip .GZIPInputStream ;
13
17
import java .util .zip .GZIPOutputStream ;
18
+ import java .util .zip .ZipEntry ;
19
+ import java .util .zip .ZipInputStream ;
14
20
15
21
/**
16
22
* データ関連
20
26
*/
21
27
public class FNDataUtil {
22
28
29
+ /**
30
+ * バッファー付きストリームをバイト配列へ変換
31
+ *
32
+ * @param stream ストリーム
33
+ * @return 変換済みバイト配列
34
+ * @throws IOException 変換失敗
35
+ */
36
+ public static byte [] bufStreamToByteArray (InputStream stream ) throws IOException {
37
+ ByteArrayOutputStream bout = new ByteArrayOutputStream ();
38
+ bufInputToOutput (stream , bout );
39
+ return bout .toByteArray ();
40
+ }
41
+
42
+ /**
43
+ * バッファー付きストリームをバイト配列へ変換
44
+ *
45
+ * @param stream ストリーム
46
+ * @param size 一度に書き込む量
47
+ * @return 変換済みバイト配列
48
+ * @throws IOException 変換失敗
49
+ */
50
+ public static byte [] bufStreamToByteArray (InputStream stream , int size ) throws IOException {
51
+ ByteArrayOutputStream bout = new ByteArrayOutputStream ();
52
+ bufInputToOutput (stream , bout , size );
53
+ return bout .toByteArray ();
54
+ }
55
+
23
56
/**
24
57
* ストリームをバイト配列へ変換
25
58
*
@@ -29,14 +62,21 @@ public class FNDataUtil {
29
62
*/
30
63
public static byte [] streamToByteArray (InputStream stream ) throws IOException {
31
64
ByteArrayOutputStream bout = new ByteArrayOutputStream ();
32
- byte [] buffer = new byte [1024 ];
33
- while (true ) {
34
- int len = stream .read (buffer );
35
- if (len < 0 ) {
36
- break ;
37
- }
38
- bout .write (buffer , 0 , len );
39
- }
65
+ inputToOutput (stream , bout );
66
+ return bout .toByteArray ();
67
+ }
68
+
69
+ /**
70
+ * ストリームをバイト配列へ変換
71
+ *
72
+ * @param stream ストリーム
73
+ * @param size 一度に書き込む量
74
+ * @return 変換済みバイト配列
75
+ * @throws IOException 変換失敗
76
+ */
77
+ public static byte [] streamToByteArray (InputStream stream , int size ) throws IOException {
78
+ ByteArrayOutputStream bout = new ByteArrayOutputStream ();
79
+ inputToOutput (stream , bout , size );
40
80
return bout .toByteArray ();
41
81
}
42
82
@@ -195,4 +235,107 @@ public static InputStream resourceExtractor(Class<?> targetClass, String path) {
195
235
stream = ClassLoader .getSystemResourceAsStream (path );
196
236
return stream != null ? new BufferedInputStream (stream ) : null ;
197
237
}
238
+
239
+ /**
240
+ * ファイル監視
241
+ *
242
+ * @param path 監視対象
243
+ * @param listener 監視listener
244
+ * @param events 監視エベント StandardWatchEventKinds.ENTRY_MODIFYなど
245
+ * @throws IOException 例外
246
+ */
247
+ public static void watchFile (Path path , Consumer <WatchEvent <?>> listener , WatchEvent .Kind <?>... events ) throws IOException {
248
+ FileWatcher watcher = new FileWatcher (path , listener , events );
249
+ watcher .start ();
250
+ }
251
+
252
+ /**
253
+ * インプットストリームをアウトプットストリームへ
254
+ *
255
+ * @param inputStream In
256
+ * @param outputStream Out
257
+ * @throws IOException 例外
258
+ */
259
+ public static void inputToOutput (InputStream inputStream , OutputStream outputStream ) throws IOException {
260
+ inputToOutput (inputStream , outputStream , 1024 );
261
+ }
262
+
263
+ /**
264
+ * インプットストリームをアウトプットストリームへ
265
+ *
266
+ * @param inputStream In
267
+ * @param outputStream Out
268
+ * @param size 一度に書き込む量
269
+ * @throws IOException 例外
270
+ */
271
+ public static void inputToOutput (InputStream inputStream , OutputStream outputStream , int size ) throws IOException {
272
+ try (InputStream in = inputStream ; OutputStream out = outputStream ) {
273
+ byte [] data = new byte [size ];
274
+ int len ;
275
+ while ((len = in .read (data )) != -1 ) {
276
+ out .write (data , 0 , len );
277
+ }
278
+ }
279
+ }
280
+
281
+ /**
282
+ * バッファー付きインプットストリームをアウトプットストリームへ
283
+ *
284
+ * @param inputStream In
285
+ * @param outputStream Out
286
+ * @param size 一度に書き込む量
287
+ * @throws IOException 例外
288
+ */
289
+ public static void bufInputToOutput (InputStream inputStream , OutputStream outputStream , int size ) throws IOException {
290
+ inputToOutput (new BufferedInputStream (inputStream ), new BufferedOutputStream (outputStream ), size );
291
+ }
292
+
293
+ /**
294
+ * バッファー付きインプットストリームをアウトプットストリームへ
295
+ *
296
+ * @param inputStream In
297
+ * @param outputStream Out
298
+ * @throws IOException 例外
299
+ */
300
+ public static void bufInputToOutput (InputStream inputStream , OutputStream outputStream ) throws IOException {
301
+ inputToOutput (new BufferedInputStream (inputStream ), new BufferedOutputStream (outputStream ));
302
+ }
303
+
304
+ /**
305
+ * Zipファイルを読み取る
306
+ *
307
+ * @param zipStream Zipのストリーム
308
+ * @param zips Zipエントリー
309
+ * @throws IOException 例外
310
+ */
311
+ public static void readZip (InputStream zipStream , BiConsumer <ZipEntry , ZipInputStream > zips ) throws IOException {
312
+ try (ZipInputStream zis = new ZipInputStream (zipStream )) {
313
+ ZipEntry ze ;
314
+ while ((ze = zis .getNextEntry ()) != null ) {
315
+ zips .accept (ze , zis );
316
+ }
317
+ }
318
+ }
319
+
320
+ /**
321
+ * Zipファイルを読み取り、ストリームへ変換
322
+ *
323
+ * @param zipStream Zipのストリーム
324
+ * @param zips Zipエントリー
325
+ * @throws IOException 例外
326
+ */
327
+ public static void readZipStreamed (InputStream zipStream , BiConsumer <ZipEntry , InputStream > zips ) throws IOException {
328
+ FNDataUtil .readZip (zipStream , (e , i ) -> {
329
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
330
+ byte [] data = new byte [1024 ];
331
+ int count ;
332
+ while ((count = i .read (data )) != -1 ) {
333
+ baos .write (data , 0 , count );
334
+ }
335
+ zips .accept (e , new ByteArrayInputStream (baos .toByteArray ()));
336
+ } catch (IOException ex ) {
337
+ zips .accept (e , null );
338
+ }
339
+ });
340
+ }
198
341
}
0 commit comments