From a73358bd27ae6ccc5efaa58df24d6b6ef6b25f50 Mon Sep 17 00:00:00 2001 From: Kevin Ruland Date: Wed, 25 Jul 2012 18:35:25 +0000 Subject: [PATCH] Added isExportable which tests if a decal is currently available from the container file. Refactored the code some. Use the FileUtils readBytes[] method. --- .../sf/openrocket/document/DecalRegistry.java | 70 ++++++++++--------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/core/src/net/sf/openrocket/document/DecalRegistry.java b/core/src/net/sf/openrocket/document/DecalRegistry.java index ab919b81f..ed61f9bbe 100644 --- a/core/src/net/sf/openrocket/document/DecalRegistry.java +++ b/core/src/net/sf/openrocket/document/DecalRegistry.java @@ -1,8 +1,6 @@ package net.sf.openrocket.document; -import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -12,6 +10,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import net.sf.openrocket.file.FileInfo; +import net.sf.openrocket.util.FileUtils; public class DecalRegistry { @@ -30,6 +29,22 @@ public class DecalRegistry { this.isZipFile = isZipFile; } + public boolean isExportable( String name ) { + if ( !isZipFile ) { + return false; + } + try { + InputStream is = forwardToEntry(name); + if ( is != null ) { + is.close(); + return true; + } else { + return false; + } + } catch ( IOException iex ) { + return false; + } + } /** * This function returns an InputStream backed by a byte[] containing the decal pixels. * If it reads in the bytes from an actual file, the underlying file is closed. @@ -50,15 +65,7 @@ public class DecalRegistry { InputStream rawIs = null; if ( isZipFile ) { - ZipInputStream zis = new ZipInputStream(fileInfo.fileURL.openStream()); - ZipEntry entry = zis.getNextEntry(); - while ( entry != null ) { - if ( entry.getName().equals(name) ) { - rawIs = zis; - break; - } - entry = zis.getNextEntry(); - } + rawIs = forwardToEntry(name); } // Check absolute file name: @@ -80,35 +87,34 @@ public class DecalRegistry { if ( rawIs == null ) { throw new FileNotFoundException( "Unable to locate decal for name " + name ); } - + try { - byte[] bytes = readBytes(rawIs); + byte[] bytes = FileUtils.readBytes(rawIs); // FIXME - update cache; return new ByteArrayInputStream(bytes); } finally { rawIs.close(); } - - } - - private static byte[] readBytes( InputStream is ) throws IOException { - - ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); - - byte[] buffer = new byte[1024]; - - if ( ! (is instanceof BufferedInputStream ) ) { - is = new BufferedInputStream(is); - } - - int bytesRead = 0; - while( (bytesRead = is.read(buffer)) > 0 ) { - bos.write(buffer,0,bytesRead); - } - - return bos.toByteArray(); } + private ZipInputStream forwardToEntry( String name ) throws IOException { + ZipInputStream zis = new ZipInputStream(fileInfo.fileURL.openStream()); + try { + ZipEntry entry = zis.getNextEntry(); + while ( entry != null ) { + if ( entry.getName().equals(name) ) { + return zis; + } + entry = zis.getNextEntry(); + } + } + catch ( IOException ioex ) { + zis.close(); + throw ioex; + } + zis.close(); + return null; + } }