You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2023/03/01 05:46:37 UTC
[causeway] branch master updated: CAUSEWAY-3304: Blob/Clob: adds reading from DataSource
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/causeway.git
The following commit(s) were added to refs/heads/master by this push:
new d6759abdc1 CAUSEWAY-3304: Blob/Clob: adds reading from DataSource
d6759abdc1 is described below
commit d6759abdc1141c601e067d1a3d4f7706cfa19dff
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Mar 1 06:46:33 2023 +0100
CAUSEWAY-3304: Blob/Clob: adds reading from DataSource
---
api/applib/src/main/java/module-info.java | 2 +-
.../org/apache/causeway/applib/value/Blob.java | 27 ++++++++++++---------
.../org/apache/causeway/applib/value/Clob.java | 28 +++++++++++++---------
3 files changed, 34 insertions(+), 23 deletions(-)
diff --git a/api/applib/src/main/java/module-info.java b/api/applib/src/main/java/module-info.java
index e1af9f8a4a..216d3cdcfc 100644
--- a/api/applib/src/main/java/module-info.java
+++ b/api/applib/src/main/java/module-info.java
@@ -134,7 +134,7 @@ module org.apache.causeway.applib {
requires transitive java.xml;
requires transitive java.inject;
requires lombok;
- requires org.apache.causeway.commons;
+ requires transitive org.apache.causeway.commons;
requires transitive org.apache.causeway.schema;
requires org.apache.logging.log4j;
requires transitive org.joda.time;
diff --git a/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java b/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java
index 8779bdca27..085748d03e 100644
--- a/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java
+++ b/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java
@@ -21,7 +21,6 @@ package org.apache.causeway.applib.value;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -51,6 +50,7 @@ import org.apache.causeway.commons.internal.base._Bytes;
import org.apache.causeway.commons.internal.base._Strings;
import org.apache.causeway.commons.internal.exceptions._Exceptions;
import org.apache.causeway.commons.internal.image._Images;
+import org.apache.causeway.commons.io.DataSource;
import lombok.NonNull;
import lombok.SneakyThrows;
@@ -103,7 +103,7 @@ public final class Blob implements NamedWithMimeType {
/**
* Returns a new {@link Blob} of given {@code name}, {@code mimeType} and {@code content}.
* <p>
- * {@code name} may or may not include the desired filename extension, it
+ * {@code name} may or may not include the desired filename extension, as it
* is guaranteed, that the resulting {@link Blob} has the appropriate extension
* as constraint by the given {@code mimeType}.
* <p>
@@ -120,25 +120,30 @@ public final class Blob implements NamedWithMimeType {
}
/**
- * Returns a new {@link Blob} of given {@code name}, {@code mimeType} and content from {@code file},
+ * Returns a new {@link Blob} of given {@code name}, {@code mimeType} and content from {@code dataSource},
* wrapped with a {@link Try}.
* <p>
- * {@code name} may or may not include the desired filename extension, it
+ * {@code name} may or may not include the desired filename extension, as it
* is guaranteed, that the resulting {@link Blob} has the appropriate extension
* as constraint by the given {@code mimeType}.
* <p>
- * For more fine-grained control use one of the {@link Blob} constructors directly.
+ * For more fine-grained control use one of the {@link Blob} factories directly.
* @param name - may or may not include the desired filename extension
* @param mimeType
- * @param file - the file to be opened for reading
+ * @param dataSource - the {@link DataSource} to be opened for reading
* @return new {@link Blob}
*/
+ public static Try<Blob> tryRead(final String name, final CommonMimeType mimeType, final DataSource dataSource) {
+ return dataSource.tryReadAsBytes()
+ .mapSuccess(bytes->Blob.of(name, mimeType, bytes.orElse(null)));
+ }
+
+ /**
+ * Shortcut for {@code tryRead(name, mimeType, DataSource.ofFile(file))}
+ * @see #tryRead(String, org.apache.causeway.applib.value.NamedWithMimeType.CommonMimeType, DataSource)
+ */
public static Try<Blob> tryRead(final String name, final CommonMimeType mimeType, final File file) {
- return Try.call(()->{
- try(val fis = new FileInputStream(file)){
- return Blob.of(name, mimeType, _Bytes.ofKeepOpen(fis));
- }
- });
+ return tryRead(name, mimeType, DataSource.ofFile(file));
}
// --
diff --git a/api/applib/src/main/java/org/apache/causeway/applib/value/Clob.java b/api/applib/src/main/java/org/apache/causeway/applib/value/Clob.java
index 2861e84194..8128ff58c3 100644
--- a/api/applib/src/main/java/org/apache/causeway/applib/value/Clob.java
+++ b/api/applib/src/main/java/org/apache/causeway/applib/value/Clob.java
@@ -19,7 +19,6 @@
package org.apache.causeway.applib.value;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
@@ -42,6 +41,7 @@ import org.apache.causeway.applib.annotation.Value;
import org.apache.causeway.applib.jaxb.PrimitiveJaxbAdapters;
import org.apache.causeway.commons.functional.Try;
import org.apache.causeway.commons.internal.base._Strings;
+import org.apache.causeway.commons.io.DataSource;
import lombok.NonNull;
import lombok.SneakyThrows;
@@ -98,7 +98,7 @@ public final class Clob implements NamedWithMimeType {
/**
* Returns a new {@link Clob} of given {@code name}, {@code mimeType} and {@code content}.
* <p>
- * {@code name} may or may not include the desired filename extension, it
+ * {@code name} may or may not include the desired filename extension, as it
* is guaranteed, that the resulting {@link Clob} has the appropriate extension
* as constraint by the given {@code mimeType}.
* <p>
@@ -115,27 +115,33 @@ public final class Clob implements NamedWithMimeType {
}
/**
- * Returns a new {@link Clob} of given {@code name}, {@code mimeType} and content from {@code file},
+ * Returns a new {@link Clob} of given {@code name}, {@code mimeType} and content from {@code dataSource},
* wrapped with a {@link Try}.
* <p>
- * {@code name} may or may not include the desired filename extension, it
+ * {@code name} may or may not include the desired filename extension, as it
* is guaranteed, that the resulting {@link Clob} has the appropriate extension
* as constraint by the given {@code mimeType}.
* <p>
* For more fine-grained control use one of the {@link Clob} constructors directly.
* @param name - may or may not include the desired filename extension
* @param mimeType
- * @param file - the file to be opened for reading
- * @param charset - {@link Charset} to use for reading given file
+ * @param dataSource - the {@link DataSource} to be opened for reading
+ * @param charset - {@link Charset} to use for reading from given {@link DataSource}
* @return new {@link Clob}
*/
+ public static Try<Clob> tryRead(final String name, final CommonMimeType mimeType, final DataSource dataSource,
+ final @NonNull Charset charset) {
+ return dataSource.tryReadAsString(charset)
+ .mapSuccess(string->Clob.of(name, mimeType, string.orElse(null)));
+ }
+
+ /**
+ * Shortcut for {@code tryRead(name, mimeType, DataSource.ofFile(file), charset)}
+ * @see #tryRead(String, org.apache.causeway.applib.value.NamedWithMimeType.CommonMimeType, DataSource, Charset)
+ */
public static Try<Clob> tryRead(final String name, final CommonMimeType mimeType, final File file,
final @NonNull Charset charset) {
- return Try.call(()->{
- try(val fis = new FileInputStream(file)){
- return Clob.of(name, mimeType, _Strings.read(fis, charset));
- }
- });
+ return tryRead(name, mimeType, DataSource.ofFile(file), charset);
}
/**