Okio Documentation

repository·master·Indexed 27 days ago

https://github.com/lysine-dev/okio

Okio is a library that complements standard Java I/O (java.io and java.nio) to simplify data access, storage, and processing. It provides a cross-platform Path class and FileSystem abstraction supporting Android, Java, Linux, UNIX, Windows, and Node.js. Key features include the okio-assetfilesystem for Android assets, okio-fakefilesystem for in-memory testing, and multiplatform support for Buffer, ByteString, Source, and Sink.

Tokens
7.5K
Snippets
22
Records
39
Agent score
94%

What's inside Okio

  1. Overview of Okio

    master
    Okio is a library designed to complement java.io and java.nio. It provides a more efficient and easier way to access, store, and process data. Originally developed as a component of OkHttp, it is a well-tested library suitable for various data processing tasks in Java and Kotlin environments.
  2. Check Okio platform availability for features

    master

    Okio feature availability varies by platform:

    FeaturePlatforms
    Core (Buffer, ByteString, Source, Sink)All platforms
    File SystemAll platforms (requires Node.js for JavaScript)
    Hashing (MD5, SHA-1, SHA-256, SHA-512, HMAC)All platforms (uses JVM built-ins on JVM)
    Compression (Deflater, Inflater, Gzip)JVM and native platforms
    Concurrency (Pipe, Throttler)JVM-only
    TimeoutsAll platforms (implementation is primarily useful on JVM)
  3. Use multiplatform Path manipulation

    master
    Okio's Path class provides cross-platform path handling. It supports both Windows-style (e.g., C:\autoexec.bat) and UNIX-style (e.g., /etc/passwd) paths. You can manipulate Windows paths on UNIX systems and vice versa. The FileSystem abstraction handles the underlying platform-specific APIs (Android, Java, Linux, UNIX, Windows, or Node.js).
  4. Understand the Linux User API symbols in Okio

    master
    The okio/src/linuxMain/headers directory contains files from the Linux kernel user API. These files are used as symbols to build Okio for Linux targets. The project uses these symbols under the 'GPL-2.0 WITH Linux-syscall-note' license, which allows building against these symbols without the resulting work being considered a derived work of the Linux kernel.
  5. Test file system operations with FakeFileSystem

    master

    To make tests faster and more reliable, swap FileSystem.SYSTEM with FakeFileSystem. This allows you to manipulate a virtual file system in memory without actual disk I/O. You can also use ForwardingFileSystem to inject faults (like simulating a full disk) to test error handling.

    val fileSystem = FakeFileSystem()
    val userHome = "/Users/sandy".toPath()
    val gitConfig = userHome / ".gitconfig"
    
    fileSystem.createDirectories(userHome)
    val original = """
        |[user]
        |  email = sandy@example.com
        |".trimMargin()
    fileSystem.write(gitConfig) { writeUtf8(original) }
    
    // Example usage with a hypothetical fixer
    GitConfigFixer(fileSystem).fix(userHome)
    
    val expected = """
      |[user]
      |  email = sandy@example.com
    |[diff]
      |  renames = true
      |  indentHeuristic = on
      |".trimIndent()
    assertEquals(expected, fileSystem.read(gitConfig) { readUtf8() })
  6. Read and write files using FileSystem

    master

    Use FileSystem.SYSTEM to perform concise reading and writing operations. Paths should be created using the .toPath() extension on a String. Reading and writing are performed within blocks that provide access to Okio buffers.

    val path = "README.md".toPath()
    
    val readmeContent = FileSystem.SYSTEM.read(path) {
      readUtf8()
    }
    
    val updatedContent = readmeContent.replace("red", "blue")
    
    FileSystem.SYSTEM.write(path) {
      writeUtf8(updatedContent)
    }
  7. Stream Android test logs with logcat

    master

    Use adb logcat with specific filters to monitor test execution and debug failures. The following command filters for relevant tags including TestRunner, TaskRunner, and error/fatal levels for specific components.

    adb logcat '*:E' TestRunner:D TaskRunner:D GnssHAL_GnssInterface:F DeviceStateChecker:F memtrack:F