From 31fcb5116b6b091227d2f9549f2566db493e85f0 Mon Sep 17 00:00:00 2001 From: Riegel <alexander.riegel@hft-stuttgart.de> Date: Thu, 12 Dec 2024 12:01:31 +0100 Subject: [PATCH] Refactor: Streamline CityGmlZipEntry filesize calculation --- .../citydoctor2/zip/CityGmlZipEntry.java | 38 +++++++++---------- .../citydoctor2/zip/CityGmlZipEntryFile.java | 23 ++++++----- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/zip/CityGmlZipEntry.java b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/zip/CityGmlZipEntry.java index 6b1a429..c3383c0 100644 --- a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/zip/CityGmlZipEntry.java +++ b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/zip/CityGmlZipEntry.java @@ -68,25 +68,26 @@ public class CityGmlZipEntry implements Serializable { private boolean entrySizeWithinMemoryLimits() throws IOException { long memoryLimit = (long) Math.ceil(((double) Runtime.getRuntime().maxMemory() / MB)*0.9); - if (fileSize != -1L) { - return memoryLimit > fileSize; - } - try (CityGmlZipEntryFile entryFile = new CityGmlZipEntryFile(this)){ - long filesize = entryFile.getFileSize(); - if (filesize != -1){ - this.fileSize = filesize; - return memoryLimit > fileSize; - } else { - return false; + if (fileSize == -1L) { + try (CityGmlZipEntryFile entryFile = new CityGmlZipEntryFile(this)){ + long filesize = entryFile.getFileSize(); + if (filesize != -1){ + this.fileSize = filesize; + } else { + return false; + } + } catch (Exception e) { + throw new IOException(e); } - } catch (Exception e) { - throw new IOException(e); } - + return memoryLimit > fileSize; } protected CityGmlZipEntry(ZipEntry entry,CityGmlZipArchive parentArchive, boolean decompressed) { this.fileName = entry.getName(); + if (entry.getSize() != -1){ + this.fileSize = entry.getSize(); + } this.model = null; this.decompressed = decompressed; this.parentArchive = parentArchive; @@ -113,12 +114,11 @@ public class CityGmlZipEntry implements Serializable { return model; } - public long getFileSize() throws IOException { - if (fileSize == -1L){ - try(CityGmlZipInputStream cgis = new CityGmlZipInputStream(this)){ - fileSize = cgis.getFileSize(); - } - } + public void setFileSize(long size) { + fileSize = size; + } + + public long getFileSize(){ return fileSize; } } diff --git a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/zip/CityGmlZipEntryFile.java b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/zip/CityGmlZipEntryFile.java index 967fe29..1969321 100644 --- a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/zip/CityGmlZipEntryFile.java +++ b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/zip/CityGmlZipEntryFile.java @@ -1,6 +1,5 @@ package de.hft.stuttgart.citydoctor2.zip; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; @@ -10,6 +9,7 @@ public class CityGmlZipEntryFile implements AutoCloseable { private final ZipFile zip; private final ZipEntry zipEntry; + private final CityGmlZipEntry cgmlZipEntry; private boolean closed = false; private static final long MB = 1024 * 1024L; @@ -17,6 +17,7 @@ public class CityGmlZipEntryFile implements AutoCloseable { CityGmlZipArchive archive = entry.getArchive(); zip = new ZipFile(archive.getArchivePath().toFile()); zipEntry = zip.getEntry(entry.getFileName()); + this.cgmlZipEntry = entry; } public InputStream getInputStream() throws IOException { @@ -30,15 +31,19 @@ public class CityGmlZipEntryFile implements AutoCloseable { if (closed){ throw new IOException("Stream closed"); } - if (zipEntry.getSize() != -1){ - return (long) Math.ceil((double) zipEntry.getSize() / MB); + if (cgmlZipEntry.getFileSize() == -1L) { + if (zipEntry.getSize() == -1) { + long bytes = 0; + InputStream is = this.getInputStream(); + for (int i = is.read(); i != -1; i = is.read()) { + bytes++; + } + cgmlZipEntry.setFileSize((long) Math.ceil((double) bytes / MB)); + } else { + cgmlZipEntry.setFileSize((long) Math.ceil((double) zipEntry.getSize() / MB)); + } } - long bytes = 0; - InputStream is = this.getInputStream(); - for(int i = is.read(); i != -1; i=is.read()) { - bytes++; - } - return (long) Math.ceil((double) bytes / MB); + return cgmlZipEntry.getFileSize(); } @Override -- GitLab