mirror of
https://gitlab.com/oeffi/oeffi.git
synced 2025-07-07 06:08:51 +00:00
Use try-with-resources where possible.
This commit is contained in:
parent
4c8114497a
commit
d08464cb7f
7 changed files with 20 additions and 70 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -378,12 +378,9 @@ public class NetworkPickerActivity extends Activity implements ActivityCompat.On
|
|||
final Map<String, NetworkListEntry> entriesMap = new LinkedHashMap<>();
|
||||
final List<NetworkListEntry> 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
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -146,8 +146,8 @@ public class PlansAdapter extends RecyclerView.Adapter<PlanViewHolder> {
|
|||
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<PlanViewHolder> {
|
|||
}
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue