Skip to content

Commit b84c8e6

Browse files
committed
1.62
1 parent f0233ab commit b84c8e6

File tree

8 files changed

+164
-36
lines changed

8 files changed

+164
-36
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package dev.felnull.fnjl;
22

33
public class FNJLBuildIn {
4-
protected static final String VERSION = "1.61";
4+
protected static final String VERSION = "1.62";
55
}

common/src/main/java/dev/felnull/fnjl/math/FNFace.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public FN3dLine getEdge(int num) {
7878
case 2:
7979
return new FN3dLine(getToX(), getToY(), getToZ(), getFromX(), getToY(), getFromZ());
8080
case 3:
81-
return new FN3dLine(getFromX(), getFromY(), getFromZ(), getFromX(), getToY(), getFromZ());
81+
return new FN3dLine(getFromX(), getFromY(), getFromZ(), getFromX(), getToY(), getFromZ());
8282
}
8383
throw new IllegalStateException("Invalid edge number");
8484
}

common/src/main/java/dev/felnull/fnjl/util/FNDataUtil.java

Lines changed: 147 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import dev.felnull.fnjl.io.watcher.SingleFileSystemWatcherImpl;
88
import org.jetbrains.annotations.NotNull;
99
import org.jetbrains.annotations.Nullable;
10+
import org.jetbrains.annotations.Range;
1011

1112
import java.io.*;
1213
import 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
/**

common/src/main/java/dev/felnull/fnjl/util/FNURLUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public static PostResponse getResponseByPOST(@NotNull URL url, @NotNull PostRequ
258258
byte[] res;
259259
String encoding = con.getContentEncoding();
260260
try (InputStream in = con.getInputStream()) {
261-
res = FNDataUtil.streamToByteArray(in);
261+
res = FNDataUtil.readAllBytesBuff(in);
262262
}
263263
return new PostResponse(res, sts, encoding);
264264
}
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package dev.felnull.fnjltest;
22

3+
import dev.felnull.fnjl.util.FNDataUtil;
4+
5+
import java.io.BufferedInputStream;
6+
import java.io.BufferedOutputStream;
7+
import java.io.FileOutputStream;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
311
public class Main {
412
public static void main(String[] args) throws Exception {
5-
13+
int ret = FNDataUtil.inputToOutputBuff(new BufferedInputStream(Files.newInputStream(Paths.get("./test/test2.mp4"))), new BufferedOutputStream(new FileOutputStream("./test/test.mp4")), 1024);
14+
System.out.println(ret);
615
}
16+
717
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fnjl_group=dev.felnull
22
fnjl_name=felnull-java-library
3-
fnjl_version=1.61
3+
fnjl_version=1.62
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.felnull.fnjln;
22

33
public class FNJLNBuildIn {
4-
protected static final String VERSION = "1.61";
4+
protected static final String VERSION = "1.62";
55

66
protected static final int NATIVE_LIBRARY_VERSION = 1;
77
}

natives/src/main/java/dev/felnull/fnjln/FelNullJavaLibraryNatives.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public synchronized static void init(String libraryLocation, Path libraryPath) {
6565

6666
try (InputStream stream = FNDataUtil.resourceExtractor(FelNullJavaLibraryNatives.class, libraryLocation + libName)) {
6767
if (stream != null) {
68-
byte[] data = FNDataUtil.streamToByteArray(stream);
68+
byte[] data = FNDataUtil.readAllBytes(stream);
6969
outLibFIle.delete();
7070
Files.write(outLibFIle.toPath(), data);
7171
loaded = loadLibrary(outLibFIle);

0 commit comments

Comments
 (0)