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:
parent
c9212c2b40
commit
a73358bd27
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user