Added isExportable which tests if a decal is currently available from the container file. Refactored the code some. Use the FileUtils readBytes[] method.

This commit is contained in:
Kevin Ruland 2012-07-25 18:35:25 +00:00 committed by U-WINDOWS-C28163E\Administrator
parent c9212c2b40
commit a73358bd27

View File

@ -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:
@ -82,7 +89,7 @@ public class DecalRegistry {
}
try {
byte[] bytes = readBytes(rawIs);
byte[] bytes = FileUtils.readBytes(rawIs);
// FIXME - update cache;
return new ByteArrayInputStream(bytes);
}
@ -92,23 +99,22 @@ public class DecalRegistry {
}
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);
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();
}
}
int bytesRead = 0;
while( (bytesRead = is.read(buffer)) > 0 ) {
bos.write(buffer,0,bytesRead);
catch ( IOException ioex ) {
zis.close();
throw ioex;
}
return bos.toByteArray();
zis.close();
return null;
}
}