Various code fixes (#3704)
Correctly close streams. Avoid ArithmeticException in case of /0.
This commit is contained in:
@@ -56,15 +56,18 @@ public class AEBaseItemBlockChargeable extends AEBaseItemBlock implements IAEIte
|
||||
double internalCurrentPower = 0;
|
||||
final double internalMaxPower = this.getMaxEnergyCapacity();
|
||||
|
||||
if( tag != null )
|
||||
if( internalMaxPower > 0 )
|
||||
{
|
||||
internalCurrentPower = tag.getDouble( "internalCurrentPower" );
|
||||
if( tag != null )
|
||||
{
|
||||
internalCurrentPower = tag.getDouble( "internalCurrentPower" );
|
||||
}
|
||||
|
||||
final double percent = internalCurrentPower / internalMaxPower;
|
||||
|
||||
lines.add( GuiText.StoredEnergy.getLocal() + ':' + MessageFormat.format( " {0,number,#} ", internalCurrentPower ) + Platform
|
||||
.gui_localize( PowerUnits.AE.unlocalizedName ) + " - " + MessageFormat.format( " {0,number,#.##%} ", percent ) );
|
||||
}
|
||||
|
||||
final double percent = internalCurrentPower / internalMaxPower;
|
||||
|
||||
lines.add( GuiText.StoredEnergy.getLocal() + ':' + MessageFormat.format( " {0,number,#} ", internalCurrentPower ) + Platform
|
||||
.gui_localize( PowerUnits.AE.unlocalizedName ) + " - " + MessageFormat.format( " {0,number,#.##%} ", percent ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -84,7 +84,7 @@ public class PacketMEFluidInventoryUpdate extends AppEngPacket
|
||||
|
||||
// int originalBytes = stream.readableBytes();
|
||||
|
||||
final GZIPInputStream gzReader = new GZIPInputStream( new InputStream()
|
||||
try( final GZIPInputStream gzReader = new GZIPInputStream( new InputStream()
|
||||
{
|
||||
@Override
|
||||
public int read() throws IOException
|
||||
@@ -96,26 +96,26 @@ public class PacketMEFluidInventoryUpdate extends AppEngPacket
|
||||
|
||||
return stream.readByte() & STREAM_MASK;
|
||||
}
|
||||
} );
|
||||
|
||||
final ByteBuf uncompressed = Unpooled.buffer( stream.readableBytes() );
|
||||
final byte[] tmp = new byte[TEMP_BUFFER_SIZE];
|
||||
while( gzReader.available() != 0 )
|
||||
} ) )
|
||||
{
|
||||
final int bytes = gzReader.read( tmp );
|
||||
if( bytes > 0 )
|
||||
|
||||
final ByteBuf uncompressed = Unpooled.buffer( stream.readableBytes() );
|
||||
final byte[] tmp = new byte[TEMP_BUFFER_SIZE];
|
||||
|
||||
while( gzReader.available() != 0 )
|
||||
{
|
||||
uncompressed.writeBytes( tmp, 0, bytes );
|
||||
final int bytes = gzReader.read( tmp );
|
||||
|
||||
if( bytes > 0 )
|
||||
{
|
||||
uncompressed.writeBytes( tmp, 0, bytes );
|
||||
}
|
||||
}
|
||||
}
|
||||
gzReader.close();
|
||||
|
||||
// int uncompressedBytes = uncompressed.readableBytes();
|
||||
// AELog.info( "Receiver: " + originalBytes + " -> " + uncompressedBytes );
|
||||
|
||||
while( uncompressed.readableBytes() > 0 )
|
||||
{
|
||||
this.list.add( AEFluidStack.fromPacket( uncompressed ) );
|
||||
while( uncompressed.readableBytes() > 0 )
|
||||
{
|
||||
this.list.add( AEFluidStack.fromPacket( uncompressed ) );
|
||||
}
|
||||
}
|
||||
|
||||
this.empty = this.list.isEmpty();
|
||||
|
||||
@@ -82,7 +82,7 @@ public class PacketMEInventoryUpdate extends AppEngPacket
|
||||
|
||||
// int originalBytes = stream.readableBytes();
|
||||
|
||||
final GZIPInputStream gzReader = new GZIPInputStream( new InputStream()
|
||||
try( GZIPInputStream gzReader = new GZIPInputStream( new InputStream()
|
||||
{
|
||||
@Override
|
||||
public int read() throws IOException
|
||||
@@ -94,29 +94,29 @@ public class PacketMEInventoryUpdate extends AppEngPacket
|
||||
|
||||
return stream.readByte() & STREAM_MASK;
|
||||
}
|
||||
} );
|
||||
|
||||
final ByteBuf uncompressed = Unpooled.buffer( stream.readableBytes() );
|
||||
final byte[] tmp = new byte[TEMP_BUFFER_SIZE];
|
||||
while( gzReader.available() != 0 )
|
||||
} ) )
|
||||
{
|
||||
final int bytes = gzReader.read( tmp );
|
||||
if( bytes > 0 )
|
||||
final ByteBuf uncompressed = Unpooled.buffer( stream.readableBytes() );
|
||||
final byte[] tmp = new byte[TEMP_BUFFER_SIZE];
|
||||
|
||||
while( gzReader.available() != 0 )
|
||||
{
|
||||
uncompressed.writeBytes( tmp, 0, bytes );
|
||||
final int bytes = gzReader.read( tmp );
|
||||
|
||||
if( bytes > 0 )
|
||||
{
|
||||
uncompressed.writeBytes( tmp, 0, bytes );
|
||||
}
|
||||
}
|
||||
}
|
||||
gzReader.close();
|
||||
|
||||
// int uncompressedBytes = uncompressed.readableBytes();
|
||||
// AELog.info( "Receiver: " + originalBytes + " -> " + uncompressedBytes );
|
||||
|
||||
while( uncompressed.readableBytes() > 0 )
|
||||
{
|
||||
this.list.add( AEItemStack.fromPacket( uncompressed ) );
|
||||
while( uncompressed.readableBytes() > 0 )
|
||||
{
|
||||
this.list.add( AEItemStack.fromPacket( uncompressed ) );
|
||||
}
|
||||
}
|
||||
|
||||
this.empty = this.list.isEmpty();
|
||||
|
||||
}
|
||||
|
||||
// api
|
||||
|
||||
@@ -102,18 +102,15 @@ final class MinecraftItemCSVExporter implements Exporter
|
||||
|
||||
final File file = new File( this.exportDirectory, ITEM_CSV_FILE_NAME );
|
||||
|
||||
try
|
||||
try( final Writer writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file ), Charset.forName( "UTF-8" ) ) ) )
|
||||
{
|
||||
FileUtils.forceMkdir( this.exportDirectory );
|
||||
|
||||
final Writer writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file ), Charset.forName( "UTF-8" ) ) );
|
||||
|
||||
final String header = this.mode == ExportMode.MINIMAL ? MINIMAL_HEADER : VERBOSE_HEADER;
|
||||
writer.write( header );
|
||||
writer.write( "\n" );
|
||||
writer.write( joined );
|
||||
writer.flush();
|
||||
writer.close();
|
||||
|
||||
AELog.info( EXPORT_SUCCESSFUL_MESSAGE, lines.size(), ITEM_CSV_FILE_NAME );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user