Commit 31fcb511 authored by Riegel's avatar Riegel
Browse files

Refactor: Streamline CityGmlZipEntry filesize calculation

Showing with 33 additions and 28 deletions
+33 -28
...@@ -68,25 +68,26 @@ public class CityGmlZipEntry implements Serializable { ...@@ -68,25 +68,26 @@ public class CityGmlZipEntry implements Serializable {
private boolean entrySizeWithinMemoryLimits() throws IOException { private boolean entrySizeWithinMemoryLimits() throws IOException {
long memoryLimit = (long) Math.ceil(((double) Runtime.getRuntime().maxMemory() / MB)*0.9); long memoryLimit = (long) Math.ceil(((double) Runtime.getRuntime().maxMemory() / MB)*0.9);
if (fileSize != -1L) { if (fileSize == -1L) {
return memoryLimit > fileSize; try (CityGmlZipEntryFile entryFile = new CityGmlZipEntryFile(this)){
} long filesize = entryFile.getFileSize();
try (CityGmlZipEntryFile entryFile = new CityGmlZipEntryFile(this)){ if (filesize != -1){
long filesize = entryFile.getFileSize(); this.fileSize = filesize;
if (filesize != -1){ } else {
this.fileSize = filesize; return false;
return memoryLimit > fileSize; }
} else { } catch (Exception e) {
return false; throw new IOException(e);
} }
} catch (Exception e) {
throw new IOException(e);
} }
return memoryLimit > fileSize;
} }
protected CityGmlZipEntry(ZipEntry entry,CityGmlZipArchive parentArchive, boolean decompressed) { protected CityGmlZipEntry(ZipEntry entry,CityGmlZipArchive parentArchive, boolean decompressed) {
this.fileName = entry.getName(); this.fileName = entry.getName();
if (entry.getSize() != -1){
this.fileSize = entry.getSize();
}
this.model = null; this.model = null;
this.decompressed = decompressed; this.decompressed = decompressed;
this.parentArchive = parentArchive; this.parentArchive = parentArchive;
...@@ -113,12 +114,11 @@ public class CityGmlZipEntry implements Serializable { ...@@ -113,12 +114,11 @@ public class CityGmlZipEntry implements Serializable {
return model; return model;
} }
public long getFileSize() throws IOException { public void setFileSize(long size) {
if (fileSize == -1L){ fileSize = size;
try(CityGmlZipInputStream cgis = new CityGmlZipInputStream(this)){ }
fileSize = cgis.getFileSize();
} public long getFileSize(){
}
return fileSize; return fileSize;
} }
} }
package de.hft.stuttgart.citydoctor2.zip; package de.hft.stuttgart.citydoctor2.zip;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
...@@ -10,6 +9,7 @@ public class CityGmlZipEntryFile implements AutoCloseable { ...@@ -10,6 +9,7 @@ public class CityGmlZipEntryFile implements AutoCloseable {
private final ZipFile zip; private final ZipFile zip;
private final ZipEntry zipEntry; private final ZipEntry zipEntry;
private final CityGmlZipEntry cgmlZipEntry;
private boolean closed = false; private boolean closed = false;
private static final long MB = 1024 * 1024L; private static final long MB = 1024 * 1024L;
...@@ -17,6 +17,7 @@ public class CityGmlZipEntryFile implements AutoCloseable { ...@@ -17,6 +17,7 @@ public class CityGmlZipEntryFile implements AutoCloseable {
CityGmlZipArchive archive = entry.getArchive(); CityGmlZipArchive archive = entry.getArchive();
zip = new ZipFile(archive.getArchivePath().toFile()); zip = new ZipFile(archive.getArchivePath().toFile());
zipEntry = zip.getEntry(entry.getFileName()); zipEntry = zip.getEntry(entry.getFileName());
this.cgmlZipEntry = entry;
} }
public InputStream getInputStream() throws IOException { public InputStream getInputStream() throws IOException {
...@@ -30,15 +31,19 @@ public class CityGmlZipEntryFile implements AutoCloseable { ...@@ -30,15 +31,19 @@ public class CityGmlZipEntryFile implements AutoCloseable {
if (closed){ if (closed){
throw new IOException("Stream closed"); throw new IOException("Stream closed");
} }
if (zipEntry.getSize() != -1){ if (cgmlZipEntry.getFileSize() == -1L) {
return (long) Math.ceil((double) zipEntry.getSize() / MB); 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; return cgmlZipEntry.getFileSize();
InputStream is = this.getInputStream();
for(int i = is.read(); i != -1; i=is.read()) {
bytes++;
}
return (long) Math.ceil((double) bytes / MB);
} }
@Override @Override
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment