Coder Social home page Coder Social logo

path-utils's Introduction

paths

Serializable wrapper for java.nio.file.Path

Build Status Coverage Status

org.hammerlab:paths_2.11 on Maven Central org.hammerlab:paths_2.12 on Maven Central

Filesystem-path convenience methods and bug-fixes:

import hammerlab.path._
val path = Path("")    // current directory; URIs and Strings accepted
path.exists            // true
path / 'src list       // list children of src dir: Iterator(src/main,src/test)

Examples

Existence / Child-resolution

val src = path / 'src  // "src" subdir
src.exists             // true
path / 'foo exists     // false

Directory-traversal

(path / 'src / 'test walk) filter (_.isFile)
// Iterator(
//   src/test/resources/log4j.properties,
//   src/test/resources/META-INF/services/java.nio.file.spi.FileSystemProvider,
//   src/test/scala/org/hammerlab/paths/PathTest.scala
// )

IO

Self-explanatory methods:

  • def inputStream: InputStream
  • def outputStream: OutputStream
  • def read: String
  • def readBytes: Array[Byte]
  • def write(String)
  • def writeLines(Iterable[String])
  • def lines: Iterator[String]
  • def size: Long

Serializability

Paths are Serializable, delegating to the SerializablePath proxy, which serializes them as URIs.

JSR203 FileSystemProvider improvements

Interacting with Java NIO FileSystemProviders from Scala is broken; see scala/bug#10247.

Paths from this repo have hooks to lazily initialize and tweak the FileSystemProvider initialization code to resolve these problems; see FileSystems.scala.

There is also fix for a missing newByteChannel implementation in the JRE's default implementation of JarFileSystemProvider; use of Paths replaces it with a custom JarFileSystemProvider with this issue resolved.

As an example, try downloading the Google Cloud Storage NIO connector JAR, and querying for the existence and size of a public file:

wget -O lib/gcs-nio.jar http://search.maven.org/remotecontent?filepath=com/google/cloud/google-cloud-nio/0.28.0-alpha/google-cloud-nio-0.28.0-alpha-shaded.jar
sbt console
import hammerlab.path._
val index = Path("gs://gcp-public-data-landsat/index.csv.gz")

index.exists  // true
index.size    // 476118237

In contrast, trying the equivalent using vanilla Java NIO paths fails:

import java._, nio.file._, net._
val uri = new URI("gs://gcp-public-data-landsat/index.csv.gz")

Paths.get(uri)
// java.nio.file.FileSystemNotFoundException: Provider "gs" not installed
//   at java.nio.file.Paths.get(Paths.java:147)
//   ... 42 elided

(test this in a fresh sbt console, as initializing a hammerlab Path fixes the providers in that JVM)

path-utils's People

Contributors

ryan-williams avatar

Stargazers

Pishen Tsai avatar Selman avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.