Removed FileChannel usage

This commit is contained in:
Robinson 2022-03-07 22:19:14 +01:00
parent 8cc7b2070c
commit 6e5313aa24
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C

View File

@ -30,7 +30,6 @@ import java.io.InputStream
import java.io.PrintWriter import java.io.PrintWriter
import java.io.RandomAccessFile import java.io.RandomAccessFile
import java.io.Reader import java.io.Reader
import java.nio.channels.FileChannel
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.nio.file.DirectoryIteratorException import java.nio.file.DirectoryIteratorException
import java.nio.file.FileVisitResult import java.nio.file.FileVisitResult
@ -637,26 +636,13 @@ object FileUtil {
if (DEBUG) { if (DEBUG) {
System.err.println("Copying file: '$`in`' --> '$out'") System.err.println("Copying file: '$`in`' --> '$out'")
} }
var sourceChannel: FileChannel? = null
var destinationChannel: FileChannel? = null
try {
sourceChannel = FileInputStream(normalizedIn).channel
destinationChannel = FileOutputStream(normalizedout).channel
if (sourceChannel.size() == 0L) {
System.err.println("Source size is ZERO: $normalizedIn")
}
sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel) FileInputStream(normalizedIn).bufferedReader().use { reader ->
} finally { FileOutputStream(normalizedout, false).bufferedWriter().use { writer ->
try { reader.transferTo(writer)
sourceChannel?.close()
} catch (ignored: Exception) {
}
try {
destinationChannel?.close()
} catch (ignored: Exception) {
} }
} }
out.setLastModified(`in`.lastModified()) out.setLastModified(`in`.lastModified())
return out return out
} }
@ -665,33 +651,16 @@ object FileUtil {
* Copies the contents of file two onto the END of file one. * Copies the contents of file two onto the END of file one.
*/ */
fun concatFiles(one: File, two: File): File { fun concatFiles(one: File, two: File): File {
val normalizedOne = normalize(one)!!.absolutePath
val normalizedTwo = normalize(two)!!.absolutePath
if (DEBUG) { if (DEBUG) {
System.err.println("Concat'ing file: '$one' --> '$two'") System.err.println("Concat'ing file: '$one' --> '$two'")
} }
var channelOne: FileChannel? = null
var channelTwo: FileChannel? = null FileOutputStream(one, true).bufferedWriter().use { writer ->
try { FileInputStream(two).bufferedReader().use { reader ->
// open it in append mode reader.transferTo(writer)
channelOne = FileOutputStream(normalizedOne, true).channel
channelTwo = FileInputStream(normalizedTwo).channel
var size = two.length()
while (size > 0) {
size -= channelOne.transferFrom(channelTwo, 0, size)
}
} catch (ignored: Exception) {
ignored.printStackTrace()
} finally {
try {
channelOne?.close()
} catch (ignored: Exception) {
}
try {
channelTwo?.close()
} catch (ignored: Exception) {
} }
} }
one.setLastModified(System.currentTimeMillis()) one.setLastModified(System.currentTimeMillis())
return one return one
} }