Use try-with-resource throughout.

This commit is contained in:
Major-
2014-04-14 16:13:53 +01:00
parent 232accb19f
commit 96e7ea97c5
10 changed files with 62 additions and 120 deletions
+1 -4
View File
@@ -66,11 +66,8 @@ public final class ServiceManager {
XmlParser parser = new XmlParser(); XmlParser parser = new XmlParser();
XmlNode rootNode; XmlNode rootNode;
InputStream is = new FileInputStream("data/services.xml"); try (InputStream is = new FileInputStream("data/services.xml")) {
try {
rootNode = parser.parse(is); rootNode = parser.parse(is);
} finally {
is.close();
} }
if (!rootNode.getName().equals("services")) { if (!rootNode.getName().equals("services")) {
+3 -12
View File
@@ -99,16 +99,12 @@ public final class GameService extends Service {
*/ */
private void init() throws IOException, SAXException, ClassNotFoundException, InstantiationException, private void init() throws IOException, SAXException, ClassNotFoundException, InstantiationException,
IllegalAccessException { IllegalAccessException {
InputStream is = new FileInputStream("data/events.xml"); try (InputStream is = new FileInputStream("data/events.xml")) {
try {
EventHandlerChainParser chainGroupParser = new EventHandlerChainParser(is); EventHandlerChainParser chainGroupParser = new EventHandlerChainParser(is);
chainGroup = chainGroupParser.parse(); chainGroup = chainGroupParser.parse();
} finally {
is.close();
} }
is = new FileInputStream("data/synchronizer.xml"); try (InputStream is = new FileInputStream("data/synchronizer.xml")) {
try {
XmlParser parser = new XmlParser(); XmlParser parser = new XmlParser();
XmlNode rootNode = parser.parse(is); XmlNode rootNode = parser.parse(is);
@@ -123,16 +119,11 @@ public final class GameService extends Service {
Class<?> clazz = Class.forName(activeNode.getValue()); Class<?> clazz = Class.forName(activeNode.getValue());
synchronizer = (ClientSynchronizer) clazz.newInstance(); synchronizer = (ClientSynchronizer) clazz.newInstance();
} finally {
is.close();
} }
is = new FileInputStream("data/rsa.xml"); try (InputStream is = new FileInputStream("data/rsa.xml")) {
try {
RsaKeyParser parser = new RsaKeyParser(is); RsaKeyParser parser = new RsaKeyParser(is);
parser.parse(); parser.parse();
} finally {
is.close();
} }
} }
+1 -4
View File
@@ -240,14 +240,11 @@ public final class World {
ItemDefinition.init(itemDefs); ItemDefinition.init(itemDefs);
logger.info("Loaded " + itemDefs.length + " item definitions."); logger.info("Loaded " + itemDefs.length + " item definitions.");
InputStream is = new BufferedInputStream(new FileInputStream("data/equipment-" + release + ".dat")); try (InputStream is = new BufferedInputStream(new FileInputStream("data/equipment-" + release + ".dat"))) {
try {
EquipmentDefinitionParser parser = new EquipmentDefinitionParser(is); EquipmentDefinitionParser parser = new EquipmentDefinitionParser(is);
EquipmentDefinition[] defs = parser.parse(); EquipmentDefinition[] defs = parser.parse();
EquipmentDefinition.init(defs); EquipmentDefinition.init(defs);
logger.info("Loaded " + defs.length + " equipment definitions."); logger.info("Loaded " + defs.length + " equipment definitions.");
} finally {
is.close();
} }
NpcDefinitionDecoder npcDecoder = new NpcDefinitionDecoder(fs); NpcDefinitionDecoder npcDecoder = new NpcDefinitionDecoder(fs);
+1 -4
View File
@@ -66,11 +66,8 @@ public final class LoginService extends Service {
XmlParser parser = new XmlParser(); XmlParser parser = new XmlParser();
XmlNode rootNode; XmlNode rootNode;
InputStream is = new FileInputStream("data/login.xml"); try (InputStream is = new FileInputStream("data/login.xml")) {
try {
rootNode = parser.parse(is); rootNode = parser.parse(is);
} finally {
is.close();
} }
if (!rootNode.getName().equals("login")) { if (!rootNode.getName().equals("login")) {
+2 -9
View File
@@ -1170,11 +1170,9 @@ public final class EquipmentUpdater {
} }
String release = args[0]; String release = args[0];
DataOutputStream os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data/equipment-" try (DataOutputStream os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data/equipment-"
+ release + ".dat"))); + release + ".dat")));
try { IndexedFileSystem fs = new IndexedFileSystem(new File("data/fs/" + release), true)) {
IndexedFileSystem fs = new IndexedFileSystem(new File("data/fs/" + release), true);
try {
ItemDefinitionDecoder decoder = new ItemDefinitionDecoder(fs); ItemDefinitionDecoder decoder = new ItemDefinitionDecoder(fs);
ItemDefinition[] definitions = decoder.decode(); ItemDefinition[] definitions = decoder.decode();
ItemDefinition.init(definitions); ItemDefinition.init(definitions);
@@ -1196,11 +1194,6 @@ public final class EquipmentUpdater {
os.writeByte(getMagicRequirement(def)); os.writeByte(getMagicRequirement(def));
} }
} }
} finally {
fs.close();
}
} finally {
os.close();
} }
} }
+3 -10
View File
@@ -30,11 +30,9 @@ public final class NoteUpdater {
} }
String release = args[0]; String release = args[0];
DataOutputStream os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data/note-" + release try (DataOutputStream os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data/note-"
+ ".dat"))); + release + ".dat")));
try { IndexedFileSystem fs = new IndexedFileSystem(new File("data/fs/" + release), true)) {
IndexedFileSystem fs = new IndexedFileSystem(new File("data/fs/" + release), true);
try {
ItemDefinitionDecoder decoder = new ItemDefinitionDecoder(fs); ItemDefinitionDecoder decoder = new ItemDefinitionDecoder(fs);
ItemDefinition[] defs = decoder.decode(); ItemDefinition[] defs = decoder.decode();
ItemDefinition.init(defs); ItemDefinition.init(defs);
@@ -57,11 +55,6 @@ public final class NoteUpdater {
os.writeBoolean(false); // not notable os.writeBoolean(false); // not notable
} }
} }
} finally {
fs.close();
}
} finally {
os.close();
} }
} }
@@ -51,12 +51,9 @@ public final class HypertextResourceProvider extends ResourceProvider {
return null; return null;
} }
RandomAccessFile raf = new RandomAccessFile(file, "r");
ByteBuffer buffer; ByteBuffer buffer;
try { try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
buffer = raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length()); buffer = raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length());
} finally {
raf.close();
} }
return buffer; return buffer;
+6 -23
View File
@@ -29,16 +29,13 @@ public final class CompressionUtil {
*/ */
public static byte[] bzip2(byte[] bytes) throws IOException { public static byte[] bzip2(byte[] bytes) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream();
BZip2CompressorOutputStream os = new BZip2CompressorOutputStream(bout, 1); try (BZip2CompressorOutputStream os = new BZip2CompressorOutputStream(bout, 1)) {
try {
os.write(bytes); os.write(bytes);
os.finish(); os.finish();
byte[] compressed = bout.toByteArray(); byte[] compressed = bout.toByteArray();
byte[] newCompressed = new byte[compressed.length - 4]; byte[] newCompressed = new byte[compressed.length - 4];
System.arraycopy(compressed, 4, newCompressed, 0, newCompressed.length); System.arraycopy(compressed, 4, newCompressed, 0, newCompressed.length);
return newCompressed; return newCompressed;
} finally {
os.close();
} }
} }
@@ -51,13 +48,10 @@ public final class CompressionUtil {
*/ */
public static byte[] gzip(byte[] bytes) throws IOException { public static byte[] gzip(byte[] bytes) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream();
DeflaterOutputStream os = new GZIPOutputStream(bout); try (DeflaterOutputStream os = new GZIPOutputStream(bout)) {
try {
os.write(bytes); os.write(bytes);
os.finish(); os.finish();
return bout.toByteArray(); return bout.toByteArray();
} finally {
os.close();
} }
} }
@@ -76,12 +70,9 @@ public final class CompressionUtil {
newCompressed[3] = '1'; newCompressed[3] = '1';
System.arraycopy(compressed, 0, newCompressed, 4, compressed.length); System.arraycopy(compressed, 0, newCompressed, 4, compressed.length);
DataInputStream is = new DataInputStream( try (DataInputStream is = new DataInputStream(new BZip2CompressorInputStream(new ByteArrayInputStream(
new BZip2CompressorInputStream(new ByteArrayInputStream(newCompressed))); newCompressed)))) {
try {
is.readFully(uncompressed); is.readFully(uncompressed);
} finally {
is.close();
} }
} }
@@ -93,11 +84,8 @@ public final class CompressionUtil {
* @throws IOException If an I/O error occurs. * @throws IOException If an I/O error occurs.
*/ */
public static void ungzip(byte[] compressed, byte[] uncompressed) throws IOException { public static void ungzip(byte[] compressed, byte[] uncompressed) throws IOException {
DataInputStream is = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(compressed))); try (DataInputStream is = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(compressed)))) {
try {
is.readFully(uncompressed); is.readFully(uncompressed);
} finally {
is.close();
} }
} }
@@ -113,9 +101,7 @@ public final class CompressionUtil {
compressed.get(data); compressed.get(data);
InputStream is = new GZIPInputStream(new ByteArrayInputStream(data)); InputStream is = new GZIPInputStream(new ByteArrayInputStream(data));
try { try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
while (true) { while (true) {
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int read = is.read(buf, 0, buf.length); int read = is.read(buf, 0, buf.length);
@@ -124,9 +110,6 @@ public final class CompressionUtil {
} }
os.write(buf, 0, read); os.write(buf, 0, read);
} }
} finally {
os.close();
}
return os.toByteArray(); return os.toByteArray();
} finally { } finally {
@@ -72,16 +72,13 @@ public final class PluginManager {
if (plugin.isDirectory() && !plugin.getName().startsWith(".")) { if (plugin.isDirectory() && !plugin.getName().startsWith(".")) {
File xml = new File(plugin, "plugin.xml"); File xml = new File(plugin, "plugin.xml");
if (xml.exists()) { if (xml.exists()) {
InputStream is = new FileInputStream(xml); try (InputStream is = new FileInputStream(xml)) {
try {
PluginMetaDataParser parser = new PluginMetaDataParser(is); PluginMetaDataParser parser = new PluginMetaDataParser(is);
PluginMetaData meta = parser.parse(); PluginMetaData meta = parser.parse();
for (String author : meta.getAuthors()) { for (String author : meta.getAuthors()) {
authors.add(author); authors.add(author);
} }
plugins.add(meta); plugins.add(meta);
} finally {
is.close();
} }
} }
} }
@@ -45,11 +45,8 @@ public final class RubyPluginEnvironment implements PluginEnvironment {
*/ */
private void parseBootstrapper() throws IOException { private void parseBootstrapper() throws IOException {
File bootstrap = new File("./data/plugins/bootstrap.rb"); File bootstrap = new File("./data/plugins/bootstrap.rb");
InputStream is = new FileInputStream(bootstrap); try (InputStream is = new FileInputStream(bootstrap)) {
try {
parse(is, bootstrap.getAbsolutePath()); parse(is, bootstrap.getAbsolutePath());
} finally {
is.close();
} }
} }