Skip to content

Commit 9b6a74d

Browse files
committed
1.48
1 parent ec891ba commit 9b6a74d

File tree

6 files changed

+156
-46
lines changed

6 files changed

+156
-46
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.47";
4+
protected static final String VERSION = "1.48";
55
}

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

Lines changed: 151 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package dev.felnull.fnjl.util;
22

3+
import dev.felnull.fnjl.io.FileWatcher;
34
import dev.felnull.fnjl.io.ProgressWriter;
45

56
import java.io.*;
67
import java.net.HttpURLConnection;
78
import java.net.URL;
9+
import java.nio.file.Path;
10+
import java.nio.file.WatchEvent;
811
import java.security.MessageDigest;
912
import java.security.NoSuchAlgorithmException;
1013
import java.util.concurrent.atomic.AtomicInteger;
14+
import java.util.function.BiConsumer;
1115
import java.util.function.Consumer;
1216
import java.util.zip.GZIPInputStream;
1317
import java.util.zip.GZIPOutputStream;
18+
import java.util.zip.ZipEntry;
19+
import java.util.zip.ZipInputStream;
1420

1521
/**
1622
* データ関連
@@ -20,6 +26,33 @@
2026
*/
2127
public class FNDataUtil {
2228

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+
2356
/**
2457
* ストリームをバイト配列へ変換
2558
*
@@ -29,14 +62,21 @@ public class FNDataUtil {
2962
*/
3063
public static byte[] streamToByteArray(InputStream stream) throws IOException {
3164
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);
4080
return bout.toByteArray();
4181
}
4282

@@ -195,4 +235,107 @@ public static InputStream resourceExtractor(Class<?> targetClass, String path) {
195235
stream = ClassLoader.getSystemResourceAsStream(path);
196236
return stream != null ? new BufferedInputStream(stream) : null;
197237
}
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+
}
198341
}

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

Lines changed: 0 additions & 29 deletions
This file was deleted.

common/src/test/java/dev/felnull/fnjltest/Main.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
public class Main {
44
public static void main(String[] args) throws Exception {
5-
// FNQuadruple<Double, Double, Double, Double> q = FNMath.toQuaternion(Math.toRadians(20), Math.toRadians(30), Math.toRadians(40));
6-
// FNTriple<Double, Double, Double> e = FNMath.toEulerAngle(q.getLeft(), q.getLeftCenter(), q.getRightCenter(), q.getRight());
7-
// System.out.println(Math.toDegrees(e.getLeft()) + ":" + Math.toDegrees(e.getCenter()) + ":" + Math.toDegrees(e.getRight()));
8-
// System.out.println(new FNVec3d(0.34906585039886584, 0.5235987755982988, 0.6981317007977317).degrees());
9-
// System.out.println(new FNVec3d(10, 150, 300).radians());
10-
// System.out.println(new FNVec3d(10, 150, 300).radians().toQuaternion().toEulerAngle().normalized().degrees());
5+
6+
117
}
128
}

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.47
3+
fnjl_version=1.48
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.47";
4+
protected static final String VERSION = "1.48";
55

66
protected static final int NATIVE_LIBRARY_VERSION = 1;
77
}

0 commit comments

Comments
 (0)