diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java index 71beb86f0e..16a8a8ed13 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java @@ -34,7 +34,7 @@ import javax.inject.Named; import javax.inject.Provider; import com.google.common.base.Throwables; -import com.google.common.io.Closeables; +import com.google.common.io.ByteStreams; import com.google.common.io.Files; import org.apache.commons.io.FileUtils; @@ -201,14 +201,13 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy { filesystemContainerNameValidator.validate(containerName); filesystemBlobKeyValidator.validate(blobKey); File outputFile = getFileForBlobKey(containerName, blobKey); - FileOutputStream output = null; try { Files.createParentDirs(outputFile); if (payload.getRawContent() instanceof File) Files.copy((File) payload.getRawContent(), outputFile); else { - output = new FileOutputStream(outputFile); - payload.writeTo(output); + payload = Payloads.newPayload(ByteStreams.toByteArray(payload)); + Files.copy(payload, outputFile); } Payloads.calculateMD5(payload, crypto.md5()); String eTag = CryptoStreams.hex(payload.getContentMetadata().getContentMD5()); @@ -219,7 +218,6 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy { } throw ex; } finally { - Closeables.closeQuietly(output); payload.release(); } } diff --git a/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java b/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java index 583b64481c..8f83d35779 100644 --- a/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java +++ b/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java @@ -44,6 +44,7 @@ import org.jclouds.filesystem.predicates.validators.internal.FilesystemBlobKeyVa import org.jclouds.filesystem.predicates.validators.internal.FilesystemContainerNameValidatorImpl; import org.jclouds.filesystem.utils.TestUtils; import org.jclouds.io.payloads.FilePayload; +import org.jclouds.io.payloads.InputStreamPayload; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -347,17 +348,36 @@ public class FilesystemStorageStrategyImplTest { } public void testWritePayloadOnFile() throws IOException { - String blobKey; - File sourceFile; - FilePayload filePayload; - - blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img"); - sourceFile = TestUtils.getImageForBlobPayload(); - filePayload = new FilePayload(sourceFile); + String blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img"); + File sourceFile = TestUtils.getImageForBlobPayload(); + FilePayload filePayload = new FilePayload(sourceFile); Blob blob = storageStrategy.newBlob(blobKey); blob.setPayload(filePayload); + // write files storageStrategy.putBlob(CONTAINER_NAME, blob); + + // verify that the files is equal + File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey); + InputSupplier expectedInput = + Files.newInputStreamSupplier(sourceFile); + InputSupplier actualInput = + Files.newInputStreamSupplier(blobFullPath); + assertTrue(ByteStreams.equal(expectedInput, actualInput), + "Files are not equal"); + } + + public void testWritePayloadOnFileInputStream() throws IOException { + String blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img"); + File sourceFile = TestUtils.getImageForBlobPayload(); + InputStreamPayload fileInputStreamPayload = new InputStreamPayload( + new FileInputStream(sourceFile)); + Blob blob = storageStrategy.newBlob(blobKey); + blob.setPayload(fileInputStreamPayload); + + // write files + storageStrategy.putBlob(CONTAINER_NAME, blob); + // verify that the files is equal File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey); InputSupplier expectedInput =