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 6b1a429b6155b4d35b1f6df8bad92e76d0c5e693..c3383c03ad939451ac6360f9ce9bfd015141b13a 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 967fe29bfd88efc74d53dbe592f037c0e87c4d96..19693213cea0ba64258bf6ea883e50e9c124cd19 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