77import dev .felnull .fnjl .io .watcher .SingleFileSystemWatcherImpl ;
88import org .jetbrains .annotations .NotNull ;
99import org .jetbrains .annotations .Nullable ;
10+ import org .jetbrains .annotations .Range ;
1011
1112import java .io .*;
1213import java .net .HttpURLConnection ;
@@ -47,7 +48,8 @@ public class FNDataUtil {
4748 * @return 変換後
4849 * @throws IOException 例外
4950 */
50- public static String readAllString (InputStream stream ) throws IOException {
51+ @ NotNull
52+ public static String readAllString (@ NotNull InputStream stream ) throws IOException {
5153 return readAllString (stream , StandardCharsets .UTF_8 );
5254 }
5355
@@ -59,7 +61,8 @@ public static String readAllString(InputStream stream) throws IOException {
5961 * @return 変換後
6062 * @throws IOException 例外
6163 */
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 {
6366 try (Reader reader = new InputStreamReader (stream , cs )) {
6467 return readAllString (reader );
6568 }
@@ -72,7 +75,8 @@ public static String readAllString(InputStream stream, Charset cs) throws IOExce
7275 * @return 変換後
7376 * @throws IOException 例外
7477 */
75- public static String readAllString (Reader reader ) throws IOException {
78+ @ NotNull
79+ public static String readAllString (@ NotNull Reader reader ) throws IOException {
7680 StringBuilder sb = new StringBuilder ();
7781 boolean flg = false ;
7882 try (BufferedReader breader = new BufferedReader (reader )) {
@@ -87,59 +91,79 @@ public static String readAllString(Reader reader) throws IOException {
8791 }
8892
8993 /**
90- * バッファー付きストリームをバイト配列へ変換
94+ * バッファー付きでストリームをバイト配列へ変換
9195 *
9296 * @param stream ストリーム
9397 * @return 変換済みバイト配列
9498 * @throws IOException 変換失敗
9599 */
96- public static byte [] bufStreamToByteArray ( InputStream stream ) throws IOException {
100+ public static byte [] readAllBytesBuff ( @ NotNull InputStream stream ) throws IOException {
97101 ByteArrayOutputStream bout = new ByteArrayOutputStream ();
98- bufInputToOutput (stream , bout );
102+ inputToOutputBuff (stream , bout );
99103 return bout .toByteArray ();
100104 }
101105
106+ @ Deprecated
107+ public static byte [] bufStreamToByteArray (InputStream stream ) throws IOException {
108+ return readAllBytesBuff (stream );
109+ }
110+
102111 /**
103- * バッファー付きストリームをバイト配列へ変換
112+ * バッファー付きでストリームをバイト配列へ変換
104113 *
105- * @param stream ストリーム
106- * @param size 一度に書き込む量
114+ * @param stream ストリーム
115+ * @param readSize 一度に書き込む量
107116 * @return 変換済みバイト配列
108117 * @throws IOException 変換失敗
109118 */
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 {
111120 ByteArrayOutputStream bout = new ByteArrayOutputStream ();
112- bufInputToOutput (stream , bout , size );
121+ inputToOutputBuff (stream , bout , readSize );
113122 return bout .toByteArray ();
114123 }
115124
125+ @ Deprecated
126+ public static byte [] bufStreamToByteArray (InputStream stream , int size ) throws IOException {
127+ return readAllBytesBuff (stream , size );
128+ }
129+
116130 /**
117131 * ストリームをバイト配列へ変換
118132 *
119133 * @param stream ストリーム
120134 * @return 変換済みバイト配列
121135 * @throws IOException 変換失敗
122136 */
123- public static byte [] streamToByteArray ( InputStream stream ) throws IOException {
137+ public static byte [] readAllBytes ( @ NotNull InputStream stream ) throws IOException {
124138 ByteArrayOutputStream bout = new ByteArrayOutputStream ();
125139 inputToOutput (stream , bout );
126140 return bout .toByteArray ();
127141 }
128142
143+ @ Deprecated
144+ public static byte [] streamToByteArray (InputStream stream ) throws IOException {
145+ return readAllBytes (stream );
146+ }
147+
129148 /**
130149 * ストリームをバイト配列へ変換
131150 *
132- * @param stream ストリーム
133- * @param size 一度に書き込む量
151+ * @param stream ストリーム
152+ * @param readSize 一度に書き込む量
134153 * @return 変換済みバイト配列
135154 * @throws IOException 変換失敗
136155 */
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 {
138157 ByteArrayOutputStream bout = new ByteArrayOutputStream ();
139- inputToOutput (stream , bout , size );
158+ inputToOutput (stream , bout , readSize );
140159 return bout .toByteArray ();
141160 }
142161
162+ @ Deprecated
163+ public static byte [] streamToByteArray (InputStream stream , int size ) throws IOException {
164+ return readAllBytes (stream , size );
165+ }
166+
143167 /**
144168 * ストリームをGZ圧縮したストリームへ変換
145169 *
@@ -162,7 +186,8 @@ public static InputStream zipGz(@NotNull InputStream data) throws IOException {
162186 * @return 解凍済みストリーム
163187 * @throws IOException 変換失敗
164188 */
165- public static InputStream unzipGz (InputStream data ) throws IOException {
189+ @ NotNull
190+ public static InputStream unzipGz (@ NotNull InputStream data ) throws IOException {
166191 return new GZIPInputStream (data );
167192 }
168193
@@ -288,12 +313,10 @@ public static byte[] fileLoadToProgress(File file, Consumer<ProgressWriter.Write
288313 */
289314 @ Nullable
290315 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 );
293317
294318 InputStream stream = clazz .getResourceAsStream ("/" + path );
295- if (stream == null )
296- stream = ClassLoader .getSystemResourceAsStream (path );
319+ if (stream == null ) stream = ClassLoader .getSystemResourceAsStream (path );
297320 return stream != null ? new BufferedInputStream (stream ) : null ;
298321 }
299322
@@ -404,51 +427,146 @@ public static FileSystemWatcher watchDirectoryTree(@NotNull Path rootPath, @NotN
404427 *
405428 * @param inputStream In
406429 * @param outputStream Out
430+ * @return 合計サイズ
407431 * @throws IOException 例外
408432 */
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 );
411436 }
412437
413438 /**
414439 * インプットストリームをアウトプットストリームへ
415440 *
416441 * @param inputStream In
417442 * @param outputStream Out
418- * @param size 一度に書き込む量
443+ * @param readSize 一度に書き込む量
444+ * @return 合計サイズ
419445 * @throws IOException 例外
420446 */
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 ;
422450 try (InputStream in = inputStream ; OutputStream out = outputStream ) {
423- byte [] data = new byte [size ];
451+ byte [] data = new byte [readSize ];
424452 int len ;
425453 while ((len = in .read (data )) != -1 ) {
454+ ct += len ;
426455 out .write (data , 0 , len );
427456 }
428457 }
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 );
429486 }
430487
431488 /**
432489 * バッファー付きインプットストリームをアウトプットストリームへ
433490 *
434491 * @param inputStream In
435492 * @param outputStream Out
436- * @param size 一度に書き込む量
493+ * @param readSize 一度に書き込む量
494+ * @return 合計サイズ
437495 * @throws IOException 例外
438496 */
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
439503 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 );
441505 }
442506
443507 /**
444508 * バッファー付きインプットストリームをアウトプットストリームへ
445509 *
446510 * @param inputStream In
447511 * @param outputStream Out
512+ * @return 合計サイズ
448513 * @throws IOException 例外
449514 */
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
450521 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 );
452570 }
453571
454572 /**
0 commit comments