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 {
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;
}
}
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
......
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