Skip to content

Instantly share code, notes, and snippets.

@cp-hardik-p
Last active September 30, 2022 12:22
Show Gist options
  • Save cp-hardik-p/eed89959fc7a2bfded4636de1abc01ec to your computer and use it in GitHub Desktop.
Save cp-hardik-p/eed89959fc7a2bfded4636de1abc01ec to your computer and use it in GitHub Desktop.
@Throws(IOException::class)
private fun ensureLogSize(logFile: File) {
if (logFile.length() < maxLogSize) return
// We remove first 25% part of logs
val startIndex = logFile.length() / 4
val randomAccessFile = RandomAccessFile(logFile, "r")
randomAccessFile.seek(startIndex)
val into = ByteArrayOutputStream()
val buf = ByteArray(4096)
var n: Int
while (true) {
n = randomAccessFile.read(buf)
if (n < 0) break
into.write(buf, 0, n)
}
randomAccessFile.close()
val outputStream = FileOutputStream(logFile)
into.writeTo(outputStream)
outputStream.close()
into.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment