Merge pull request #427 from yueh/development

Fix potentially unclosed streams
This commit is contained in:
thatsIch
2014-11-21 12:45:55 +01:00
+54 -44
View File
@@ -18,9 +18,11 @@
package appeng.core; package appeng.core;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@@ -31,6 +33,8 @@ import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import com.mojang.authlib.GameProfile;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.CompressedStreamTools;
@@ -40,6 +44,7 @@ import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.config.ConfigCategory; import net.minecraftforge.common.config.ConfigCategory;
import net.minecraftforge.common.config.Configuration; import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property; import net.minecraftforge.common.config.Property;
import appeng.api.util.WorldCoord; import appeng.api.util.WorldCoord;
import appeng.core.sync.network.NetworkHandler; import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketNewStorageDimension; import appeng.core.sync.packets.PacketNewStorageDimension;
@@ -49,26 +54,26 @@ import appeng.me.GridStorage;
import appeng.me.GridStorageSearch; import appeng.me.GridStorageSearch;
import appeng.services.CompassService; import appeng.services.CompassService;
import com.mojang.authlib.GameProfile;
public class WorldSettings extends Configuration public class WorldSettings extends Configuration
{ {
private static final String SPAWNDATA_FOLDER = "spawndata";
private static final String COMPASS_Folder = "compass";
private static WorldSettings instance; private static WorldSettings instance;
long lastGridStorage = 0; private final File aeFolder;
int lastPlayer = 0;
private final CompassService compass; private final CompassService compass;
final File AEFolder; private long lastGridStorage = 0;
private int lastPlayer = 0;
public WorldSettings( File aeFolder ) public WorldSettings( File aeFolder )
{ {
super( new File( aeFolder.getPath() + File.separatorChar + "settings.cfg" ) ); super( new File( aeFolder.getPath() + File.separatorChar + "settings.cfg" ) );
AEFolder = aeFolder; this.aeFolder = aeFolder;
this.compass = new CompassService( aeFolder );
compass = new CompassService( AEFolder );
for ( int dimID : get( "DimensionManager", "StorageCells", new int[0] ).getIntList() ) for ( int dimID : get( "DimensionManager", "StorageCells", new int[0] ).getIntList() )
{ {
@@ -93,40 +98,44 @@ public class WorldSettings extends Configuration
if ( !Thread.holdsLock( WorldSettings.class ) ) if ( !Thread.holdsLock( WorldSettings.class ) )
throw new RuntimeException( "Invalid Request" ); throw new RuntimeException( "Invalid Request" );
File f = new File( AEFolder, "spawndata" + File.separatorChar + dim + "_" + (chunkX >> 4) + "_" + (chunkZ >> 4) + ".dat" );
if ( f.isFile() && f.exists() )
{
// open
FileInputStream fis;
try
{
fis = new FileInputStream( f );
NBTTagCompound data = null; NBTTagCompound data = null;
File file = new File( aeFolder, SPAWNDATA_FOLDER + File.separatorChar + dim + "_" + ( chunkX >> 4 ) + "_" + ( chunkZ >> 4 ) + ".dat" );
if ( file.isFile() )
{
FileInputStream fileInputStream = null;
try try
{ {
data = CompressedStreamTools.readCompressed( fis ); fileInputStream = new FileInputStream( file );
data = CompressedStreamTools.readCompressed( fileInputStream );
} }
catch ( Throwable e ) catch ( Throwable e )
{ {
data = new NBTTagCompound(); data = new NBTTagCompound();
AELog.error( e ); AELog.error( e );
} }
finally
fis.close(); {
if ( fileInputStream != null )
return data; {
try
{
fileInputStream.close();
} }
catch (Throwable e) catch ( IOException e )
{ {
AELog.error( e ); AELog.error( e );
} }
}
}
}
else
{
data = new NBTTagCompound();
} }
return new NBTTagCompound(); return data;
} }
void writeSpawnData( int dim, int chunkX, int chunkZ, NBTTagCompound data ) void writeSpawnData( int dim, int chunkX, int chunkZ, NBTTagCompound data )
@@ -134,29 +143,33 @@ public class WorldSettings extends Configuration
if ( !Thread.holdsLock( WorldSettings.class ) ) if ( !Thread.holdsLock( WorldSettings.class ) )
throw new RuntimeException( "Invalid Request" ); throw new RuntimeException( "Invalid Request" );
File f = new File( AEFolder, "spawndata" + File.separatorChar + dim + "_" + (chunkX >> 4) + "_" + (chunkZ >> 4) + ".dat" ); File file = new File( aeFolder, SPAWNDATA_FOLDER + File.separatorChar + dim + "_" + ( chunkX >> 4 ) + "_" + ( chunkZ >> 4 ) + ".dat" );
FileOutputStream fileOutputStream = null;
try try
{ {
// save fileOutputStream = new FileOutputStream( file );
FileOutputStream fos = new FileOutputStream( f ); CompressedStreamTools.writeCompressed( data, fileOutputStream );
try
{
CompressedStreamTools.writeCompressed( data, fos );
} }
catch ( Throwable e ) catch ( Throwable e )
{ {
AELog.error( e ); AELog.error( e );
} }
finally
fos.close(); {
if ( fileOutputStream != null )
{
try
{
fileOutputStream.close();
} }
catch (Throwable e) catch ( IOException e )
{ {
AELog.error( e ); AELog.error( e );
} }
} }
}
}
public Collection<NBTTagCompound> getNearByMeteorites( int dim, int chunkX, int chunkZ ) public Collection<NBTTagCompound> getNearByMeteorites( int dim, int chunkX, int chunkZ )
{ {
@@ -271,22 +284,19 @@ public class WorldSettings extends Configuration
File aeBaseFolder = new File( world.getPath() + File.separatorChar + "AE2" ); File aeBaseFolder = new File( world.getPath() + File.separatorChar + "AE2" );
if ( !aeBaseFolder.exists() || !aeBaseFolder.isDirectory() ) if ( !aeBaseFolder.isDirectory() && !aeBaseFolder.mkdir() )
if ( !aeBaseFolder.mkdir() || !aeBaseFolder.exists() )
{ {
throw new RuntimeException( "Failed to create " + aeBaseFolder.getAbsolutePath() ); throw new RuntimeException( "Failed to create " + aeBaseFolder.getAbsolutePath() );
} }
File compass = new File( aeBaseFolder, "compass" ); File compass = new File( aeBaseFolder, COMPASS_Folder );
if ( !compass.exists() || !compass.isDirectory() ) if ( !compass.isDirectory() && !compass.mkdir() )
if ( !compass.mkdir() || !compass.exists() )
{ {
throw new RuntimeException( "Failed to create " + compass.getAbsolutePath() ); throw new RuntimeException( "Failed to create " + compass.getAbsolutePath() );
} }
File spawnData = new File( aeBaseFolder, "spawndata" ); File spawnData = new File( aeBaseFolder, SPAWNDATA_FOLDER );
if ( !spawnData.exists() || !spawnData.isDirectory() ) if ( !spawnData.isDirectory() && !spawnData.mkdir() )
if ( !spawnData.mkdir() || !spawnData.exists() )
{ {
throw new RuntimeException( "Failed to create " + spawnData.getAbsolutePath() ); throw new RuntimeException( "Failed to create " + spawnData.getAbsolutePath() );
} }