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;
|
package net.sf.openrocket.document;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@ -12,6 +10,7 @@ import java.util.zip.ZipEntry;
|
|||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
import net.sf.openrocket.file.FileInfo;
|
import net.sf.openrocket.file.FileInfo;
|
||||||
|
import net.sf.openrocket.util.FileUtils;
|
||||||
|
|
||||||
public class DecalRegistry {
|
public class DecalRegistry {
|
||||||
|
|
||||||
@ -30,6 +29,22 @@ public class DecalRegistry {
|
|||||||
this.isZipFile = isZipFile;
|
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.
|
* 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.
|
* 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;
|
InputStream rawIs = null;
|
||||||
|
|
||||||
if ( isZipFile ) {
|
if ( isZipFile ) {
|
||||||
ZipInputStream zis = new ZipInputStream(fileInfo.fileURL.openStream());
|
rawIs = forwardToEntry(name);
|
||||||
ZipEntry entry = zis.getNextEntry();
|
|
||||||
while ( entry != null ) {
|
|
||||||
if ( entry.getName().equals(name) ) {
|
|
||||||
rawIs = zis;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
entry = zis.getNextEntry();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check absolute file name:
|
// Check absolute file name:
|
||||||
@ -82,7 +89,7 @@ public class DecalRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
byte[] bytes = readBytes(rawIs);
|
byte[] bytes = FileUtils.readBytes(rawIs);
|
||||||
// FIXME - update cache;
|
// FIXME - update cache;
|
||||||
return new ByteArrayInputStream(bytes);
|
return new ByteArrayInputStream(bytes);
|
||||||
}
|
}
|
||||||
@ -92,23 +99,22 @@ public class DecalRegistry {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] readBytes( InputStream is ) throws IOException {
|
private ZipInputStream forwardToEntry( String name ) throws IOException {
|
||||||
|
ZipInputStream zis = new ZipInputStream(fileInfo.fileURL.openStream());
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
|
try {
|
||||||
|
ZipEntry entry = zis.getNextEntry();
|
||||||
byte[] buffer = new byte[1024];
|
while ( entry != null ) {
|
||||||
|
if ( entry.getName().equals(name) ) {
|
||||||
if ( ! (is instanceof BufferedInputStream ) ) {
|
return zis;
|
||||||
is = new BufferedInputStream(is);
|
}
|
||||||
|
entry = zis.getNextEntry();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch ( IOException ioex ) {
|
||||||
int bytesRead = 0;
|
zis.close();
|
||||||
while( (bytesRead = is.read(buffer)) > 0 ) {
|
throw ioex;
|
||||||
bos.write(buffer,0,bytesRead);
|
|
||||||
}
|
}
|
||||||
|
zis.close();
|
||||||
return bos.toByteArray();
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user