This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git


The following commit(s) were added to refs/heads/master by this push:
     new 4a508c68 Add ByteSource.path(Path)
4a508c68 is described below

commit 4a508c686082de1224bef66336f490e4483c9ddb
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Jul 4 09:21:03 2023 -0400

    Add ByteSource.path(Path)
---
 .../commons/imaging/bytesource/ByteSource.java       |  7 +++++++
 .../imaging/bytesource/ByteSourceDataTest.java       | 20 ++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git 
a/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java 
b/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java
index 640c04f6..f3e65796 100644
--- a/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java
+++ b/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java
@@ -19,11 +19,14 @@ package org.apache.commons.imaging.bytesource;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Path;
+import java.util.Objects;
 
 import org.apache.commons.imaging.common.BinaryFunctions;
 import org.apache.commons.io.build.AbstractOrigin;
 import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin;
 import org.apache.commons.io.build.AbstractOrigin.FileOrigin;
+import org.apache.commons.io.build.AbstractOrigin.PathOrigin;
 
 public class ByteSource {
 
@@ -39,6 +42,10 @@ public class ByteSource {
         return new ByteSource(new FileOrigin(file), file.getName());
     }
 
+    public static ByteSource path(final Path file) {
+        return new ByteSource(new PathOrigin(file), 
Objects.toString(file.getFileName(), null));
+    }
+
     public static ByteSource inputStream(final InputStream is, final String 
name) {
         return new InputStreamByteSource(is, name);
     }
diff --git 
a/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceDataTest.java 
b/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceDataTest.java
index e63fd4a9..e6153a76 100644
--- 
a/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceDataTest.java
+++ 
b/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceDataTest.java
@@ -26,6 +26,8 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.stream.Stream;
 
@@ -51,6 +53,18 @@ public class ByteSourceDataTest extends ByteSourceTest {
         }
     }
 
+    private class ByteSourcePathFactory implements ByteSourceFactory {
+        @Override
+        public ByteSource getByteSource(final byte[] src) throws IOException {
+            final Path file = createTempFile(src).toPath();
+
+            // test that all bytes written to file.
+            assertEquals(src.length, Files.size(file));
+
+            return ByteSource.path(file);
+        }
+    }
+
     private class ByteSourceInputStreamFileFactory implements 
ByteSourceFactory {
         @Override
         public ByteSource getByteSource(final byte[] src) throws IOException {
@@ -82,6 +96,12 @@ public class ByteSourceDataTest extends ByteSourceTest {
         writeAndReadBytes(new ByteSourceFileFactory(), testByteArray);
     }
 
+    @ParameterizedTest
+    @MethodSource("data")
+    public void testByteSourcePathFactory(final byte[] testByteArray) throws 
Exception {
+        writeAndReadBytes(new ByteSourcePathFactory(), testByteArray);
+    }
+
     @ParameterizedTest
     @MethodSource("data")
     public void testByteSourceInputStreamFileFactory(final byte[] 
testByteArray) throws Exception {

Reply via email to