We need to pull the bytes out of the input stream so we can close the input stream. It seems the Jogl Texture loader does not close the input stream. When loading textures from a zip file, this leaves the zip file open and prevents saving/ moving the file.

This commit is contained in:
Kevin Ruland 2012-07-23 17:10:41 +00:00 committed by U-WINDOWS-C28163E\Administrator
parent 3195b8d5df
commit d40a36d09b

View File

@ -1,5 +1,8 @@
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;
@ -27,6 +30,15 @@ public class DecalRegistry {
this.isZipFile = isZipFile;
}
/**
* 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.
*
* @param name
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public InputStream getDecal( String name ) throws FileNotFoundException, IOException {
/* FIXME - Caching?
byte[] bytes = cache.get(name);
@ -34,33 +46,69 @@ public class DecalRegistry {
return new ByteArrayInputStream(bytes);
}
*/
InputStream rawIs = null;
if ( isZipFile ) {
ZipInputStream zis = new ZipInputStream(fileInfo.fileURL.openStream());
ZipEntry entry = zis.getNextEntry();
while ( entry != null ) {
if ( entry.getName().equals(name) ) {
return zis;
rawIs = zis;
break;
}
entry = zis.getNextEntry();
}
}
// Check absolute file name:
{
if ( rawIs == null ) {
File decal = new File(name);
if ( decal.isAbsolute() ) {
return new FileInputStream(decal);
rawIs = new FileInputStream(decal);
}
}
// Try relative to the model file directory.
if ( rawIs == null ) {
if( fileInfo.getDirectory() != null ) {
File decal = new File(fileInfo.getDirectory(), name);
// FIXME - update cache
return new FileInputStream(decal);
rawIs = new FileInputStream(decal);
}
}
if ( rawIs == null ) {
throw new FileNotFoundException( "Unable to locate decal for name " + name );
}
try {
byte[] bytes = 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();
}
}