Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added writeUtf8 and writeUtf8Lines #3167

Merged
merged 8 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions io/js/src/main/scala/fs2/io/file/FilesPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ private[fs2] trait FilesCompanionPlatform {
override def isSameFile(path1: Path, path2: Path): F[Boolean] =
F.pure(path1.absolute == path2.absolute)

override def lineSeparator: String = facade.os.EOL

override def list(path: Path): Stream[F, Path] =
Stream
.bracket(F.fromPromise(F.delay(facade.fs.promises.opendir(path.toString))))(dir =>
Expand Down
4 changes: 4 additions & 0 deletions io/js/src/main/scala/fs2/io/internal/facade/os.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ package object os {
@JSImport("os", "networkInterfaces")
private[io] def networkInterfaces(): js.Dictionary[js.Array[NetworkInterfaceInfo]] = js.native

@js.native
@JSImport("os", "EOL")
private[io] def EOL: String = js.native

}

package os {
Expand Down
2 changes: 2 additions & 0 deletions io/jvm-native/src/main/scala/fs2/io/file/FilesPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ private[file] trait FilesCompanionPlatform {
def isSameFile(path1: Path, path2: Path): F[Boolean] =
Sync[F].blocking(JFiles.isSameFile(path1.toNioPath, path2.toNioPath))

def lineSeparator: String = System.lineSeparator()

def list(path: Path): Stream[F, Path] =
_runJavaCollectionResource[JStream[JPath]](
Sync[F].blocking(JFiles.list(path.toNioPath)),
Expand Down
51 changes: 37 additions & 14 deletions io/shared/src/main/scala/fs2/io/file/Files.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
/** Returns true if the supplied paths reference the same file. */
def isSameFile(path1: Path, path2: Path): F[Boolean]

/** Returns the line separator for the specific OS */
def lineSeparator: String

/** Gets the contents of the specified directory. */
def list(path: Path): Stream[F, Path]

Expand Down Expand Up @@ -269,7 +272,10 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
def readAll(path: Path, chunkSize: Int, flags: Flags): Stream[F, Byte]

/** Returns a `ReadCursor` for the specified path, using the supplied flags when opening the file. */
def readCursor(path: Path, flags: Flags): Resource[F, ReadCursor[F]]
def readCursor(path: Path, flags: Flags): Resource[F, ReadCursor[F]] =
open(path, flags.addIfAbsent(Flag.Read)).map { fileHandle =>
ReadCursor(fileHandle, 0L)
}

/** Reads a range of data synchronously from the file at the specified path.
* `start` is inclusive, `end` is exclusive, so when `start` is 0 and `end` is 2,
Expand All @@ -278,10 +284,10 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
def readRange(path: Path, chunkSize: Int, start: Long, end: Long): Stream[F, Byte]

/** Reads all bytes from the file specified and decodes them as a utf8 string. */
def readUtf8(path: Path): Stream[F, String]
def readUtf8(path: Path): Stream[F, String] = readAll(path).through(text.utf8.decode)

/** Reads all bytes from the file specified and decodes them as utf8 lines. */
def readUtf8Lines(path: Path): Stream[F, String]
def readUtf8Lines(path: Path): Stream[F, String] = readUtf8(path).through(text.lines)

/** Returns the real path i.e. the actual location of `path`.
* The precise definition of this method is implementation dependent but in general
Expand Down Expand Up @@ -409,6 +415,34 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
limit: Long,
flags: Flags
): Pipe[F, Byte, Nothing]

/** Writes to the specified file as an utf8 string.
*
* The file is created if it does not exist and is truncated.
* Use `writeUtf8(path, Flags.Append)` to append to the end of
* the file, or pass other flags to further customize behavior.
*/
def writeUtf8(path: Path): Pipe[F, String, Nothing] = writeUtf8(path, Flags.Write)

/** Writes to the specified file as an utf8 string using
* the specified flags to open the file.
*/
def writeUtf8(path: Path, flags: Flags): Pipe[F, String, Nothing] = in =>
in.through(text.utf8.encode).through(writeAll(path, flags))
armanbilge marked this conversation as resolved.
Show resolved Hide resolved

/** Writes each string to the specified file as utf8 lines.
*
* The file is created if it does not exist and is truncated.
* Use `writeUtf8Lines(path, Flags.Append)` to append to the end
* of the file, or pass other flags to further customize behavior.
*/
def writeUtf8Lines(path: Path): Pipe[F, String, Nothing] = writeUtf8Lines(path, Flags.Write)

/** Writes each string to the specified file as utf8 lines
* using the specified flags to open the file.
*/
def writeUtf8Lines(path: Path, flags: Flags): Pipe[F, String, Nothing] = in =>
in.flatMap(s => Stream[F, String](s, lineSeparator)).through(writeUtf8(path, flags))
}

object Files extends FilesCompanionPlatform {
Expand All @@ -419,22 +453,11 @@ object Files extends FilesCompanionPlatform {
cursor.readAll(chunkSize).void.stream
}

def readCursor(path: Path, flags: Flags): Resource[F, ReadCursor[F]] =
open(path, flags.addIfAbsent(Flag.Read)).map { fileHandle =>
ReadCursor(fileHandle, 0L)
}

def readRange(path: Path, chunkSize: Int, start: Long, end: Long): Stream[F, Byte] =
Stream.resource(readCursor(path, Flags.Read)).flatMap { cursor =>
cursor.seek(start).readUntil(chunkSize, end).void.stream
}

def readUtf8(path: Path): Stream[F, String] =
readAll(path).through(text.utf8.decode)

def readUtf8Lines(path: Path): Stream[F, String] =
readUtf8(path).through(text.lines)

def tail(
path: Path,
chunkSize: Int,
Expand Down
31 changes: 31 additions & 0 deletions io/shared/src/test/scala/fs2/io/file/FilesSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,37 @@ class FilesSuite extends Fs2IoSuite with BaseFileSuite {
.compile
.drain
}

test("writeUtf8") {
Stream
.resource(tempFile)
.flatMap { path =>
Stream("Hello", " world!")
.covary[IO]
.through(Files[IO].writeUtf8(path)) ++ Files[IO]
.readAll(path)
.through(text.utf8.decode)
}
.compile
.foldMonoid
.assertEquals("Hello world!")
}

test("writeUtf8Lines") {
Stream
.resource(tempFile)
.flatMap { path =>
Stream("foo", "bar")
.covary[IO]
.through(Files[IO].writeUtf8Lines(path)) ++ Files[IO]
.readUtf8(path)
}
.compile
.foldMonoid
.assertEquals("""|foo
|bar
|""".stripMargin)
}
}

group("tail") {
Expand Down