diff --git a/oeffi/src/de/schildbach/oeffi/OeffiMainActivity.java b/oeffi/src/de/schildbach/oeffi/OeffiMainActivity.java index 9ba245e..9b1e75c 100644 --- a/oeffi/src/de/schildbach/oeffi/OeffiMainActivity.java +++ b/oeffi/src/de/schildbach/oeffi/OeffiMainActivity.java @@ -403,16 +403,12 @@ public abstract class OeffiMainActivity extends OeffiActivity { } private void processMessages(final String network) { - BufferedReader reader = null; + String line = null; - final File indexFile = new File(getFilesDir(), "messages.txt"); - - try { - reader = new BufferedReader(new InputStreamReader( - indexFile.exists() ? new FileInputStream(indexFile) : getAssets().open("messages.txt"), - Charsets.UTF_8)); - + try (final BufferedReader reader = new BufferedReader(new InputStreamReader( + indexFile.exists() ? new FileInputStream(indexFile) : getAssets().open("messages.txt"), + Charsets.UTF_8))) { while (true) { line = reader.readLine(); if (line == null) @@ -430,13 +426,6 @@ public abstract class OeffiMainActivity extends OeffiActivity { } } catch (final IOException x) { // ignore - } finally { - if (reader != null) { - try { - reader.close(); - } catch (final IOException x2) { - } - } } } @@ -556,8 +545,8 @@ public abstract class OeffiMainActivity extends OeffiActivity { request.url(url.build()); final Call call = Application.OKHTTP_CLIENT.newCall(request.build()); call.enqueue(new Callback() { - public void onResponse(final Call call, final Response response) throws IOException { - try { + public void onResponse(final Call call, final Response r) throws IOException { + try (final Response response = r) { if (response.isSuccessful()) { final Bundle message = new Bundle(); message.putString("action", action); @@ -609,8 +598,6 @@ public abstract class OeffiMainActivity extends OeffiActivity { log.info("Got '{}: {}' when fetching message from: '{}'", response.code(), response.message(), url); } - } finally { - response.close(); } } diff --git a/oeffi/src/de/schildbach/oeffi/network/NetworkPickerActivity.java b/oeffi/src/de/schildbach/oeffi/network/NetworkPickerActivity.java index 0a09dbe..c7ec453 100644 --- a/oeffi/src/de/schildbach/oeffi/network/NetworkPickerActivity.java +++ b/oeffi/src/de/schildbach/oeffi/network/NetworkPickerActivity.java @@ -378,12 +378,9 @@ public class NetworkPickerActivity extends Activity implements ActivityCompat.On final Map entriesMap = new LinkedHashMap<>(); final List entries = new LinkedList<>(); - BufferedReader reader = null; String line = null; - - try { - reader = new BufferedReader(new InputStreamReader(getAssets().open(INDEX_FILENAME))); - + try (final BufferedReader reader = + new BufferedReader(new InputStreamReader(getAssets().open(INDEX_FILENAME)))) { while (true) { line = reader.readLine(); if (line == null) @@ -403,12 +400,6 @@ public class NetworkPickerActivity extends Activity implements ActivityCompat.On } } catch (final Exception x) { throw new RuntimeException("problem parsing: '" + line + "'", x); - } finally { - try { - if (reader != null) - reader.close(); - } catch (final IOException x2) { - } } // last used networks diff --git a/oeffi/src/de/schildbach/oeffi/plans/PlanContentProvider.java b/oeffi/src/de/schildbach/oeffi/plans/PlanContentProvider.java index a889790..aaa168d 100644 --- a/oeffi/src/de/schildbach/oeffi/plans/PlanContentProvider.java +++ b/oeffi/src/de/schildbach/oeffi/plans/PlanContentProvider.java @@ -191,9 +191,8 @@ public class PlanContentProvider extends ContentProvider { private Cursor readIndexIntoCursor(final InputStream is, @Nullable final String idFilter, @Nullable final String query) throws IOException, NumberFormatException { - final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - try { + try (final BufferedReader reader = new BufferedReader(new InputStreamReader(is))) { final MatrixCursor cursor = new MatrixCursor( new String[] { BaseColumns._ID, KEY_PLAN_ID, KEY_PLAN_NAME, KEY_PLAN_LAT, KEY_PLAN_LON, KEY_PLAN_VALID_FROM, KEY_PLAN_DISCLAIMER, KEY_PLAN_REMOTE_URL, KEY_PLAN_NETWORK_LOGO }); @@ -240,8 +239,6 @@ public class PlanContentProvider extends ContentProvider { } return cursor; - } finally { - reader.close(); } } @@ -263,9 +260,7 @@ public class PlanContentProvider extends ContentProvider { private Cursor readStationsIntoCursor(final InputStream is, @Nullable final String planIdFilter, @Nullable final String networkFilter, @Nullable final String localIdFilter) throws IOException, NumberFormatException { - final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - - try { + try (final BufferedReader reader = new BufferedReader(new InputStreamReader(is))) { final MatrixCursor cursor = new MatrixCursor(new String[] { BaseColumns._ID, KEY_STATION_NETWORK, KEY_STATION_ID, KEY_STATION_LABEL, KEY_STATION_PLAN_ID, KEY_STATION_X, KEY_STATION_Y }); @@ -327,8 +322,6 @@ public class PlanContentProvider extends ContentProvider { } return cursor; - } finally { - reader.close(); } } diff --git a/oeffi/src/de/schildbach/oeffi/plans/list/PlansAdapter.java b/oeffi/src/de/schildbach/oeffi/plans/list/PlansAdapter.java index d0ea00e..1c7778a 100644 --- a/oeffi/src/de/schildbach/oeffi/plans/list/PlansAdapter.java +++ b/oeffi/src/de/schildbach/oeffi/plans/list/PlansAdapter.java @@ -146,8 +146,8 @@ public class PlansAdapter extends RecyclerView.Adapter { final Call call = cachingOkHttpClient.newCall(request); holder.setCall(call); call.enqueue(new Callback() { - public void onResponse(final Call call, final Response response) throws IOException { - try { + public void onResponse(final Call call, final Response r) throws IOException { + try (final Response response = r) { final Drawable thumb; if (response.isSuccessful()) thumb = new BitmapDrawable(res, response.body().byteStream()); @@ -163,8 +163,6 @@ public class PlansAdapter extends RecyclerView.Adapter { } }); } - } finally { - response.close(); } } diff --git a/oeffi/src/de/schildbach/oeffi/stations/DecodeForeignActivity.java b/oeffi/src/de/schildbach/oeffi/stations/DecodeForeignActivity.java index e921b09..03e4fe5 100644 --- a/oeffi/src/de/schildbach/oeffi/stations/DecodeForeignActivity.java +++ b/oeffi/src/de/schildbach/oeffi/stations/DecodeForeignActivity.java @@ -72,8 +72,8 @@ public class DecodeForeignActivity extends Activity { request.url(HttpUrl.parse(uri.toString())); final Call call = Application.OKHTTP_CLIENT.newCall(request.build()); call.enqueue(new Callback() { - public void onResponse(final Call call, final Response response) throws IOException { - try { + public void onResponse(final Call call, final Response r) throws IOException { + try (final Response response = r) { if (response.isSuccessful()) { final Matcher mRefresh = PATTERN_META_REFRESH.matcher(response.body().string()); if (mRefresh.find()) { @@ -99,8 +99,6 @@ public class DecodeForeignActivity extends Activity { } else { onFail(); } - } finally { - response.close(); } } diff --git a/oeffi/src/de/schildbach/oeffi/util/Downloader.java b/oeffi/src/de/schildbach/oeffi/util/Downloader.java index d24819e..3d7481a 100644 --- a/oeffi/src/de/schildbach/oeffi/util/Downloader.java +++ b/oeffi/src/de/schildbach/oeffi/util/Downloader.java @@ -103,8 +103,8 @@ public class Downloader { private final File tempFile = new File(cacheDir, targetFile.getName() + ".part." + String.format("%04x", random.nextInt(0x10000))); - public void onResponse(final Call call, final Response response) throws IOException { - try { + public void onResponse(final Call call, final Response r) throws IOException { + try (final Response response = r) { final int status = response.code(); if (status == HttpURLConnection.HTTP_OK) { final ResponseBody body = response.body(); @@ -136,7 +136,6 @@ public class Downloader { future.set(status); semaphore.release(); } finally { - response.close(); tempFile.delete(); } } @@ -160,9 +159,7 @@ public class Downloader { final String etag = headers.get("ETag"); final File metaFile = metaFile(file); if (expires != null || etag != null) { - PrintWriter writer = null; - try { - writer = new PrintWriter(metaFile); + try (final PrintWriter writer = new PrintWriter(metaFile)) { if (expires != null) writer.println("Expires: " + expires); if (lastModified != null) @@ -171,9 +168,6 @@ public class Downloader { writer.println("ETag: " + etag); } catch (final IOException x) { log.warn("Problem saving expiration time " + metaFile, x); - } finally { - if (writer != null) - writer.close(); } } else { metaFile.delete(); @@ -186,9 +180,7 @@ public class Downloader { final File metaFile = metaFile(file); if (metaFile.exists()) { String line = null; - BufferedReader reader = null; - try { - reader = new BufferedReader(new FileReader(metaFile), 128); + try (final BufferedReader reader = new BufferedReader(new FileReader(metaFile), 128)) { while (true) { line = reader.readLine(); if (line == null) @@ -204,13 +196,6 @@ public class Downloader { throw new RuntimeException("Problem loading meta data " + metaFile, x); } catch (final Exception x) { throw new RuntimeException("Problem parsing meta data: '" + line + "'", x); - } finally { - try { - if (reader != null) - reader.close(); - } catch (final IOException x) { - // Ignore - } } } return builder.build(); diff --git a/oeffi/src/de/schildbach/oeffi/util/ErrorReporter.java b/oeffi/src/de/schildbach/oeffi/util/ErrorReporter.java index 4ad2168..0838add 100644 --- a/oeffi/src/de/schildbach/oeffi/util/ErrorReporter.java +++ b/oeffi/src/de/schildbach/oeffi/util/ErrorReporter.java @@ -335,8 +335,8 @@ public class ErrorReporter implements Thread.UncaughtExceptionHandler { final Call call = Application.OKHTTP_CLIENT.newCall(request.build()); final Handler callbackHandler = new Handler(Looper.myLooper()); call.enqueue(new Callback() { - public void onResponse(final Call call, final Response response) throws IOException { - try { + public void onResponse(final Call call, final Response r) throws IOException { + try (final Response response = r) { final CharSequence page = response.body().string(); final Matcher m = PATTERN_VERSION.matcher(page); if (m.find()) { @@ -348,8 +348,6 @@ public class ErrorReporter implements Thread.UncaughtExceptionHandler { else callback(null); } - } finally { - response.close(); } }