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();
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")) {
+3 -12
View File
@@ -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();
}
}
+1 -4
View File
@@ -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);
+1 -4
View File
@@ -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")) {
+20 -27
View File
@@ -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();
}
}
+21 -28
View File
@@ -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<Integer, Integer> itemToNote = new HashMap<Integer, Integer>();
os.writeShort(defs.length);
Map<Integer, Integer> itemToNote = new HashMap<Integer, Integer>();
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();
}
}
@@ -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;
+12 -29
View File
@@ -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();
@@ -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();
}
}
}
@@ -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();
}
}