7
7
import dev .felnull .fnjl .io .watcher .SingleFileSystemWatcherImpl ;
8
8
import org .jetbrains .annotations .NotNull ;
9
9
import org .jetbrains .annotations .Nullable ;
10
+ import org .jetbrains .annotations .Range ;
10
11
11
12
import java .io .*;
12
13
import java .net .HttpURLConnection ;
@@ -47,7 +48,8 @@ public class FNDataUtil {
47
48
* @return 変換後
48
49
* @throws IOException 例外
49
50
*/
50
- public static String readAllString (InputStream stream ) throws IOException {
51
+ @ NotNull
52
+ public static String readAllString (@ NotNull InputStream stream ) throws IOException {
51
53
return readAllString (stream , StandardCharsets .UTF_8 );
52
54
}
53
55
@@ -59,7 +61,8 @@ public static String readAllString(InputStream stream) throws IOException {
59
61
* @return 変換後
60
62
* @throws IOException 例外
61
63
*/
62
- public static String readAllString (InputStream stream , Charset cs ) throws IOException {
64
+ @ NotNull
65
+ public static String readAllString (@ NotNull InputStream stream , @ NotNull Charset cs ) throws IOException {
63
66
try (Reader reader = new InputStreamReader (stream , cs )) {
64
67
return readAllString (reader );
65
68
}
@@ -72,7 +75,8 @@ public static String readAllString(InputStream stream, Charset cs) throws IOExce
72
75
* @return 変換後
73
76
* @throws IOException 例外
74
77
*/
75
- public static String readAllString (Reader reader ) throws IOException {
78
+ @ NotNull
79
+ public static String readAllString (@ NotNull Reader reader ) throws IOException {
76
80
StringBuilder sb = new StringBuilder ();
77
81
boolean flg = false ;
78
82
try (BufferedReader breader = new BufferedReader (reader )) {
@@ -87,59 +91,79 @@ public static String readAllString(Reader reader) throws IOException {
87
91
}
88
92
89
93
/**
90
- * バッファー付きストリームをバイト配列へ変換
94
+ * バッファー付きでストリームをバイト配列へ変換
91
95
*
92
96
* @param stream ストリーム
93
97
* @return 変換済みバイト配列
94
98
* @throws IOException 変換失敗
95
99
*/
96
- public static byte [] bufStreamToByteArray ( InputStream stream ) throws IOException {
100
+ public static byte [] readAllBytesBuff ( @ NotNull InputStream stream ) throws IOException {
97
101
ByteArrayOutputStream bout = new ByteArrayOutputStream ();
98
- bufInputToOutput (stream , bout );
102
+ inputToOutputBuff (stream , bout );
99
103
return bout .toByteArray ();
100
104
}
101
105
106
+ @ Deprecated
107
+ public static byte [] bufStreamToByteArray (InputStream stream ) throws IOException {
108
+ return readAllBytesBuff (stream );
109
+ }
110
+
102
111
/**
103
- * バッファー付きストリームをバイト配列へ変換
112
+ * バッファー付きでストリームをバイト配列へ変換
104
113
*
105
- * @param stream ストリーム
106
- * @param size 一度に書き込む量
114
+ * @param stream ストリーム
115
+ * @param readSize 一度に書き込む量
107
116
* @return 変換済みバイト配列
108
117
* @throws IOException 変換失敗
109
118
*/
110
- public static byte [] bufStreamToByteArray ( InputStream stream , int size ) throws IOException {
119
+ public static byte [] readAllBytesBuff ( @ NotNull InputStream stream , @ Range ( from = 0 , to = Integer . MAX_VALUE ) int readSize ) throws IOException {
111
120
ByteArrayOutputStream bout = new ByteArrayOutputStream ();
112
- bufInputToOutput (stream , bout , size );
121
+ inputToOutputBuff (stream , bout , readSize );
113
122
return bout .toByteArray ();
114
123
}
115
124
125
+ @ Deprecated
126
+ public static byte [] bufStreamToByteArray (InputStream stream , int size ) throws IOException {
127
+ return readAllBytesBuff (stream , size );
128
+ }
129
+
116
130
/**
117
131
* ストリームをバイト配列へ変換
118
132
*
119
133
* @param stream ストリーム
120
134
* @return 変換済みバイト配列
121
135
* @throws IOException 変換失敗
122
136
*/
123
- public static byte [] streamToByteArray ( InputStream stream ) throws IOException {
137
+ public static byte [] readAllBytes ( @ NotNull InputStream stream ) throws IOException {
124
138
ByteArrayOutputStream bout = new ByteArrayOutputStream ();
125
139
inputToOutput (stream , bout );
126
140
return bout .toByteArray ();
127
141
}
128
142
143
+ @ Deprecated
144
+ public static byte [] streamToByteArray (InputStream stream ) throws IOException {
145
+ return readAllBytes (stream );
146
+ }
147
+
129
148
/**
130
149
* ストリームをバイト配列へ変換
131
150
*
132
- * @param stream ストリーム
133
- * @param size 一度に書き込む量
151
+ * @param stream ストリーム
152
+ * @param readSize 一度に書き込む量
134
153
* @return 変換済みバイト配列
135
154
* @throws IOException 変換失敗
136
155
*/
137
- public static byte [] streamToByteArray ( InputStream stream , int size ) throws IOException {
156
+ public static byte [] readAllBytes ( @ NotNull InputStream stream , @ Range ( from = 0 , to = Integer . MAX_VALUE ) int readSize ) throws IOException {
138
157
ByteArrayOutputStream bout = new ByteArrayOutputStream ();
139
- inputToOutput (stream , bout , size );
158
+ inputToOutput (stream , bout , readSize );
140
159
return bout .toByteArray ();
141
160
}
142
161
162
+ @ Deprecated
163
+ public static byte [] streamToByteArray (InputStream stream , int size ) throws IOException {
164
+ return readAllBytes (stream , size );
165
+ }
166
+
143
167
/**
144
168
* ストリームをGZ圧縮したストリームへ変換
145
169
*
@@ -162,7 +186,8 @@ public static InputStream zipGz(@NotNull InputStream data) throws IOException {
162
186
* @return 解凍済みストリーム
163
187
* @throws IOException 変換失敗
164
188
*/
165
- public static InputStream unzipGz (InputStream data ) throws IOException {
189
+ @ NotNull
190
+ public static InputStream unzipGz (@ NotNull InputStream data ) throws IOException {
166
191
return new GZIPInputStream (data );
167
192
}
168
193
@@ -288,12 +313,10 @@ public static byte[] fileLoadToProgress(File file, Consumer<ProgressWriter.Write
288
313
*/
289
314
@ Nullable
290
315
public static InputStream resourceExtractor (@ NotNull Class <?> clazz , @ NotNull String path ) {
291
- if (path .startsWith ("/" ))
292
- path = path .substring (1 );
316
+ if (path .startsWith ("/" )) path = path .substring (1 );
293
317
294
318
InputStream stream = clazz .getResourceAsStream ("/" + path );
295
- if (stream == null )
296
- stream = ClassLoader .getSystemResourceAsStream (path );
319
+ if (stream == null ) stream = ClassLoader .getSystemResourceAsStream (path );
297
320
return stream != null ? new BufferedInputStream (stream ) : null ;
298
321
}
299
322
@@ -404,51 +427,146 @@ public static FileSystemWatcher watchDirectoryTree(@NotNull Path rootPath, @NotN
404
427
*
405
428
* @param inputStream In
406
429
* @param outputStream Out
430
+ * @return 合計サイズ
407
431
* @throws IOException 例外
408
432
*/
409
- public static void inputToOutput (InputStream inputStream , OutputStream outputStream ) throws IOException {
410
- inputToOutput (inputStream , outputStream , 1024 );
433
+ @ Range (from = 0 , to = Integer .MAX_VALUE )
434
+ public static int inputToOutput (@ NotNull InputStream inputStream , @ NotNull OutputStream outputStream ) throws IOException {
435
+ return inputToOutput (inputStream , outputStream , 1024 );
411
436
}
412
437
413
438
/**
414
439
* インプットストリームをアウトプットストリームへ
415
440
*
416
441
* @param inputStream In
417
442
* @param outputStream Out
418
- * @param size 一度に書き込む量
443
+ * @param readSize 一度に書き込む量
444
+ * @return 合計サイズ
419
445
* @throws IOException 例外
420
446
*/
421
- public static void inputToOutput (InputStream inputStream , OutputStream outputStream , int size ) throws IOException {
447
+ @ Range (from = 0 , to = Integer .MAX_VALUE )
448
+ public static int inputToOutput (@ NotNull InputStream inputStream , @ NotNull OutputStream outputStream , @ Range (from = 0 , to = Integer .MAX_VALUE ) int readSize ) throws IOException {
449
+ int ct = 0 ;
422
450
try (InputStream in = inputStream ; OutputStream out = outputStream ) {
423
- byte [] data = new byte [size ];
451
+ byte [] data = new byte [readSize ];
424
452
int len ;
425
453
while ((len = in .read (data )) != -1 ) {
454
+ ct += len ;
426
455
out .write (data , 0 , len );
427
456
}
428
457
}
458
+ return ct ;
459
+ }
460
+
461
+ /**
462
+ * インプットストリームをアウトプットストリームへ
463
+ *
464
+ * @param inputStream In
465
+ * @param outputStream Out
466
+ * @return 合計サイズ
467
+ * @throws IOException 例外
468
+ */
469
+ @ Range (from = 0 , to = Integer .MAX_VALUE )
470
+ public static int i2o (@ NotNull InputStream inputStream , @ NotNull OutputStream outputStream ) throws IOException {
471
+ return inputToOutput (inputStream , outputStream );
472
+ }
473
+
474
+ /**
475
+ * インプットストリームをアウトプットストリームへ
476
+ *
477
+ * @param inputStream In
478
+ * @param outputStream Out
479
+ * @param readSize 一度に書き込む量
480
+ * @return 合計サイズ
481
+ * @throws IOException 例外
482
+ */
483
+ @ Range (from = 0 , to = Integer .MAX_VALUE )
484
+ public static int i2o (@ NotNull InputStream inputStream , @ NotNull OutputStream outputStream , @ Range (from = 0 , to = Integer .MAX_VALUE ) int readSize ) throws IOException {
485
+ return inputToOutput (inputStream , outputStream , readSize );
429
486
}
430
487
431
488
/**
432
489
* バッファー付きインプットストリームをアウトプットストリームへ
433
490
*
434
491
* @param inputStream In
435
492
* @param outputStream Out
436
- * @param size 一度に書き込む量
493
+ * @param readSize 一度に書き込む量
494
+ * @return 合計サイズ
437
495
* @throws IOException 例外
438
496
*/
497
+ @ Range (from = 0 , to = Integer .MAX_VALUE )
498
+ public static int inputToOutputBuff (@ NotNull InputStream inputStream , @ NotNull OutputStream outputStream , @ Range (from = 0 , to = Integer .MAX_VALUE ) int readSize ) throws IOException {
499
+ return inputToOutput (new BufferedInputStream (inputStream ), new BufferedOutputStream (outputStream ), readSize );
500
+ }
501
+
502
+ @ Deprecated
439
503
public static void bufInputToOutput (InputStream inputStream , OutputStream outputStream , int size ) throws IOException {
440
- inputToOutput ( new BufferedInputStream ( inputStream ), new BufferedOutputStream ( outputStream ) , size );
504
+ inputToOutputBuff ( inputStream , outputStream , size );
441
505
}
442
506
443
507
/**
444
508
* バッファー付きインプットストリームをアウトプットストリームへ
445
509
*
446
510
* @param inputStream In
447
511
* @param outputStream Out
512
+ * @return 合計サイズ
448
513
* @throws IOException 例外
449
514
*/
515
+ @ Range (from = 0 , to = Integer .MAX_VALUE )
516
+ public static int inputToOutputBuff (@ NotNull InputStream inputStream , @ NotNull OutputStream outputStream ) throws IOException {
517
+ return inputToOutput (new BufferedInputStream (inputStream ), new BufferedOutputStream (outputStream ));
518
+ }
519
+
520
+ @ Deprecated
450
521
public static void bufInputToOutput (InputStream inputStream , OutputStream outputStream ) throws IOException {
451
- inputToOutput (new BufferedInputStream (inputStream ), new BufferedOutputStream (outputStream ));
522
+ inputToOutputBuff (inputStream , outputStream );
523
+ }
524
+
525
+ /**
526
+ * インプットストリームをアウトプットストリームへ
527
+ * サイズ制限付き
528
+ * 超えた場合は切り上げられる
529
+ *
530
+ * @param inputStream In
531
+ * @param outputStream Out
532
+ * @param readSize 一度に書き込む量
533
+ * @param limit 制限サイズ
534
+ * @return 制限サイズを超えた場合は-1、それ以外はサイズ
535
+ * @throws IOException 例外
536
+ */
537
+ @ Range (from = -1 , to = Integer .MAX_VALUE )
538
+ public static int inputToOutputLimit (@ NotNull InputStream inputStream , @ NotNull OutputStream outputStream , @ Range (from = 0 , to = Integer .MAX_VALUE ) int readSize , @ Range (from = 0 , to = Integer .MAX_VALUE ) int limit ) throws IOException {
539
+ int ct = 0 ;
540
+ boolean flg = false ;
541
+ try (InputStream in = inputStream ; OutputStream out = outputStream ) {
542
+ byte [] data = new byte [readSize ];
543
+ int len ;
544
+ while (!flg && (len = in .read (data )) != -1 ) {
545
+ if ((ct + len ) > limit ) {
546
+ len = limit - ct ;
547
+ flg = true ;
548
+ }
549
+ ct += len ;
550
+ out .write (data , 0 , len );
551
+ }
552
+ }
553
+ return flg ? -1 : ct ;
554
+ }
555
+
556
+ /**
557
+ * インプットストリームをアウトプットストリームへ
558
+ * サイズ制限付き
559
+ * 超えた場合は切り上げられる
560
+ *
561
+ * @param inputStream In
562
+ * @param outputStream Out
563
+ * @param limit 制限サイズ
564
+ * @return 制限サイズを超えた場合は-1、それ以外はサイズ
565
+ * @throws IOException 例外
566
+ */
567
+ @ Range (from = -1 , to = Integer .MAX_VALUE )
568
+ public static int inputToOutputLimit (@ NotNull InputStream inputStream , @ NotNull OutputStream outputStream , @ Range (from = 0 , to = Integer .MAX_VALUE ) int limit ) throws IOException {
569
+ return inputToOutputLimit (inputStream , outputStream , 1024 , limit );
452
570
}
453
571
454
572
/**
0 commit comments