Skip to content

Commit 3e40ee1

Browse files
committed
1.54
1 parent 75482c2 commit 3e40ee1

File tree

7 files changed

+106
-59
lines changed

7 files changed

+106
-59
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.53";
4+
protected static final String VERSION = "1.54";
55
}

common/src/main/java/dev/felnull/fnjl/io/ProgressWriter.java

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,41 @@ public ProgressWriter(InputStream stream, long length, Function<WriteData, IOExc
2626
}
2727

2828
public void start() throws IOException {
29-
BufferedInputStream bstream = new BufferedInputStream(stream);
30-
byte[] data = new byte[1024];
31-
long ct = 0;
32-
int x;
33-
while ((x = bstream.read(data, 0, 1024)) >= 0) {
34-
int finalX = x;
35-
ct += x;
36-
IOException ex = writer.apply(new WriteData() {
37-
@Override
38-
public byte[] getBytes() {
39-
return data;
40-
}
29+
try (BufferedInputStream bstream = new BufferedInputStream(stream)) {
30+
byte[] data = new byte[1024];
31+
long ct = 0;
32+
int x;
33+
while ((x = bstream.read(data, 0, 1024)) >= 0) {
34+
int finalX = x;
35+
ct += x;
36+
IOException ex = writer.apply(new WriteData() {
37+
@Override
38+
public byte[] getBytes() {
39+
return data;
40+
}
4141

42-
@Override
43-
public long getReadSize() {
44-
return finalX;
45-
}
46-
});
42+
@Override
43+
public long getReadSize() {
44+
return finalX;
45+
}
46+
});
4747

48-
long finalCt = ct;
49-
progress.accept(new WriteProgressListener() {
50-
@Override
51-
public long getLength() {
52-
return length;
53-
}
48+
long finalCt = ct;
49+
progress.accept(new WriteProgressListener() {
50+
@Override
51+
public long getLength() {
52+
return length;
53+
}
5454

55-
@Override
56-
public long getWrittenLength() {
57-
return finalCt;
58-
}
59-
});
55+
@Override
56+
public long getWrittenLength() {
57+
return finalCt;
58+
}
59+
});
6060

61-
if (ex != null)
62-
throw ex;
61+
if (ex != null)
62+
throw ex;
63+
}
6364
}
6465
}
6566

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

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

3+
import dev.felnull.fnjl.io.PostResponse;
4+
35
import java.io.IOException;
46
import java.net.URL;
57
import java.util.concurrent.CompletableFuture;
@@ -48,13 +50,18 @@ public int send() throws IOException {
4850
return FNURLUtil.getResponseByPOST(new URL(url), createContent(), "jp", "application/JSON").getCode();
4951
}
5052

53+
@Deprecated
5154
public CompletableFuture<Void> sendAsync(Consumer<Integer> response) throws IOException {
52-
return FNURLUtil.getResponseByPOSTAsync(new URL(url), createContent(), "jp", "application/JSON", n -> {
55+
return FNURLUtil.getResponseByPOSTAsync(new URL(url), createContent(), "jp", "application/JSON").thenAccept(n -> {
5356
if (response != null)
5457
response.accept(n.getCode());
5558
});
5659
}
5760

61+
public CompletableFuture<Integer> sendAsync() throws IOException {
62+
return FNURLUtil.getResponseByPOSTAsync(new URL(url), createContent(), "jp", "application/JSON").thenApply(PostResponse::getCode);
63+
}
64+
5865
public static DiscordWebHookBuilder newBuilder(String url, String content) {
5966
return new DiscordWebHookBuilder(url, content);
6067
}

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

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

33
import dev.felnull.fnjl.io.FileWatcher;
44
import dev.felnull.fnjl.io.ProgressWriter;
5+
import org.jetbrains.annotations.NotNull;
56

67
import java.io.*;
78
import java.net.HttpURLConnection;
@@ -139,13 +140,12 @@ public static byte[] streamToByteArray(InputStream stream, int size) throws IOEx
139140
* @return GZ圧縮済みストリーム
140141
* @throws IOException 変換失敗
141142
*/
142-
public static InputStream zipGz(InputStream data) throws IOException {
143-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
144-
GZIPOutputStream gos = new GZIPOutputStream(baos);
145-
gos.write(streamToByteArray(data));
146-
gos.close();
147-
baos.close();
148-
return new ByteArrayInputStream(baos.toByteArray());
143+
@NotNull
144+
public static InputStream zipGz(@NotNull InputStream data) throws IOException {
145+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gos = new GZIPOutputStream(baos)) {
146+
inputToOutput(data, gos);
147+
return new ByteArrayInputStream(baos.toByteArray());
148+
}
149149
}
150150

151151
/**
@@ -221,7 +221,7 @@ public static void fileDownloadToProgress(URL url, File file, Consumer<ProgressW
221221
* @throws IOException 例外
222222
*/
223223
public static void fileCopyToProgress(File copyFile, File file, Consumer<ProgressWriter.WriteProgressListener> progress) throws IOException {
224-
fileWriteToProgress(new FileInputStream(copyFile), copyFile.length(), file, progress);
224+
fileWriteToProgress(Files.newInputStream(copyFile.toPath()), copyFile.length(), file, progress);
225225
}
226226

227227
/**

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

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,62 +108,101 @@ public static String getResponse(@NotNull URL url) throws IOException {
108108
/**
109109
* 非同期でストリームを取得
110110
* 失敗時はnullを返す
111+
* {@link #getStreamAsync(URL)}に移行してください
111112
*
112113
* @param url URL
113114
* @param streamConsumer ストリーム
114115
* @return 処理結果
115116
*/
117+
@Deprecated
116118
public static CompletableFuture<Void> getStreamAsync(URL url, Consumer<InputStream> streamConsumer) {
117-
return CompletableFuture.runAsync(() -> {
118-
InputStream stream = null;
119+
return getStreamAsync(url).thenAccept(streamConsumer);
120+
}
121+
122+
/**
123+
* 非同期でストリームを取得
124+
* 失敗時はnullを返す
125+
*
126+
* @param url URL
127+
* @return ストリーム
128+
*/
129+
@NotNull
130+
public static CompletableFuture<InputStream> getStreamAsync(@NotNull URL url) {
131+
return CompletableFuture.supplyAsync(() -> {
119132
try {
120-
stream = getStream(url);
133+
return getStream(url);
121134
} catch (IOException e) {
122-
e.printStackTrace();
135+
return null;
123136
}
124-
streamConsumer.accept(stream);
125137
});
126138
}
127139

128140
/**
129141
* 非同期で文字列を取得
130142
* 失敗時はnullを返す
143+
* {@link #getResponseAsync(URL)}に移行してください
131144
*
132145
* @param url URL
133146
* @param stringConsumer 文字列
134147
* @return 処理結果
135148
*/
149+
@Deprecated()
136150
public static CompletableFuture<Void> getResponseAsync(URL url, Consumer<String> stringConsumer) {
137-
return CompletableFuture.runAsync(() -> {
138-
String str = null;
151+
return getResponseAsync(url).thenAccept(stringConsumer);
152+
}
153+
154+
/**
155+
* 非同期で文字列を取得
156+
* 失敗時はnullを返す
157+
*
158+
* @param url URL
159+
* @return 処理結果
160+
*/
161+
@NotNull
162+
public static CompletableFuture<String> getResponseAsync(@NotNull URL url) {
163+
return CompletableFuture.supplyAsync(() -> {
139164
try {
140-
str = getResponse(url);
165+
return getResponse(url);
141166
} catch (IOException e) {
142-
e.printStackTrace();
167+
return null;
143168
}
144-
stringConsumer.accept(str);
145169
});
146170
}
147171

148172
/**
149-
* POSTでテキストを送り返ってきた文字列とステータスコードを取得
173+
* 非同期でPOSTでテキストを送り返ってきた文字列とステータスコードを取得
174+
* {@link #getResponseByPOSTAsync(URL, String, String, String)}に移行してください
150175
*
151176
* @param url URL
152177
* @param body テキスト
153178
* @param language 言語
154179
* @param contentType type
155180
* @param responseConsumer 返答とステータスコードのペア
156-
* @return 返答とステータスコードのペア
181+
* @return 返答
157182
*/
183+
@Deprecated
158184
public static CompletableFuture<Void> getResponseByPOSTAsync(URL url, String body, String language, String contentType, Consumer<PostResponse> responseConsumer) {
159-
return CompletableFuture.runAsync(() -> {
160-
PostResponse ret = null;
185+
return getResponseByPOSTAsync(url, body, language, contentType).thenAccept(responseConsumer);
186+
}
187+
188+
/**
189+
* 非同期でPOSTでテキストを送り返ってきた文字列とステータスコードを取得
190+
* 失敗時はnullを返す
191+
*
192+
* @param url URL
193+
* @param body テキスト
194+
* @param language 言語
195+
* @param contentType type
196+
* @return 返答
197+
*/
198+
@NotNull
199+
public static CompletableFuture<PostResponse> getResponseByPOSTAsync(@NotNull URL url, @NotNull String body, @NotNull String language, @NotNull String contentType) {
200+
return CompletableFuture.supplyAsync(() -> {
161201
try {
162-
ret = getResponseByPOST(url, body, language, contentType);
202+
return getResponseByPOST(url, body, language, contentType);
163203
} catch (IOException e) {
164-
e.printStackTrace();
204+
return null;
165205
}
166-
responseConsumer.accept(ret);
167206
});
168207
}
169208

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

66
protected static final int NATIVE_LIBRARY_VERSION = 1;
77
}

0 commit comments

Comments
 (0)