From d6e94912a4906101203570f177ca56a32e19ead6 Mon Sep 17 00:00:00 2001 From: LSmyrnaios Date: Thu, 26 May 2022 15:24:36 +0300 Subject: [PATCH] - Optimize zip-file creation. - Update dependencies. --- build.gradle | 4 ++-- .../urls_worker/util/FilesZipper.java | 19 ++++++++----------- .../openaire/urls_worker/util/UriBuilder.java | 1 - 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/build.gradle b/build.gradle index 9d1c903..5be54fa 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'org.springframework.boot' version '2.6.6' + id 'org.springframework.boot' version '2.7.0' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } @@ -26,7 +26,7 @@ dependencies { implementation("org.springframework.security:spring-security-config") //implementation("io.jsonwebtoken:jjwt:0.9.1") // Use this in case we use auth-tokens later on. - implementation "org.projectlombok:lombok:1.18.22" + implementation 'org.projectlombok:lombok:1.18.24' // Enable the validation annotations. //implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final' diff --git a/src/main/java/eu/openaire/urls_worker/util/FilesZipper.java b/src/main/java/eu/openaire/urls_worker/util/FilesZipper.java index aaa9e26..fda1251 100644 --- a/src/main/java/eu/openaire/urls_worker/util/FilesZipper.java +++ b/src/main/java/eu/openaire/urls_worker/util/FilesZipper.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import java.io.*; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -22,7 +23,7 @@ public class FilesZipper int numZippedFiles = 0; File zipFile = new File(zipFilename); - try ( ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile), StandardCharsets.UTF_8) ) + try ( ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()), StandardCharsets.UTF_8) ) { for ( String file : filesToZip ) { if ( zipAFile(file, zos, baseDirectory) ) @@ -37,20 +38,16 @@ public class FilesZipper } - private static final int BUFFER_SIZE = 3145728; // 3MB (average fullText-size) - private static final byte[] dataBuffer = new byte[BUFFER_SIZE]; - - // This method is "synchronized" to avoid any future problems with shared-buffer, if the requests are asynchronous. - private static synchronized boolean zipAFile(String fileName, ZipOutputStream zos, String baseDir) + private static boolean zipAFile(String fileName, ZipOutputStream zos, String baseDir) { - boolean shouldCloseEntry = false; // Useful in order to close the entry in case of an exception. + boolean shouldCloseEntry = false; // Useful in order to know if we should close the entry (an Exception may appear, and so we should not try to close it). String fullFileName = baseDir + fileName; - try ( BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fullFileName), BUFFER_SIZE) ) { + try (FileInputStream fis = new FileInputStream(fullFileName)) { zos.putNextEntry(new ZipEntry(fileName)); shouldCloseEntry = true; - int count; - while ( (count = bis.read(dataBuffer, 0, BUFFER_SIZE)) != -1 ) { - zos.write(dataBuffer, 0, count); + int readByte; + while ( (readByte = fis.read()) != -1 ) { + zos.write(readByte); } } catch (FileNotFoundException fnfe) { logger.error("Error zipping file: " + fullFileName, fnfe.getMessage()); diff --git a/src/main/java/eu/openaire/urls_worker/util/UriBuilder.java b/src/main/java/eu/openaire/urls_worker/util/UriBuilder.java index be87080..3ffa9bd 100644 --- a/src/main/java/eu/openaire/urls_worker/util/UriBuilder.java +++ b/src/main/java/eu/openaire/urls_worker/util/UriBuilder.java @@ -26,7 +26,6 @@ public class UriBuilder { sslEnabled = "false"; } baseUrl += sslEnabled.equals("true") ? "s" : ""; - baseUrl += "://"; String hostName = getPublicIP();