diff --git a/src/org/apollo/ServiceManager.java b/src/org/apollo/ServiceManager.java index dfda54ea..9828a1a6 100644 --- a/src/org/apollo/ServiceManager.java +++ b/src/org/apollo/ServiceManager.java @@ -66,11 +66,8 @@ public final class ServiceManager { XmlParser parser = new XmlParser(); XmlNode rootNode; - InputStream is = new FileInputStream("data/services.xml"); - try { + try (InputStream is = new FileInputStream("data/services.xml")) { rootNode = parser.parse(is); - } finally { - is.close(); } if (!rootNode.getName().equals("services")) { diff --git a/src/org/apollo/game/GameService.java b/src/org/apollo/game/GameService.java index 6f75989d..eaf998aa 100644 --- a/src/org/apollo/game/GameService.java +++ b/src/org/apollo/game/GameService.java @@ -99,16 +99,12 @@ public final class GameService extends Service { */ private void init() throws IOException, SAXException, ClassNotFoundException, InstantiationException, IllegalAccessException { - InputStream is = new FileInputStream("data/events.xml"); - try { + try (InputStream is = new FileInputStream("data/events.xml")) { EventHandlerChainParser chainGroupParser = new EventHandlerChainParser(is); chainGroup = chainGroupParser.parse(); - } finally { - is.close(); } - is = new FileInputStream("data/synchronizer.xml"); - try { + try (InputStream is = new FileInputStream("data/synchronizer.xml")) { XmlParser parser = new XmlParser(); XmlNode rootNode = parser.parse(is); @@ -123,16 +119,11 @@ public final class GameService extends Service { Class clazz = Class.forName(activeNode.getValue()); synchronizer = (ClientSynchronizer) clazz.newInstance(); - } finally { - is.close(); } - is = new FileInputStream("data/rsa.xml"); - try { + try (InputStream is = new FileInputStream("data/rsa.xml")) { RsaKeyParser parser = new RsaKeyParser(is); parser.parse(); - } finally { - is.close(); } } diff --git a/src/org/apollo/game/model/World.java b/src/org/apollo/game/model/World.java index be765a00..34ac4817 100644 --- a/src/org/apollo/game/model/World.java +++ b/src/org/apollo/game/model/World.java @@ -240,14 +240,11 @@ public final class World { ItemDefinition.init(itemDefs); logger.info("Loaded " + itemDefs.length + " item definitions."); - InputStream is = new BufferedInputStream(new FileInputStream("data/equipment-" + release + ".dat")); - try { + try (InputStream is = new BufferedInputStream(new FileInputStream("data/equipment-" + release + ".dat"))) { EquipmentDefinitionParser parser = new EquipmentDefinitionParser(is); EquipmentDefinition[] defs = parser.parse(); EquipmentDefinition.init(defs); logger.info("Loaded " + defs.length + " equipment definitions."); - } finally { - is.close(); } NpcDefinitionDecoder npcDecoder = new NpcDefinitionDecoder(fs); diff --git a/src/org/apollo/login/LoginService.java b/src/org/apollo/login/LoginService.java index b93bd75d..c477750f 100644 --- a/src/org/apollo/login/LoginService.java +++ b/src/org/apollo/login/LoginService.java @@ -66,11 +66,8 @@ public final class LoginService extends Service { XmlParser parser = new XmlParser(); XmlNode rootNode; - InputStream is = new FileInputStream("data/login.xml"); - try { + try (InputStream is = new FileInputStream("data/login.xml")) { rootNode = parser.parse(is); - } finally { - is.close(); } if (!rootNode.getName().equals("login")) { diff --git a/src/org/apollo/tools/EquipmentUpdater.java b/src/org/apollo/tools/EquipmentUpdater.java index d7d1f668..ce32f421 100644 --- a/src/org/apollo/tools/EquipmentUpdater.java +++ b/src/org/apollo/tools/EquipmentUpdater.java @@ -1170,37 +1170,30 @@ public final class EquipmentUpdater { } 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"))); - try { - IndexedFileSystem fs = new IndexedFileSystem(new File("data/fs/" + release), true); - try { - ItemDefinitionDecoder decoder = new ItemDefinitionDecoder(fs); - ItemDefinition[] definitions = decoder.decode(); - ItemDefinition.init(definitions); + IndexedFileSystem fs = new IndexedFileSystem(new File("data/fs/" + release), true)) { + ItemDefinitionDecoder decoder = new ItemDefinitionDecoder(fs); + ItemDefinition[] definitions = decoder.decode(); + ItemDefinition.init(definitions); - os.writeShort(definitions.length); - for (int id = 0; id < definitions.length; id++) { - ItemDefinition def = ItemDefinition.lookup(id); - int type = getWeaponType(def); - os.writeByte(type); - if (type != -1) { - os.writeBoolean(isTwoHanded(def)); - os.writeBoolean(isFullBody(def)); - os.writeBoolean(isFullHat(def)); - os.writeBoolean(isFullMask(def)); - os.writeByte(getAttackRequirement(def)); - os.writeByte(getStrengthRequirement(def)); - os.writeByte(getDefenceRequirement(def)); - os.writeByte(getRangedRequirement(def)); - os.writeByte(getMagicRequirement(def)); - } + os.writeShort(definitions.length); + for (int id = 0; id < definitions.length; id++) { + ItemDefinition def = ItemDefinition.lookup(id); + int type = getWeaponType(def); + os.writeByte(type); + if (type != -1) { + os.writeBoolean(isTwoHanded(def)); + os.writeBoolean(isFullBody(def)); + os.writeBoolean(isFullHat(def)); + os.writeBoolean(isFullMask(def)); + os.writeByte(getAttackRequirement(def)); + os.writeByte(getStrengthRequirement(def)); + os.writeByte(getDefenceRequirement(def)); + os.writeByte(getRangedRequirement(def)); + os.writeByte(getMagicRequirement(def)); } - } finally { - fs.close(); } - } finally { - os.close(); } } diff --git a/src/org/apollo/tools/NoteUpdater.java b/src/org/apollo/tools/NoteUpdater.java index 6db9de8c..c47aa7db 100644 --- a/src/org/apollo/tools/NoteUpdater.java +++ b/src/org/apollo/tools/NoteUpdater.java @@ -30,38 +30,31 @@ public final class NoteUpdater { } String release = args[0]; - DataOutputStream os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data/note-" + release - + ".dat"))); - try { - IndexedFileSystem fs = new IndexedFileSystem(new File("data/fs/" + release), true); - try { - ItemDefinitionDecoder decoder = new ItemDefinitionDecoder(fs); - ItemDefinition[] defs = decoder.decode(); - ItemDefinition.init(defs); + try (DataOutputStream os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data/note-" + + release + ".dat"))); + IndexedFileSystem fs = new IndexedFileSystem(new File("data/fs/" + release), true)) { + ItemDefinitionDecoder decoder = new ItemDefinitionDecoder(fs); + ItemDefinition[] defs = decoder.decode(); + ItemDefinition.init(defs); - os.writeShort(defs.length); - Map itemToNote = new HashMap(); + os.writeShort(defs.length); + Map itemToNote = new HashMap(); - for (int id = 0; id < defs.length; id++) { - ItemDefinition def = ItemDefinition.lookup(id); - if (def.isNote()) { - itemToNote.put(def.getNoteInfoId(), def.getId()); - } + for (int id = 0; id < defs.length; id++) { + ItemDefinition def = ItemDefinition.lookup(id); + if (def.isNote()) { + itemToNote.put(def.getNoteInfoId(), def.getId()); + } + } + + for (int id = 0; id < defs.length; id++) { + if (itemToNote.containsKey(id)) { + os.writeBoolean(true); // notable + os.writeShort(itemToNote.get(id)); + } else { + os.writeBoolean(false); // not notable } - - for (int id = 0; id < defs.length; id++) { - if (itemToNote.containsKey(id)) { - os.writeBoolean(true); // notable - os.writeShort(itemToNote.get(id)); - } else { - os.writeBoolean(false); // not notable - } - } - } finally { - fs.close(); } - } finally { - os.close(); } } diff --git a/src/org/apollo/update/resource/HypertextResourceProvider.java b/src/org/apollo/update/resource/HypertextResourceProvider.java index 81e5e484..7b6fed1f 100644 --- a/src/org/apollo/update/resource/HypertextResourceProvider.java +++ b/src/org/apollo/update/resource/HypertextResourceProvider.java @@ -51,12 +51,9 @@ public final class HypertextResourceProvider extends ResourceProvider { return null; } - RandomAccessFile raf = new RandomAccessFile(file, "r"); ByteBuffer buffer; - try { + try (RandomAccessFile raf = new RandomAccessFile(file, "r")) { buffer = raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length()); - } finally { - raf.close(); } return buffer; diff --git a/src/org/apollo/util/CompressionUtil.java b/src/org/apollo/util/CompressionUtil.java index 2cc07a81..cc3ce697 100644 --- a/src/org/apollo/util/CompressionUtil.java +++ b/src/org/apollo/util/CompressionUtil.java @@ -29,16 +29,13 @@ public final class CompressionUtil { */ public static byte[] bzip2(byte[] bytes) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); - BZip2CompressorOutputStream os = new BZip2CompressorOutputStream(bout, 1); - try { + try (BZip2CompressorOutputStream os = new BZip2CompressorOutputStream(bout, 1)) { os.write(bytes); os.finish(); byte[] compressed = bout.toByteArray(); byte[] newCompressed = new byte[compressed.length - 4]; System.arraycopy(compressed, 4, newCompressed, 0, newCompressed.length); return newCompressed; - } finally { - os.close(); } } @@ -51,13 +48,10 @@ public final class CompressionUtil { */ public static byte[] gzip(byte[] bytes) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); - DeflaterOutputStream os = new GZIPOutputStream(bout); - try { + try (DeflaterOutputStream os = new GZIPOutputStream(bout)) { os.write(bytes); os.finish(); return bout.toByteArray(); - } finally { - os.close(); } } @@ -76,12 +70,9 @@ public final class CompressionUtil { newCompressed[3] = '1'; System.arraycopy(compressed, 0, newCompressed, 4, compressed.length); - DataInputStream is = new DataInputStream( - new BZip2CompressorInputStream(new ByteArrayInputStream(newCompressed))); - try { + try (DataInputStream is = new DataInputStream(new BZip2CompressorInputStream(new ByteArrayInputStream( + newCompressed)))) { is.readFully(uncompressed); - } finally { - is.close(); } } @@ -93,11 +84,8 @@ public final class CompressionUtil { * @throws IOException If an I/O error occurs. */ public static void ungzip(byte[] compressed, byte[] uncompressed) throws IOException { - DataInputStream is = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(compressed))); - try { + try (DataInputStream is = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(compressed)))) { is.readFully(uncompressed); - } finally { - is.close(); } } @@ -113,19 +101,14 @@ public final class CompressionUtil { compressed.get(data); InputStream is = new GZIPInputStream(new ByteArrayInputStream(data)); - try { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - try { - while (true) { - byte[] buf = new byte[1024]; - int read = is.read(buf, 0, buf.length); - if (read == -1) { - break; - } - os.write(buf, 0, read); + try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { + while (true) { + byte[] buf = new byte[1024]; + int read = is.read(buf, 0, buf.length); + if (read == -1) { + break; } - } finally { - os.close(); + os.write(buf, 0, read); } return os.toByteArray(); diff --git a/src/org/apollo/util/plugin/PluginManager.java b/src/org/apollo/util/plugin/PluginManager.java index 00f1f71f..b0db4afa 100644 --- a/src/org/apollo/util/plugin/PluginManager.java +++ b/src/org/apollo/util/plugin/PluginManager.java @@ -72,16 +72,13 @@ public final class PluginManager { if (plugin.isDirectory() && !plugin.getName().startsWith(".")) { File xml = new File(plugin, "plugin.xml"); if (xml.exists()) { - InputStream is = new FileInputStream(xml); - try { + try (InputStream is = new FileInputStream(xml)) { PluginMetaDataParser parser = new PluginMetaDataParser(is); PluginMetaData meta = parser.parse(); for (String author : meta.getAuthors()) { authors.add(author); } plugins.add(meta); - } finally { - is.close(); } } } diff --git a/src/org/apollo/util/plugin/RubyPluginEnvironment.java b/src/org/apollo/util/plugin/RubyPluginEnvironment.java index b1f0e5f5..23745705 100644 --- a/src/org/apollo/util/plugin/RubyPluginEnvironment.java +++ b/src/org/apollo/util/plugin/RubyPluginEnvironment.java @@ -45,11 +45,8 @@ public final class RubyPluginEnvironment implements PluginEnvironment { */ private void parseBootstrapper() throws IOException { File bootstrap = new File("./data/plugins/bootstrap.rb"); - InputStream is = new FileInputStream(bootstrap); - try { + try (InputStream is = new FileInputStream(bootstrap)) { parse(is, bootstrap.getAbsolutePath()); - } finally { - is.close(); } }