summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorjonmv <venstad@gmail.com>2022-11-05 09:24:21 +0100
committerjonmv <venstad@gmail.com>2022-11-07 09:59:27 +0100
commitf8a4549269d9df145a4f27c5368a2578f18128a7 (patch)
treeee802d429172bbb301221a1dea2fe48a34bed9b6 /vespajlib
parenta2eca5f54c45838d072f9641505baa3ae0d26746 (diff)
Revert "Merge pull request #24763 from vespa-engine/jonmv/revert-streams"
This reverts commit 6d8bca79a1f600501290593ecd920eca0b237c78, reversing changes made to 36374eb2d3cc94c3792dd0a70963244abb6284b4.
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/abi-spec.json1
-rw-r--r--vespajlib/src/main/java/com/yahoo/compress/ArchiveStreamReader.java4
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/LazyInputStream.java53
-rw-r--r--vespajlib/src/main/java/com/yahoo/yolean/concurrent/Memoized.java8
4 files changed, 64 insertions, 2 deletions
diff --git a/vespajlib/abi-spec.json b/vespajlib/abi-spec.json
index a22e24aafc2..8a2e68a8d8c 100644
--- a/vespajlib/abi-spec.json
+++ b/vespajlib/abi-spec.json
@@ -3833,6 +3833,7 @@
"public"
],
"methods" : [
+ "public void <init>(java.util.function.Supplier)",
"public void <init>(java.util.function.Supplier, com.yahoo.yolean.concurrent.Memoized$Closer)",
"public static com.yahoo.yolean.concurrent.Memoized of(java.util.function.Supplier)",
"public static com.yahoo.yolean.concurrent.Memoized combine(com.yahoo.yolean.concurrent.Memoized, java.util.function.Function, com.yahoo.yolean.concurrent.Memoized$Closer)",
diff --git a/vespajlib/src/main/java/com/yahoo/compress/ArchiveStreamReader.java b/vespajlib/src/main/java/com/yahoo/compress/ArchiveStreamReader.java
index e65a645f5be..f8faf655415 100644
--- a/vespajlib/src/main/java/com/yahoo/compress/ArchiveStreamReader.java
+++ b/vespajlib/src/main/java/com/yahoo/compress/ArchiveStreamReader.java
@@ -136,8 +136,8 @@ public class ArchiveStreamReader implements AutoCloseable {
// Commons Compress only has limited support for symlinks as they are only detected when the ZIP file is read
// through org.apache.commons.compress.archivers.zip.ZipFile. This is not the case in this class, because it must
// support reading ZIP files from generic input streams. The check below thus always returns false.
- if (entry instanceof ZipArchiveEntry) return ((ZipArchiveEntry) entry).isUnixSymlink();
- if (entry instanceof TarArchiveEntry) return ((TarArchiveEntry) entry).isSymbolicLink();
+ if (entry instanceof ZipArchiveEntry zipEntry) return zipEntry.isUnixSymlink();
+ if (entry instanceof TarArchiveEntry tarEntry) return tarEntry.isSymbolicLink();
throw new IllegalArgumentException("Unsupported archive entry " + entry.getClass().getSimpleName() + ", cannot check for symbolic link");
}
diff --git a/vespajlib/src/main/java/com/yahoo/io/LazyInputStream.java b/vespajlib/src/main/java/com/yahoo/io/LazyInputStream.java
new file mode 100644
index 00000000000..3ff7ada6b59
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/io/LazyInputStream.java
@@ -0,0 +1,53 @@
+package com.yahoo.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.function.Supplier;
+
+/**
+ * Input stream wrapping an input stream supplier, which doesn't have content yet at declaration time.
+ *
+ * @author jonmv
+ */
+public class LazyInputStream extends InputStream {
+
+ private Supplier<InputStream> source;
+ private InputStream delegate;
+
+ public LazyInputStream(Supplier<InputStream> source) {
+ this.source = source;
+ }
+
+ private InputStream in() {
+ if (delegate == null) {
+ delegate = source.get();
+ source = null;
+ }
+ return delegate;
+ }
+
+ @Override
+ public int read() throws IOException { return in().read(); }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException { return in().read(b, off, len); }
+
+ @Override
+ public long skip(long n) throws IOException { return in().skip(n); }
+
+ @Override
+ public int available() throws IOException { return in().available(); }
+
+ @Override
+ public void close() throws IOException { in().close(); }
+
+ @Override
+ public synchronized void mark(int readlimit) { in().mark(readlimit); }
+
+ @Override
+ public synchronized void reset() throws IOException { in().reset(); }
+
+ @Override
+ public boolean markSupported() { return in().markSupported(); }
+
+}
diff --git a/vespajlib/src/main/java/com/yahoo/yolean/concurrent/Memoized.java b/vespajlib/src/main/java/com/yahoo/yolean/concurrent/Memoized.java
index 8e2b7b7a7eb..ba5ef7bab2d 100644
--- a/vespajlib/src/main/java/com/yahoo/yolean/concurrent/Memoized.java
+++ b/vespajlib/src/main/java/com/yahoo/yolean/concurrent/Memoized.java
@@ -34,15 +34,23 @@ public class Memoized<T, E extends Exception> implements Supplier<T>, AutoClosea
private volatile T wrapped;
private Supplier<T> factory;
+ /** Returns a new Memoized which has no close method. */
+ public Memoized(Supplier<T> factory) {
+ this(factory, __ -> { });
+ }
+
+ /** Returns a new Memoized with the given factory and closer. */
public Memoized(Supplier<T> factory, Closer<T, E> closer) {
this.factory = requireNonNull(factory);
this.closer = requireNonNull(closer);
}
+ /** Returns a generic AutoCloseable Memoized with the given AutoCloseable-supplier. */
public static <T extends AutoCloseable> Memoized<T, ?> of(Supplier<T> factory) {
return new Memoized<>(factory, AutoCloseable::close);
}
+ /** Composes the given memoized with a function taking its output as an argument to produce a new Memoized, with the given closer. */
public static <T, U, E extends Exception> Memoized<U, E> combine(Memoized<T, ? extends E> inner, Function<T, U> outer, Closer<U, ? extends E> closer) {
return new Memoized<>(() -> outer.apply(inner.get()), compose(closer, inner::close));
}