This is an automated email from the ASF dual-hosted git repository.
fortino pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
The following commit(s) were added to refs/heads/trunk by this push:
new f8aeae7c3a OAK-10182: use streams to avoid buffer positioning issues
leading to corrupted files (#893)
f8aeae7c3a is described below
commit f8aeae7c3a360cbe8bd509e1268838a9a06f851d
Author: Fabrizio Fortino <[email protected]>
AuthorDate: Tue Apr 11 14:42:12 2023 +0200
OAK-10182: use streams to avoid buffer positioning issues leading to
corrupted files (#893)
* OAK-10167: fix error in ElasticIndexAggregationNtFileTest
* OAK-10182: use streams to avoid buffer positioning issues leading to
corrupted files
---
.../org/apache/jackrabbit/oak/run/Downloader.java | 22 +++++++++-------------
.../oak/run/DataStoreCopyCommandTest.java | 5 +++++
2 files changed, 14 insertions(+), 13 deletions(-)
diff --git
a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Downloader.java
b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Downloader.java
index be8e4bebc2..2a70e18d9f 100644
--- a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Downloader.java
+++ b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Downloader.java
@@ -24,17 +24,14 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Closeable;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
-import java.nio.ByteBuffer;
-import java.nio.channels.Channels;
-import java.nio.channels.FileChannel;
-import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
@@ -175,19 +172,18 @@ public class Downloader implements Closeable {
Path destinationPath = Paths.get(item.destination);
Files.createDirectories(destinationPath.getParent());
+
long size = 0;
- try (ReadableByteChannel byteChannel =
Channels.newChannel(sourceUrl.getInputStream());
- FileChannel outputChannel = FileChannel.open(destinationPath,
StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
- ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
+ try (InputStream inputStream = sourceUrl.getInputStream();
+ FileOutputStream outputStream = new
FileOutputStream(destinationPath.toFile())) {
+ byte[] buffer = new byte[bufferSize];
int bytesRead;
- while ((bytesRead = byteChannel.read(buffer)) != -1) {
- buffer.flip();
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
if (md != null) {
- md.update(buffer);
+ md.update(buffer, 0, bytesRead);
}
+ outputStream.write(buffer, 0, bytesRead);
size += bytesRead;
- outputChannel.write(buffer);
- buffer.clear();
}
}
diff --git
a/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCopyCommandTest.java
b/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCopyCommandTest.java
index 72efac276a..82943c55f9 100644
---
a/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCopyCommandTest.java
+++
b/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCopyCommandTest.java
@@ -274,6 +274,11 @@ public class DataStoreCopyCommandTest {
Path.of(cmd.getDestinationFromId(BLOB2)).getFileName().toString()),
files.map(f ->
f.getFileName().toString()).collect(Collectors.toSet()));
}
+
+ assertEquals(BLOBS_WITH_CONTENT.get(BLOB1),
+
IOUtils.toString(Path.of(cmd.getDestinationFromId(BLOB1)).toUri(),
StandardCharsets.UTF_8));
+ assertEquals(BLOBS_WITH_CONTENT.get(BLOB2),
+
IOUtils.toString(Path.of(cmd.getDestinationFromId(BLOB2)).toUri(),
StandardCharsets.UTF_8));
}
private CloudBlobContainer createBlobContainer() throws Exception {