mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-07 06:08:52 +00:00
Use try-with-resources where possible.
This commit is contained in:
parent
12ef4f8039
commit
48e25e01f8
2 changed files with 4 additions and 24 deletions
|
@ -2102,10 +2102,7 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"pointer " + pointer + " cannot exceed strings table size " + table.length);
|
"pointer " + pointer + " cannot exceed strings table size " + table.length);
|
||||||
|
|
||||||
final InputStreamReader reader = new InputStreamReader(
|
try (final InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(table, pointer, table.length - pointer), encoding)) {
|
||||||
new ByteArrayInputStream(table, pointer, table.length - pointer), encoding);
|
|
||||||
|
|
||||||
try {
|
|
||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
|
@ -2113,8 +2110,6 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
||||||
builder.append((char) c);
|
builder.append((char) c);
|
||||||
|
|
||||||
return builder.toString().trim();
|
return builder.toString().trim();
|
||||||
} finally {
|
|
||||||
reader.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2139,10 +2134,7 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"pointer " + pointer + " cannot exceed comments table size " + table.length);
|
"pointer " + pointer + " cannot exceed comments table size " + table.length);
|
||||||
|
|
||||||
final LittleEndianDataInputStream commentsInputStream = new LittleEndianDataInputStream(
|
try (final LittleEndianDataInputStream commentsInputStream = new LittleEndianDataInputStream(new ByteArrayInputStream(table, pointer, table.length - pointer))) {
|
||||||
new ByteArrayInputStream(table, pointer, table.length - pointer));
|
|
||||||
|
|
||||||
try {
|
|
||||||
final int numComments = commentsInputStream.readShortReverse();
|
final int numComments = commentsInputStream.readShortReverse();
|
||||||
final String[] comments = new String[numComments];
|
final String[] comments = new String[numComments];
|
||||||
|
|
||||||
|
@ -2150,8 +2142,6 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
||||||
comments[i] = strings.read(commentsInputStream);
|
comments[i] = strings.read(commentsInputStream);
|
||||||
|
|
||||||
return comments;
|
return comments;
|
||||||
} finally {
|
|
||||||
commentsInputStream.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2177,10 +2167,7 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"pointer " + ptr + " cannot exceed stations table size " + table.length);
|
"pointer " + ptr + " cannot exceed stations table size " + table.length);
|
||||||
|
|
||||||
final LittleEndianDataInputStream stationInputStream = new LittleEndianDataInputStream(
|
try (final LittleEndianDataInputStream stationInputStream = new LittleEndianDataInputStream(new ByteArrayInputStream(table, ptr, 14))) {
|
||||||
new ByteArrayInputStream(table, ptr, 14));
|
|
||||||
|
|
||||||
try {
|
|
||||||
final String[] placeAndName = splitStationName(strings.read(stationInputStream));
|
final String[] placeAndName = splitStationName(strings.read(stationInputStream));
|
||||||
final int id = stationInputStream.readIntReverse();
|
final int id = stationInputStream.readIntReverse();
|
||||||
final int lon = stationInputStream.readIntReverse();
|
final int lon = stationInputStream.readIntReverse();
|
||||||
|
@ -2188,8 +2175,6 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
||||||
|
|
||||||
return new Location(LocationType.STATION, id != 0 ? Integer.toString(id) : null,
|
return new Location(LocationType.STATION, id != 0 ? Integer.toString(id) : null,
|
||||||
Point.from1E6(lat, lon), placeAndName[0], placeAndName[1]);
|
Point.from1E6(lat, lon), placeAndName[0], placeAndName[1]);
|
||||||
} finally {
|
|
||||||
stationInputStream.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -266,9 +266,7 @@ public final class HttpClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
final Call call = okHttpClient.newCall(request.build());
|
final Call call = okHttpClient.newCall(request.build());
|
||||||
Response response = null;
|
try (final Response response = call.execute()) {
|
||||||
try {
|
|
||||||
response = call.execute();
|
|
||||||
final int responseCode = response.code();
|
final int responseCode = response.code();
|
||||||
final String bodyPeek = response.peekBody(SCRAPE_PEEK_SIZE).string().replaceAll("\\p{C}", "");
|
final String bodyPeek = response.peekBody(SCRAPE_PEEK_SIZE).string().replaceAll("\\p{C}", "");
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
@ -308,9 +306,6 @@ public final class HttpClient {
|
||||||
final String message = "got response: " + responseCode + " " + response.message();
|
final String message = "got response: " + responseCode + " " + response.message();
|
||||||
throw new IOException(message + ": " + url);
|
throw new IOException(message + ": " + url);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
if (response != null)
|
|
||||||
response.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue