Skip to content

Commit e27d749

Browse files
authored
Merge pull request #3 from Archinamon/feature/copy-move-methods
implemented copy/move methods
2 parents 905874a + c5c6560 commit e27d749

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "me.archinamon"
9-
version = "1.3.0"
9+
version = "1.3.1"
1010

1111
val isRunningInIde: Boolean = System.getProperty("idea.active")
1212
?.toBoolean() == true
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package me.archinamon.fileio
2+
3+
fun File.copyTo(dest: String, overwrite: Boolean) {
4+
this.copyTo(File(dest), overwrite)
5+
}
6+
7+
fun File.copyTo(dest: File, overwrite: Boolean) {
8+
if (this.isDirectory()) {
9+
throw UnsupportedOperationException("Moving/copying directories directly not allowed!")
10+
}
11+
12+
this.readBytes().let { bytes ->
13+
dest.apply {
14+
if (exists() && !overwrite) {
15+
throw IllegalFileAccess(dest.getAbsolutePath(), "Already exists and not allowed to be overwritten!")
16+
}
17+
18+
if (!exists()) {
19+
createNewFile()
20+
}
21+
22+
writeBytes(bytes)
23+
}
24+
}
25+
}
26+
27+
fun File.moveTo(dest: String, overwrite: Boolean) {
28+
this.copyTo(dest, overwrite)
29+
this.delete()
30+
}
31+
32+
fun File.moveTo(dest: File, overwrite: Boolean) {
33+
this.copyTo(dest, overwrite)
34+
this.delete()
35+
}
36+
37+
expect fun File.writeBytes(bytes: ByteArray)

src/jsMain/kotlin/File.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,7 @@ actual fun File.appendText(text: String) {
9898
actual fun File.writeText(text: String) {
9999
innerFile = JsFile(text.encodeToByteArray().toTypedArray(), innerFile.name)
100100
}
101+
102+
actual fun File.writeBytes(bytes: ByteArray) {
103+
innerFile = JsFile(bytes.toTypedArray(), innerFile.name)
104+
}

src/jvmMain/kotlin/File.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package me.archinamon.fileio
44
import java.net.URLConnection
55
import java.nio.charset.Charset
66
import kotlin.io.readBytes as kReadBytes
7+
import kotlin.io.writeBytes as kWriteBytes
78

89
actual typealias File = java.io.File
910

@@ -14,6 +15,8 @@ actual fun File.readBytes() = kReadBytes()
1415

1516
actual fun File.readText() = readText(Charset.defaultCharset())
1617

18+
actual fun File.writeBytes(bytes: ByteArray) = kWriteBytes(bytes)
19+
1720
actual fun File.appendText(text: String) = appendText(text, Charset.defaultCharset())
1821

1922
actual fun File.writeText(text: String) = writeText(text, Charset.defaultCharset())

src/mingwX64Main/kotlin/File.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ actual fun File.readBytes(): ByteArray = memScoped {
302302
}
303303
}
304304

305+
actual fun File.writeBytes(bytes: ByteArray) {
306+
// no need to use pinning or memscope, cause it's inside the method already does
307+
writeBytes(bytes, GENERIC_WRITE)
308+
}
309+
305310
actual fun File.readText(): String {
306311
return readBytes().toKString()
307312
}

src/posixMain/kotlin/File.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ actual fun File.readBytes(): ByteArray {
249249
}
250250
}
251251

252+
actual fun File.writeBytes(bytes: ByteArray) {
253+
// no need to use pinning or memscope, cause it's inside the method already does
254+
writeBytes(bytes, O_RDWR, bytes.size.convert(), Byte.SIZE_BYTES.convert())
255+
}
256+
252257
actual fun File.readText(): String {
253258
return readBytes().toKString()
254259
}

0 commit comments

Comments
 (0)