Clojure support for java.nio.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Clojure support for java.nio.

Extends’s input-stream, output-stream, and copy functions to java.nio classes.

Defines new coercion functions buffer, byte-buffer, char-buffer, double-buffer, float-buffer, int-buffer, long-buffer, short-buffer, channel, readable-channel, and writable-channel. These functions are implemented for many of the Classes you would expect.

Also defines a function mmap to memory map a file.

Additionally, there are three functions buffer-seq, buffer-nth, and buffer-to-array intended to make it a little easier to integrate java.nio classes into Clojure. Hopefully, these will become obsolete by rolling their functionality into the clojure.core.

For additional functions to work with ByteBuffers, you can also use

Dependency information

Leiningen dependency information:

[nio "1.0.3"]

Maven dependency information:



The recommended way to use nio at the REPL is:

(require '[nio.core :as nio])

And similarly for your ns imports.

There are coercion functions for java.nio.Buffer and java.nio.channels.Channel types:

(nio/char-buffer (nio/byte-buffer (into-array Byte/TYPE [0 102 111 111])))
(nio/char-buffer "a string")
(nio/channel (io/file "/home/paul/.bashrc"))
(nio/readable-channel (nio/buffer (into-array Byte/TYPE [1 2 3])))

There are two convenience functions for getting and setting the byte order of a ByteBuffer:

(byte-order (ByteBuffer/allocate 32))
(set-byte-order! (ByteBuffer/allocate 32) :little-endian)’s functions work as you’d expect:

(io/input-stream (nio/channel "/home/paul/.bashrc"))
(io/input-stream (nio/byte-buffer (.getBytes "/home/paul/.bashrc")))
(io/copy (nio/channel "/home/paul/.bashrc") (nio/channel "/home/paul/.bashrc.bak"))

Memory mapping a file

(mmap "/home/paul/.bashrc")

You can also call mmap on a File or FileChannel. The File or FileChannel may be closed without affecting the mapping.


Copyright © 2012-2014 Paul Stadig.

Distributed under the Eclipse Public License, the same as