diff --git a/src/main/java/io/github/nstdio/http/ext/DecompressingSubscriber.java b/src/main/java/io/github/nstdio/http/ext/DecompressingSubscriber.java index 6043aff..9339ade 100644 --- a/src/main/java/io/github/nstdio/http/ext/DecompressingSubscriber.java +++ b/src/main/java/io/github/nstdio/http/ext/DecompressingSubscriber.java @@ -16,6 +16,8 @@ package io.github.nstdio.http.ext; +import static io.github.nstdio.http.ext.IOUtils.closeQuietly; + import java.io.EOFException; import java.io.IOException; import java.io.InputStream; @@ -67,14 +69,6 @@ private DecompressingSubscriber(BodySubscriber downstream, UnaryOperator getBody() { return downstream.getBody(); diff --git a/src/main/java/io/github/nstdio/http/ext/DiskCache.java b/src/main/java/io/github/nstdio/http/ext/DiskCache.java index 469e6bc..6a6b031 100644 --- a/src/main/java/io/github/nstdio/http/ext/DiskCache.java +++ b/src/main/java/io/github/nstdio/http/ext/DiskCache.java @@ -16,6 +16,9 @@ package io.github.nstdio.http.ext; +import static io.github.nstdio.http.ext.IOUtils.delete; +import static io.github.nstdio.http.ext.IOUtils.size; + import lombok.Getter; import lombok.Value; import lombok.experimental.Accessors; @@ -55,21 +58,6 @@ class DiskCache extends SizeConstrainedCache { restore(); } - private static long size(Path path) { - try { - return Files.size(path); - } catch (IOException e) { - return -1; - } - } - - private static void delete(Path path) { - try { - Files.delete(path); - } catch (IOException ignored) { - } - } - private void restore() { var namePredicate = Pattern.compile("[a-f0-9]{32}_m").asMatchPredicate(); diff --git a/src/main/java/io/github/nstdio/http/ext/IOUtils.java b/src/main/java/io/github/nstdio/http/ext/IOUtils.java new file mode 100644 index 0000000..035e224 --- /dev/null +++ b/src/main/java/io/github/nstdio/http/ext/IOUtils.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2022 Edgar Asatryan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.github.nstdio.http.ext; + +import java.io.Closeable; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +class IOUtils { + private IOUtils() { + } + + static void closeQuietly(Closeable c) { + if (c != null) { + try { + c.close(); + } catch (IOException ignored) {} + } + } + + static void delete(Path path) { + try { + Files.delete(path); + } catch (IOException ignored) {} + } + + static long size(Path path) { + try { + return Files.size(path); + } catch (IOException e) { + return -1; + } + } +} diff --git a/src/main/java/io/github/nstdio/http/ext/PathReadingSubscription.java b/src/main/java/io/github/nstdio/http/ext/PathReadingSubscription.java index 049025b..077ddcf 100644 --- a/src/main/java/io/github/nstdio/http/ext/PathReadingSubscription.java +++ b/src/main/java/io/github/nstdio/http/ext/PathReadingSubscription.java @@ -16,6 +16,8 @@ package io.github.nstdio.http.ext; +import static io.github.nstdio.http.ext.IOUtils.closeQuietly; + import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; @@ -75,11 +77,6 @@ public void request(long n) { @Override public void cancel() { completed.set(true); - if (channel != null) { - try { - channel.close(); - } catch (IOException ignored) { - } - } + closeQuietly(channel); } } diff --git a/src/main/java/io/github/nstdio/http/ext/PathSubscriber.java b/src/main/java/io/github/nstdio/http/ext/PathSubscriber.java index 0cf55ce..05c36ce 100644 --- a/src/main/java/io/github/nstdio/http/ext/PathSubscriber.java +++ b/src/main/java/io/github/nstdio/http/ext/PathSubscriber.java @@ -16,6 +16,7 @@ package io.github.nstdio.http.ext; +import static io.github.nstdio.http.ext.IOUtils.closeQuietly; import static java.nio.file.StandardOpenOption.CREATE; import static java.nio.file.StandardOpenOption.WRITE; @@ -74,13 +75,7 @@ public void onNext(List item) { } private void close() { - if (out != null) { - try { - out.close(); - } catch (IOException ignore) { - // ignore - } - } + closeQuietly(out); } @Override