Skip to content

Commit 4bdb475

Browse files
authored
Revert "minimize the number of times of open/close file (#32)" (#33)
This reverts commit bd05448.
1 parent bd05448 commit 4bdb475

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed

core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.segment.analytics.kotlin.core.utilities
22

3-
import kotlinx.coroutines.runBlocking
43
import java.io.File
54
import java.io.FileOutputStream
65
import java.time.Instant
@@ -40,13 +39,10 @@ class EventsFileManager(
4039

4140
init {
4241
createDirectory(directory)
43-
registerShutdownHook()
4442
}
4543

4644
private val fileIndexKey = "segment.events.file.index.$writeKey"
4745

48-
private var os: FileOutputStream? = null
49-
5046
companion object {
5147
const val MAX_FILE_SIZE = 475_000 // 475KB
5248
}
@@ -124,8 +120,6 @@ class EventsFileManager(
124120
val contents = """],"sentAt":"${Instant.now()}"}"""
125121
writeToFile(contents.toByteArray(), file)
126122
file.renameTo(File(directory, file.nameWithoutExtension))
127-
os?.close()
128-
os = null
129123
incrementFileIndex()
130124
}
131125

@@ -138,22 +132,10 @@ class EventsFileManager(
138132
// Atomic write to underlying file
139133
// TODO make atomic
140134
private fun writeToFile(content: ByteArray, file: File) {
141-
os = os ?: FileOutputStream(file, true)
142-
os?.run {
143-
write(content)
144-
flush()
145-
}
146-
}
147-
148-
private fun registerShutdownHook() {
149-
// close the stream if the app shuts down
150-
Runtime.getRuntime().addShutdownHook(object : Thread() {
151-
override fun run() {
152-
runBlocking {
153-
os?.close()
154-
}
155-
}
156-
})
135+
val os = FileOutputStream(file, true)
136+
os.write(content)
137+
os.flush()
138+
os.close()
157139
}
158140
}
159141

0 commit comments

Comments
 (0)