Skip to content

Commit

Permalink
front: Fix resource leaks and quick abort on asset extraction failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
littleguy77 committed Jan 21, 2013
1 parent bd588f9 commit cc1363a
Showing 1 changed file with 58 additions and 11 deletions.
69 changes: 58 additions & 11 deletions src/paulscode/android/mupen64plusae/util/AssetExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package paulscode.android.mupen64plusae.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -60,7 +61,8 @@ public static boolean extractAssets( AssetManager assetManager, String srcPath,
for( String srcSubPath : srcSubPaths )
{
String suffix = "/" + srcSubPath;
result &= extractAssets( assetManager, srcPath + suffix, dstPath + suffix, onProgress );
if( !extractAssets( assetManager, srcPath + suffix, dstPath + suffix, onProgress ) )
return false;
}
}
else
Expand All @@ -71,28 +73,73 @@ public static boolean extractAssets( AssetManager assetManager, String srcPath,
if( onProgress != null )
onProgress.onExtractionProgress( dstPath );

// IO objects, initialize null to eliminate lint error
OutputStream out = null;
InputStream in = null;

// Extract the file
try
{
InputStream in = assetManager.open( srcPath );
OutputStream out = new FileOutputStream( dstPath );
out = new FileOutputStream( dstPath );
in = assetManager.open( srcPath );
byte[] buffer = new byte[1024];
int read;

while( ( read = in.read( buffer ) ) != -1 )
{
out.write( buffer, 0, read );
}

in.close();
out.flush();
out.close();
}
catch( FileNotFoundException e )
{
Log.e( "AssetExtractor", "Failed to open output file " + dstPath, e );
result = false;
}
catch( IOException e )
{
Log.w( "AssetExtractor", "Failed to extract asset file: " + srcPath );
Log.e( "AssetExtractor", "Failed to extract asset " + srcPath + " to " + dstPath, e );
result = false;
}
finally
{
if( out != null )
{
try
{
out.flush();
}
catch( IOException e )
{
Log.e( "AssetExtractor", "Failed to flush output file " + dstPath, e );
result = false;
}
try
{
out.close();
}
catch( IOException e )
{
Log.e( "AssetExtractor", "Failed to close output file " + dstPath, e );
result = false;
}
}
if( in != null )
{
try
{
in.close();
}
catch( IOException e )
{
Log.e( "AssetExtractor", "Failed to close asset " + srcPath, e );
result = false;
}
}
}
}

return result;
Expand All @@ -101,7 +148,7 @@ public static boolean extractAssets( AssetManager assetManager, String srcPath,
public static int countAssets( AssetManager assetManager, String srcPath )
{
int count = 0;

// TODO: This function takes a surprisingly long time to complete.
if( srcPath.startsWith( "/" ) )
srcPath = srcPath.substring( 1 );
Expand All @@ -120,14 +167,14 @@ public static int countAssets( AssetManager assetManager, String srcPath )
// srcPath is a file
count++;
}

return count;
}

private static String[] getAssetList( AssetManager assetManager, String srcPath )
{
String[] srcSubPaths = null;

try
{
srcSubPaths = assetManager.list( srcPath );
Expand All @@ -136,7 +183,7 @@ private static String[] getAssetList( AssetManager assetManager, String srcPath
{
Log.w( "AssetExtractor", "Failed to get asset file list." );
}

return srcSubPaths;
}
}

0 comments on commit cc1363a

Please sign in to comment.