uowfile library: prefer nio over core/io
Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/94f7cca8 Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/94f7cca8 Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/94f7cca8 Branch: refs/heads/develop Commit: 94f7cca8ed0eefd9abdce1beea14f52dd40f24de Parents: 95c1b24 Author: Paul Merlin <[email protected]> Authored: Mon Nov 28 09:53:57 2016 +0100 Committer: Paul Merlin <[email protected]> Committed: Mon Nov 28 09:53:57 2016 +0100 ---------------------------------------------------------------------- .../zest/library/uowfile/internal/UoWFile.java | 21 ++++---- .../zest/library/uowfile/HasUoWFileTest.java | 50 ++++++++++++-------- .../zest/library/uowfile/HasUoWFilesTest.java | 40 ++++++++++------ 3 files changed, 66 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/94f7cca8/libraries/uowfile/src/main/java/org/apache/zest/library/uowfile/internal/UoWFile.java ---------------------------------------------------------------------- diff --git a/libraries/uowfile/src/main/java/org/apache/zest/library/uowfile/internal/UoWFile.java b/libraries/uowfile/src/main/java/org/apache/zest/library/uowfile/internal/UoWFile.java index 0785d30..1228d4b 100644 --- a/libraries/uowfile/src/main/java/org/apache/zest/library/uowfile/internal/UoWFile.java +++ b/libraries/uowfile/src/main/java/org/apache/zest/library/uowfile/internal/UoWFile.java @@ -21,16 +21,14 @@ package org.apache.zest.library.uowfile.internal; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.concurrent.atomic.AtomicLong; -import org.apache.zest.io.Inputs; -import org.apache.zest.io.Outputs; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class UoWFile { /* package */ static final Logger LOGGER = LoggerFactory.getLogger( "org.apache.zest.library.uowfile" ); - private static final int FILE_BUFFER_SIZE = 4096; private static final AtomicLong COUNT = new AtomicLong( 0L ); private final long originalIdentity; private final File original; @@ -56,11 +54,11 @@ public class UoWFile { StringBuilder sb = new StringBuilder().append( UoWFile.class.getSimpleName() ); // UoWFile{parent/( original(oid->id) | current(id) | backup(id) )} - sb.append( "{" ).append( original.getParentFile().getName() ).append( "/( " ). - append( original.getName() ).append( "(" ).append( originalIdentity ).append( "->" ).append( fileTag( original ) ).append( ") | " ). - append( current.getName() ).append( "(" ).append( fileTag( current ) ).append( ") | " ). - append( backup.getName() ).append( "(" ).append( fileTag( backup ) ). - append( ") )}" ); + sb.append( "{" ).append( original.getParentFile().getName() ).append( "/( " ) + .append( original.getName() ).append( "(" ).append( originalIdentity ).append( "->" ) + .append( fileTag( original ) ).append( ") | " ).append( current.getName() ) + .append( "(" ).append( fileTag( current ) ).append( ") | " ).append( backup.getName() ) + .append( "(" ).append( fileTag( backup ) ).append( ") )}" ); return sb.toString(); } @@ -80,7 +78,9 @@ public class UoWFile { if( fileTag( original ) != originalIdentity ) { - LOGGER.info( "Concurrent modification, original creation reference is {} and original apply reference is {}", originalIdentity, fileTag( original ) ); + LOGGER.info( + "Concurrent modification, original creation reference is {} and original apply reference is {}", + originalIdentity, fileTag( original ) ); throw new ConcurrentUoWFileStateModificationException( this ); } if( original.exists() ) @@ -132,7 +132,7 @@ public class UoWFile { try { - Inputs.byteBuffer( source, FILE_BUFFER_SIZE ).transferTo( Outputs.byteBuffer( dest ) ); + Files.copy( source.toPath(), dest.toPath() ); } catch( IOException ex ) { @@ -165,5 +165,4 @@ public class UoWFile + "Are they on different filesystems?", source, dest ); } } - } http://git-wip-us.apache.org/repos/asf/zest-java/blob/94f7cca8/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFileTest.java ---------------------------------------------------------------------- diff --git a/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFileTest.java b/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFileTest.java index 8a53c0a..2796f83 100644 --- a/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFileTest.java +++ b/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFileTest.java @@ -21,6 +21,7 @@ package org.apache.zest.library.uowfile; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.util.ArrayList; @@ -42,8 +43,6 @@ import org.apache.zest.api.unitofwork.concern.UnitOfWorkPropagation; import org.apache.zest.api.unitofwork.concern.UnitOfWorkRetry; import org.apache.zest.bootstrap.AssemblyException; import org.apache.zest.bootstrap.ModuleAssembly; -import org.apache.zest.io.Inputs; -import org.apache.zest.io.Outputs; import org.apache.zest.library.fileconfig.FileConfigurationAssembler; import org.apache.zest.library.uowfile.bootstrap.UoWFileAssembler; import org.apache.zest.library.uowfile.internal.ConcurrentUoWFileModificationException; @@ -59,6 +58,7 @@ import org.junit.rules.TemporaryFolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; @@ -171,9 +171,11 @@ public class HasUoWFileTest File attachedFile = entity.attachedFile(); File managedFile = entity.managedFile(); // END SNIPPET: api - Inputs.text( MODIFICATION_CONTENT_URL ).transferTo( Outputs.text( managedFile ) ); + try( InputStream input = MODIFICATION_CONTENT_URL.openStream() ) + { + Files.copy( input, managedFile.toPath(), REPLACE_EXISTING ); + } } - } @Override @@ -225,9 +227,9 @@ public class HasUoWFileTest } try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) { - assertThat("File content was not the good one", - lines.limit( 1 ).findFirst().get(), - equalTo( "Creation" ) ); + assertThat( "File content was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Creation" ) ); } } @@ -255,9 +257,9 @@ public class HasUoWFileTest } try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) { - assertThat("File content after discarded modification was not the good one", - lines.limit( 1 ).findFirst().get(), - equalTo( "Creation" ) ); + assertThat( "File content after discarded modification was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Creation" ) ); } // Testing completed modification @@ -268,9 +270,9 @@ public class HasUoWFileTest } try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) { - assertThat("Modified file content was not the good one", - lines.limit( 1 ).findFirst().get(), - equalTo( "Modification" ) ); + assertThat( "Modified file content was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Modification" ) ); } } @@ -330,11 +332,17 @@ public class HasUoWFileTest uow = unitOfWorkFactory.newUnitOfWork(); entity = uow.get( TestedEntity.class, entityId ); - Inputs.text( MODIFICATION_CONTENT_URL ).transferTo( Outputs.text( entity.managedFile() ) ); + try( InputStream input = MODIFICATION_CONTENT_URL.openStream() ) + { + Files.copy( input, entity.managedFile().toPath(), REPLACE_EXISTING ); + } uow2 = unitOfWorkFactory.newUnitOfWork(); entity = uow2.get( TestedEntity.class, entityId ); - Inputs.text( MODIFICATION_CONTENT_URL ).transferTo( Outputs.text( entity.managedFile() ) ); + try( InputStream input = MODIFICATION_CONTENT_URL.openStream() ) + { + Files.copy( input, entity.managedFile().toPath(), REPLACE_EXISTING ); + } uow.complete(); try @@ -403,9 +411,9 @@ public class HasUoWFileTest assertTrue( "There were errors during TestRetry", ex.isEmpty() ); try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) { - assertThat("Modified file content was not the good one", - lines.limit( 1 ).findFirst().get(), - equalTo( "Modification" ) ); + assertThat( "Modified file content was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Modification" ) ); } } @@ -416,8 +424,10 @@ public class HasUoWFileTest TestedEntity entity = builder.instance(); entity.name().set( name ); entity = builder.newInstance(); - Inputs.text( CREATION_CONTENT_URL ).transferTo( Outputs.text( entity.managedFile() ) ); + try( InputStream input = CREATION_CONTENT_URL.openStream() ) + { + Files.copy( input, entity.managedFile().toPath() ); + } return entity; } - } http://git-wip-us.apache.org/repos/asf/zest-java/blob/94f7cca8/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFilesTest.java ---------------------------------------------------------------------- diff --git a/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFilesTest.java b/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFilesTest.java index 4cdb275..8630de9 100644 --- a/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFilesTest.java +++ b/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFilesTest.java @@ -21,44 +21,44 @@ package org.apache.zest.library.uowfile; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; -import org.apache.zest.api.identity.HasIdentity; -import org.apache.zest.api.identity.Identity; -import org.apache.zest.api.unitofwork.UnitOfWorkFactory; -import org.apache.zest.spi.ZestSPI; -import org.apache.zest.test.AbstractZestTest; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; import org.apache.zest.api.concern.Concerns; import org.apache.zest.api.entity.EntityBuilder; +import org.apache.zest.api.identity.HasIdentity; +import org.apache.zest.api.identity.Identity; import org.apache.zest.api.injection.scope.Structure; import org.apache.zest.api.injection.scope.This; import org.apache.zest.api.mixin.Mixins; import org.apache.zest.api.property.Property; import org.apache.zest.api.unitofwork.UnitOfWork; import org.apache.zest.api.unitofwork.UnitOfWorkCompletionException; +import org.apache.zest.api.unitofwork.UnitOfWorkFactory; import org.apache.zest.api.unitofwork.concern.UnitOfWorkConcern; import org.apache.zest.api.unitofwork.concern.UnitOfWorkPropagation; import org.apache.zest.api.unitofwork.concern.UnitOfWorkRetry; import org.apache.zest.bootstrap.AssemblyException; import org.apache.zest.bootstrap.ModuleAssembly; -import org.apache.zest.io.Inputs; -import org.apache.zest.io.Outputs; import org.apache.zest.library.fileconfig.FileConfigurationAssembler; import org.apache.zest.library.uowfile.bootstrap.UoWFileAssembler; import org.apache.zest.library.uowfile.internal.ConcurrentUoWFileModificationException; import org.apache.zest.library.uowfile.plural.HasUoWFilesLifecycle; import org.apache.zest.library.uowfile.plural.UoWFilesLocator; +import org.apache.zest.spi.ZestSPI; +import org.apache.zest.test.AbstractZestTest; import org.apache.zest.test.EntityTestAssembler; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; @@ -188,7 +188,10 @@ public class HasUoWFilesTest File attachedFileTwo = entity.attachedFile( MyEnum.fileTwo ); File managedFileOne = entity.managedFile( MyEnum.fileOne ); // END SNIPPET: api - Inputs.text( MODIFICATION_CONTENT_URL ).transferTo( Outputs.text( managedFileOne ) ); + try( InputStream input = MODIFICATION_CONTENT_URL.openStream() ) + { + Files.copy( input, managedFileOne.toPath(), REPLACE_EXISTING ); + } } } @@ -346,11 +349,17 @@ public class HasUoWFilesTest uow = unitOfWorkFactory.newUnitOfWork(); entity = uow.get( TestedEntity.class, entityId ); - Inputs.text( MODIFICATION_CONTENT_URL ).transferTo( Outputs.text( entity.managedFile( MyEnum.fileOne ) ) ); + try( InputStream input = MODIFICATION_CONTENT_URL.openStream() ) + { + Files.copy( input, entity.managedFile( MyEnum.fileOne ).toPath(), REPLACE_EXISTING ); + } uow2 = unitOfWorkFactory.newUnitOfWork(); entity = uow2.get( TestedEntity.class, entityId ); - Inputs.text( MODIFICATION_CONTENT_URL ).transferTo( Outputs.text( entity.managedFile( MyEnum.fileOne ) ) ); + try( InputStream input = MODIFICATION_CONTENT_URL.openStream() ) + { + Files.copy( input, entity.managedFile( MyEnum.fileOne ).toPath(), REPLACE_EXISTING ); + } uow.complete(); try @@ -432,7 +441,10 @@ public class HasUoWFilesTest TestedEntity entity = builder.instance(); entity.name().set( name ); entity = builder.newInstance(); - Inputs.text( CREATION_CONTENT_URL ).transferTo( Outputs.text( entity.managedFile( MyEnum.fileOne ) ) ); + try( InputStream input = CREATION_CONTENT_URL.openStream() ) + { + Files.copy( input, entity.managedFile( MyEnum.fileOne ).toPath() ); + } return entity; }
