diff --git a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java index 30acf8a6c6780fb0d041c299ca6baee3d128be23..bd646909440c6c136bb457791a703d4dbda764b6 100644 --- a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java +++ b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java @@ -171,13 +171,23 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { } private void copyStreamFast(InputStream is, OutputStream os) throws IOException{ - int blocksize = getBlockSize(); - assert(blocksize > 0); - byte[] buffer = new byte[ blocksize ]; - int n; - while ((n = is.read(buffer)) != -1) { - os.write(buffer,0,n); - } + if ((is instanceof FileInputStream) && (os instanceof FileOutputStream) ) { + /* use very fast java.nio.channels.FileChannel.transferFrom() function if both are Filestreams */ + FileInputStream fis = (FileInputStream) is; + FileOutputStream fos = (FileOutputStream) os; + FileChannel sourceChannel = fis.getChannel(); + FileChannel destChannel = fos.getChannel(); + destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); + } else { + /* otherwise use blockwise copy loop */ + int blocksize = getBlockSize(); + assert (blocksize > 0); + byte[] buffer = new byte[blocksize]; + int n; + while ((n = is.read(buffer)) != -1) { + os.write(buffer, 0, n); + } + } } @@ -350,7 +360,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { //log.debug("SLUBStoragePlugin.checkFixityByPlugin() pluginname=" + pluginname); // HINT: Workaround for Rosetta case 06472860 - Storage Plugin: Empty custom fixity plugin name breaks file updates [Rosetta 7.3.0.0] // -> use fallback names in case fixity plugin name is missing - if (pluginname == null || pluginname.length() == 0) { + if (pluginname == null || pluginname.isEmpty()) { log.warn("SLUBStoragePlugin.checkFixityByPlugin() failed to get pluginname, because it is empty. Possibly, there is no valid fixity type used or a CustomFixityPlugin missed"); log.warn("SLUBStoragePlugin.checkFixityByPlugin() trying to use fallback table to determine plugin name"); String algorithm = fixity.getAlgorithm(); @@ -451,7 +461,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { contractAssertIsRelativePath(storedEntityIdentifier); var absolute_filename = getFullFilePath(storedEntityIdentifier); var absolute_path = Paths.get(absolute_filename); - var is = java.nio.file.Files.newInputStream( absolute_path ); + var is = new FileInputStream( absolute_path.toString() ); return new BufferedInputStream( is , getBlockSize()); } @@ -532,13 +542,11 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { log.warn("SLUBStoragePlugin.storeEntity() InputStream is null"); return null; } - try (OutputStream output = java.nio.file.Files.newOutputStream( Paths.get( absoluteDestFilePath.filestring()))) - { - long starttime = System.currentTimeMillis(); - copyStreamFast(is, output); - long endtime = System.currentTimeMillis(); - log.info("SLUBStoragePlugin.storeEntity() try copy (copyStreamFast(is, '"+absoluteDestFilePath.filestring()+"') was successfull (" + throughput(starttime, endtime, absoluteDestFilePath)+ ")" ); - } + FileOutputStream output = new FileOutputStream(absoluteDestFilePath.filestring()); + long starttime = System.currentTimeMillis(); + copyStreamFast(is, output); + long endtime = System.currentTimeMillis(); + log.info("SLUBStoragePlugin.storeEntity() try copy (copyStreamFast(is, '" + absoluteDestFilePath.filestring() + "') was successfull (" + throughput(starttime, endtime, absoluteDestFilePath) + ")"); } if (!checkFixityAbs(storedEntityMetadata.getFixities(), absoluteDestFilePath)) { log.warn("SLUBStoragePlugin.storeEntity() called checkFixity(fixities, '"+absoluteDestFilePath+"') failed"); @@ -570,20 +578,16 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { */ private void copyFile(String source, AbsolutePath destination) throws IOException { long starttime = System.currentTimeMillis(); - if (!source.toLowerCase().trim().startsWith("http")) { /* simpleFilecopy, see https://www.digitalocean.com/community/tutorials/java-copy-file */ - try ( - FileChannel sourceChannel = new FileInputStream(source).getChannel(); - FileChannel destChannel = new FileOutputStream(destination.filestring()).getChannel() - ) { - destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); - } - } else { - InputStream input = FileTransferUtil.getUrlContent(source); - FileOutputStream output = new FileOutputStream(destination.filestring()); - copyStreamFast(input, output); - input.close(); - output.close(); - } + FileOutputStream output = new FileOutputStream(destination.filestring()); + InputStream input; + if (source.toLowerCase().trim().startsWith("http")) { + input = FileTransferUtil.getUrlContent(source); + } else { /* simpleFilecopy, see https://www.digitalocean.com/community/tutorials/java-copy-file */ + input = new FileInputStream(source); + } + copyStreamFast(input, output); + input.close(); + output.close(); long endtime = System.currentTimeMillis(); log.info("SLUBStoragePlugin.copyFile (" + source + ", " + destination.filestring() + ") was successfull (" + throughput(starttime, endtime, destination) + ")"); } @@ -764,7 +768,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { SimpleDateFormat sdf; sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setLenient(false); /* if parse errors, do not guess about */ - Date d = null; + Date d; try { d = sdf.parse(datestring); } catch (ParseException e) {