Add java.nio.Path extensions to stdlib-jdk7

This PR adds most extensions on `java.io.File` in `kotlin.io` to `java.nio.Path`. This includes extensions from `FileReadWrite.kt`, `Utils.kt`, and `FileTreeWalk.kt`.

I attempted to keep the implementations, documentation, and tests as similar as possible to the existing implementations.

I am happy to add, remove, or move to separate PRs any of the functions of this PR.

### `File` extensions that were not added to `Path`

##### `createTempDir`, `createTempFile`

These functions have no `File` parameters, so can't be overloaded. Equivalents exist as `Files.createTempFile()` and `Files.createTempDirectory()`.

##### `startsWith`, `endsWith`, `normalize`, `resolve`, `resolveSibling`

These exist as member functions on `Path`

##### `relativeTo`, `relativeToOrNull`, `relativeToOrSelf`, `toRelativeString`, `toRelativeStringOrNull`

This functionality exists as the `Path.relativize` member function, which is equivalent to `relativeTo`, but with the receiver and parameter flipped. `foo.relativeTo(bar)` is equivalent to `bar.relativize(foo)`. We could potentially add a `relativizeOrNull` extension to make that pattern simpler.

##### `isRooted`

`Path` has a `root` method, so `isRooted` is equivalent to `root != null`

### New extensions

All of the simple boolean attribute checks from `java.nio.Files` were added as extensions on `Path`. These extensions are used commonly enough that it seems worth supporting them. This functionality for `File` is implemented as member methods.

The following `Path` extensions were added:

- `exists`
- `isDirectory`
- `isExecutable`
- `isFile`
- `isHidden`
- `isReadable`
- `isSameFile`
- `isSymbolicLink`
- `isWritable`

Some of these extensions take options that are forwarded to their `Files` method, so all of the extensions were implemented as functions rather than properties for consistency.

Additionally, `Path.listFiles` was added to match the `File.listFiles` method. One motivation for its addition was that it's used several times in the implementation of other file extensions. The way to list directory contents with `java.nio` is via `Files.newDirectoryStream()`, which returns an iterable object that must be closed to avoid leaking resources. It's difficult to use correctly with functions like `map` and `filter`, so this extension was added as a simpler, less error-prone alternative.

### Other changes

I added overloads of several of the read-write that take `OpenOptions` to expose the greater control that `java.nio` introduces. For example, you can use `printWriter(APPEND)` to create a `PrintWriter` that doesn't delete the contents of an existing file.

All the new extensions throw exceptions (such as `NoSuchFileException`) from `java.nio` rather than the copies from `kotlin.io`. The `kotlin.io` copies take `File` objects as parameters, and so aren't compatible with `Path`s.

### Address review comments

- Move varargs parameters to the last position
- Remove PathTreeWalk

#KT-19192
This commit is contained in:
AJ
2020-04-17 09:56:52 -07:00
committed by Ilya Gorbunov
parent 008da87160
commit 03cc0bf6aa
5 changed files with 1003 additions and 0 deletions
@@ -0,0 +1,80 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.jdk7.test
import java.nio.file.Files
import java.nio.file.StandardOpenOption
import java.util.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class PathReadWriteTest {
@Test
fun testAppendText() {
val file = Files.createTempFile(null, null)
file.writeText("Hello\n")
file.appendText("World\n")
file.appendText("Again")
assertEquals("Hello\nWorld\nAgain", file.readText())
assertEquals(listOf("Hello", "World", "Again"), file.readLines(Charsets.UTF_8))
file.toFile().deleteOnExit()
}
@Test
fun file() {
val file = Files.createTempFile(null, null)
val writer = file.outputStream().writer().buffered()
writer.write("Hello")
writer.newLine()
writer.write("World")
writer.close()
file.forEachBlock { arr: ByteArray, size: Int ->
assertTrue(size in 11..12, size.toString())
assertTrue(arr.contains('W'.toByte()))
}
val list = ArrayList<String>()
file.forEachLine(StandardOpenOption.READ, charset = Charsets.UTF_8) {
list.add(it)
}
assertEquals(arrayListOf("Hello", "World"), list)
assertEquals(arrayListOf("Hello", "World"), file.readLines())
file.useLines {
assertEquals(arrayListOf("Hello", "World"), it.toList())
}
val text = file.inputStream().reader().readText()
assertTrue(text.contains("Hello"))
assertTrue(text.contains("World"))
file.writeText("")
var c = 0
file.forEachLine { c++ }
assertEquals(0, c)
file.writeText(" ")
file.forEachLine { c++ }
assertEquals(1, c)
file.writeText(" \n")
c = 0
file.forEachLine { c++ }
assertEquals(1, c)
file.writeText(" \n ")
c = 0
file.forEachLine { c++ }
assertEquals(2, c)
file.toFile().deleteOnExit()
}
}