Skip to content

Commit

Permalink
Close streams after use (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxvd authored Jul 15, 2024
1 parent fd7a016 commit e90f0b9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ExportService : IntentService("Export Service") {
continue
}
val outWriter = BufferedWriter(FileWriter(entriesFile))
outWriter.apply {
outWriter.use { writer ->
var firstLine = true
val entries: MutableList<MonitoringEntry> = ArrayList()
for (formEntry in formEntries) {
Expand All @@ -134,16 +134,16 @@ class ExportService : IntentService("Export Service") {
entry.data.keys.retainAll(normalizedKeys)
val lines = convertToCsvLines(entry.data)
if (firstLine) {
write(commonLines[0])
write(CSVStrategy.DEFAULT.delimiter.code)
write(lines[0])
newLine()
writer.write(commonLines[0])
writer.write(CSVStrategy.DEFAULT.delimiter.code)
writer.write(lines[0])
writer.newLine()
firstLine = false
}
write(commonLines[1])
write(CSVStrategy.DEFAULT.delimiter.code)
write(lines[1])
newLine()
writer.write(commonLines[1])
writer.write(CSVStrategy.DEFAULT.delimiter.code)
writer.write(lines[1])
writer.newLine()
}
}
}
Expand All @@ -156,12 +156,12 @@ class ExportService : IntentService("Export Service") {
private fun convertToCsvLines(data: HashMap<String, String>): Array<String> {
val prepared = CsvPreparer.prepareCsvLine(data)
val memory = StringWriter()
memory.apply {
val csvWriter = CSVWriterBuilder<Array<String>>(this).strategy(CSVStrategy.DEFAULT)
memory.use {
val csvWriter = CSVWriterBuilder<Array<String>>(it).strategy(CSVStrategy.DEFAULT)
.entryConverter(SmartBirdsCSVEntryConverter()).build()
csvWriter.write(prepared.keys)
csvWriter.write(prepared.values)
flush()
it.flush()
}
val commonData = memory.buffer.toString()
return commonData.split(System.lineSeparator()).toTypedArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object KmlUtils {
val cursor: Cursor? =
context.contentResolver
.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)
cursor.let {
cursor.use {
if (it != null && it.moveToFirst()) {
result = it.getString(0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class MonitoringUtils {
val file = File(createMonitoringDir(context, monitoring), "track.gpx")
try {
val osw: Writer = BufferedWriter(FileWriter(file, true))
GpxWriter(osw).writeFooter()
osw.use {
GpxWriter(it).writeFooter()
}
} catch (e: IOException) {
Reporting.logException(e)
Toast.makeText(context, "Could not write to track.gpx!", Toast.LENGTH_SHORT).show()
Expand All @@ -55,7 +57,7 @@ class MonitoringUtils {
val file = File(createMonitoringDir(context, monitoring), "track.gpx")
try {
val osw = OutputStreamWriter(BufferedOutputStream(FileOutputStream(file, false)))
osw.let {
osw.use {
val writer = GpxWriter(it)
writer.writeHeader()
}
Expand Down

0 comments on commit e90f0b9

Please sign in to comment.