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

fanningpj pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko-connectors.git


The following commit(s) were added to refs/heads/main by this push:
     new b1e6e1d6b zip: use ByteString conversions because we know they are 
safe in this case (#657)
b1e6e1d6b is described below

commit b1e6e1d6b8f3ba77d2b606a7b7ef3ec90d521d42
Author: PJ Fanning <[email protected]>
AuthorDate: Sat May 18 18:04:26 2024 +0100

    zip: use ByteString conversions because we know they are safe in this case 
(#657)
---
 .../pekko/stream/connectors/file/impl/archive/ZipArchiveFlow.scala  | 3 +--
 file/src/test/java/docs/javadsl/ArchiveHelper.java                  | 6 ++++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git 
a/file/src/main/scala/org/apache/pekko/stream/connectors/file/impl/archive/ZipArchiveFlow.scala
 
b/file/src/main/scala/org/apache/pekko/stream/connectors/file/impl/archive/ZipArchiveFlow.scala
index 0e5077fde..41abf75ad 100644
--- 
a/file/src/main/scala/org/apache/pekko/stream/connectors/file/impl/archive/ZipArchiveFlow.scala
+++ 
b/file/src/main/scala/org/apache/pekko/stream/connectors/file/impl/archive/ZipArchiveFlow.scala
@@ -60,8 +60,7 @@ import pekko.util.{ ByteString, ByteStringBuilder }
           case b: ByteString if FileByteStringSeparators.isEndingByteString(b) 
=>
             zip.closeEntry()
           case b: ByteString =>
-            val array = b.toArray
-            zip.write(array, 0, array.length)
+            zip.write(b.toArrayUnsafe())
         }
         zip.flush()
         val result = builder.result()
diff --git a/file/src/test/java/docs/javadsl/ArchiveHelper.java 
b/file/src/test/java/docs/javadsl/ArchiveHelper.java
index 7ccb9ebe1..d3f6cacb8 100644
--- a/file/src/test/java/docs/javadsl/ArchiveHelper.java
+++ b/file/src/test/java/docs/javadsl/ArchiveHelper.java
@@ -25,7 +25,8 @@ import java.util.zip.ZipInputStream;
 public class ArchiveHelper {
 
   public Map<String, ByteString> unzip(ByteString zipArchive) throws Exception 
{
-    ZipInputStream zis = new ZipInputStream(new 
ByteArrayInputStream(zipArchive.toArray()));
+    // toArrayUnsafe is ok here because we know that ZipInputStream will not 
mutate the array
+    ZipInputStream zis = new ZipInputStream(new 
ByteArrayInputStream(zipArchive.toArrayUnsafe()));
     ZipEntry entry;
     Map<String, ByteString> result = new HashMap<>();
     try {
@@ -40,7 +41,8 @@ public class ArchiveHelper {
         dest.flush();
         dest.close();
         zis.closeEntry();
-        result.putIfAbsent(entry.getName(), 
ByteString.fromArray(dest.toByteArray()));
+        // fromArrayUnsafe is ok here because we know the `dest` data is not 
mutated
+        result.putIfAbsent(entry.getName(), 
ByteString.fromArrayUnsafe(dest.toByteArray()));
       }
     } finally {
       zis.close();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to