Skip to content

Commit 002de4f

Browse files
authored
Remove use of try-with-resources. (#388)
1 parent 7af41a0 commit 002de4f

File tree

2 files changed

+71
-47
lines changed

2 files changed

+71
-47
lines changed

transport/transport-backend-cct/src/main/java/com/google/android/datatransport/cct/CctTransportBackend.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,31 @@ private BackendResponse doSend(BatchedLogRequest requestBody) throws IOException
202202
connection.setRequestMethod("POST");
203203
connection.setRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING);
204204
connection.setRequestProperty(CONTENT_TYPE_HEADER_KEY, PROTOBUF_CONTENT_TYPE);
205-
try (WritableByteChannel channel = Channels.newChannel(connection.getOutputStream())) {
205+
206+
WritableByteChannel channel = Channels.newChannel(connection.getOutputStream());
207+
try {
206208
ByteArrayOutputStream output = new ByteArrayOutputStream();
207-
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(output)) {
209+
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(output);
210+
211+
try {
208212
requestBody.writeTo(gzipOutputStream);
213+
} finally {
214+
gzipOutputStream.close();
209215
}
210216
channel.write(ByteBuffer.wrap(output.toByteArray()));
211217
int responseCode = connection.getResponseCode();
212218
LOGGER.info("Status Code: " + responseCode);
213219

214220
long nextRequestMillis;
215-
try (InputStream inputStream = connection.getInputStream()) {
221+
InputStream inputStream = connection.getInputStream();
222+
try {
216223
try {
217224
nextRequestMillis = LogResponse.parseFrom(inputStream).getNextRequestWaitMillis();
218225
} catch (InvalidProtocolBufferException e) {
219226
return BackendResponse.fatalError();
220227
}
228+
} finally {
229+
inputStream.close();
221230
}
222231
if (responseCode == 200) {
223232
return BackendResponse.ok(nextRequestMillis);
@@ -226,6 +235,8 @@ private BackendResponse doSend(BatchedLogRequest requestBody) throws IOException
226235
} else {
227236
return BackendResponse.fatalError();
228237
}
238+
} finally {
239+
channel.close();
229240
}
230241
}
231242

transport/transport-runtime/src/main/java/com/google/android/datatransport/runtime/scheduling/persistence/SQLiteEventStore.java

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,21 @@ private long ensureTransportContext(SQLiteDatabase db, TransportContext transpor
127127

128128
@Nullable
129129
private Long getTransportContextId(SQLiteDatabase db, TransportContext transportContext) {
130-
try (Cursor cursor =
130+
return tryWithCursor(
131131
db.query(
132132
"transport_contexts",
133133
new String[] {"_id"},
134134
"backend_name = ?",
135135
new String[] {transportContext.getBackendName()},
136136
null,
137137
null,
138-
null)) {
139-
if (!cursor.moveToNext()) {
140-
return null;
141-
}
142-
return cursor.getLong(0);
143-
}
138+
null),
139+
cursor -> {
140+
if (!cursor.moveToNext()) {
141+
return null;
142+
}
143+
return cursor.getLong(0);
144+
});
144145
}
145146

146147
@Override
@@ -184,19 +185,20 @@ private static String toIdList(Iterable<PersistedEvent> events) {
184185

185186
@Override
186187
public long getNextCallTime(TransportContext transportContext) {
187-
try (Cursor cursor =
188+
return tryWithCursor(
188189
getDb()
189190
.rawQuery(
190191
"SELECT next_request_ms FROM transport_contexts WHERE backend_name = ? and priority = ?",
191192
new String[] {
192193
transportContext.getBackendName(),
193194
String.valueOf(transportContext.getPriority().ordinal())
194-
})) {
195-
if (cursor.moveToNext()) {
196-
return cursor.getLong(0);
197-
}
198-
}
199-
return 0;
195+
}),
196+
cursor -> {
197+
if (cursor.moveToNext()) {
198+
return cursor.getLong(0);
199+
}
200+
return 0L;
201+
});
200202
}
201203

202204
@Override
@@ -207,13 +209,12 @@ public boolean hasPendingEventsFor(TransportContext transportContext) {
207209
if (contextId == null) {
208210
return false;
209211
}
210-
try (Cursor cursor =
212+
return tryWithCursor(
211213
getDb()
212214
.rawQuery(
213215
"SELECT 1 FROM events WHERE context_id = ? LIMIT 1",
214-
new String[] {contextId.toString()})) {
215-
return cursor.moveToNext();
216-
}
216+
new String[] {contextId.toString()}),
217+
Cursor::moveToNext);
217218
});
218219
}
219220

@@ -264,7 +265,7 @@ private List<PersistedEvent> loadEvents(SQLiteDatabase db, TransportContext tran
264265
return events;
265266
}
266267

267-
try (Cursor cursor =
268+
tryWithCursor(
268269
db.query(
269270
"events",
270271
new String[] {"_id", "transport_name", "timestamp_ms", "uptime_ms", "payload"},
@@ -273,19 +274,21 @@ private List<PersistedEvent> loadEvents(SQLiteDatabase db, TransportContext tran
273274
null,
274275
null,
275276
null,
276-
String.valueOf(config.getLoadBatchSize()))) {
277-
while (cursor.moveToNext()) {
278-
long id = cursor.getLong(0);
279-
EventInternal event =
280-
EventInternal.builder()
281-
.setTransportName(cursor.getString(1))
282-
.setEventMillis(cursor.getLong(2))
283-
.setUptimeMillis(cursor.getLong(3))
284-
.setPayload(cursor.getBlob(4))
285-
.build();
286-
events.add(PersistedEvent.create(id, transportContext, event));
287-
}
288-
}
277+
String.valueOf(config.getLoadBatchSize())),
278+
cursor -> {
279+
while (cursor.moveToNext()) {
280+
long id = cursor.getLong(0);
281+
EventInternal event =
282+
EventInternal.builder()
283+
.setTransportName(cursor.getString(1))
284+
.setEventMillis(cursor.getLong(2))
285+
.setUptimeMillis(cursor.getLong(3))
286+
.setPayload(cursor.getBlob(4))
287+
.build();
288+
events.add(PersistedEvent.create(id, transportContext, event));
289+
}
290+
return null;
291+
});
289292
return events;
290293
}
291294

@@ -301,25 +304,27 @@ private Map<Long, Set<Metadata>> loadMetadata(SQLiteDatabase db, List<PersistedE
301304
}
302305
whereClause.append(')');
303306

304-
try (Cursor cursor =
307+
tryWithCursor(
305308
db.query(
306309
"event_metadata",
307310
new String[] {"event_id", "name", "value"},
308311
whereClause.toString(),
309312
null,
310313
null,
311314
null,
312-
null)) {
313-
while (cursor.moveToNext()) {
314-
long eventId = cursor.getLong(0);
315-
Set<Metadata> currentSet = metadataIndex.get(eventId);
316-
if (currentSet == null) {
317-
currentSet = new HashSet<>();
318-
metadataIndex.put(eventId, currentSet);
319-
}
320-
currentSet.add(new Metadata(cursor.getString(1), cursor.getString(2)));
321-
}
322-
}
315+
null),
316+
cursor -> {
317+
while (cursor.moveToNext()) {
318+
long eventId = cursor.getLong(0);
319+
Set<Metadata> currentSet = metadataIndex.get(eventId);
320+
if (currentSet == null) {
321+
currentSet = new HashSet<>();
322+
metadataIndex.put(eventId, currentSet);
323+
}
324+
currentSet.add(new Metadata(cursor.getString(1), cursor.getString(2)));
325+
}
326+
return null;
327+
});
323328
return metadataIndex;
324329
}
325330

@@ -436,6 +441,14 @@ private long getPageCount() {
436441
return getDb().compileStatement("PRAGMA page_count").simpleQueryForLong();
437442
}
438443

444+
private static <T> T tryWithCursor(Cursor c, Function<Cursor, T> function) {
445+
try {
446+
return function.apply(c);
447+
} finally {
448+
c.close();
449+
}
450+
}
451+
439452
private static class OpenHelper extends SQLiteOpenHelper {
440453
// TODO: when we do schema upgrades in the future we need to make sure both downgrades and
441454
// upgrades work as expected, e.g. `up+down+up` is equivalent to `up`.

0 commit comments

Comments
 (0)