• Stars
    star
    413
  • Rank 101,392 (Top 3 %)
  • Language
    Clojure
  • Created almost 11 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Rosetta stone for JVM byte representations

Clojars Project cljdoc badge CircleCI

Java has a lot of different ways to represent a stream of bytes. Depending on the author and age of a library, it might use byte[], InputStream, ByteBuffer, or ReadableByteChannel. If the bytes represent strings, there's also String, Reader, and CharSequence to worry about. Remembering how to convert between all of them is a thankless task, made that much worse by libraries which define their own custom representations, or composing them with Clojure's lazy sequences and stream representations.

This library is a Rosetta stone for all the byte representations Java has to offer, and gives you the freedom to forget all the APIs you never wanted to know in the first place. Complete documentation can be found here.

Usage

Leiningen:

[org.clj-commons/byte-streams "0.3.4"]

deps.edn:

org.clj-commons/byte-streams {:mvn/version "0.3.4"}

Converting types

To convert one byte representation to another, use convert:

clj-commons.byte-streams> (convert "abcd" java.nio.ByteBuffer)
#<HeapByteBuffer java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]>
clj-commons.byte-streams> (convert *1 String)
"abcd"

(convert data to-type options?) converts, if possible, the data from its current type to the destination type. This destination type can either be a Java class or a Clojure protocol. However, since there's no direct route from a string to a byte-buffer, under the covers convert is doing whatever it takes to get the desired type:

clj-commons.byte-streams> (conversion-path String java.nio.ByteBuffer)
([java.lang.String [B]
 [[B java.nio.ByteBuffer])

While we can't turn a string into a ByteBuffer, we can turn a string into a byte[], and byte[] into a ByteBuffer. When invoked, convert will choose the minimal path along the graph of available conversions. Common conversions are exposed via to-byte-buffer, to-byte-buffers, to-byte-array, to-input-stream, to-readable-channel, to-char-sequence, to-string, and to-line-seq.

Every type can exist either by itself, or as a sequence. For instance, we can create an InputStream representing an infinite number of repeated strings:

clj-commons.byte-streams> (to-input-stream (repeat "hello"))
#<InputStream byte_streams.InputStream@3962a02c>

And then we can turn that into a lazy sequence of ByteBuffers:

clj-commons.byte-streams> 
  (take 2 (convert *1
                   (seq-of java.nio.ByteBuffer)
                   {:chunk-size 128}))
(#<HeapByteBuffer java.nio.HeapByteBuffer[pos=0 lim=128 cap=128]>
 #<HeapByteBuffer java.nio.HeapByteBuffer[pos=0 lim=128 cap=128]>)

Notice that we describe a sequence of a type as (seq-of type), and that we've passed a map to convert describing the size of the ByteBuffers we want to create. Available options include:

  • :chunk-size - the size in bytes of each chunk when converting a stream into a lazy seq of discrete chunks, defaults to 4096
  • :direct? - whether any ByteBuffers which are created should be direct, defaults to false
  • :encoding - the character set for any strings we're encoding or decoding, defaults to "UTF-8"

To create a Manifold stream, use (stream-of type). To convert a core.async channel, convert it using manifold.stream/->source.

Custom conversions

While there are conversions defined for all common byte types, this can be extended to other libraries via def-conversion:

;; a conversion from byte-buffers to my-byte-representation
(def-conversion [ByteBuffer MyByteRepresentation]
  [buf options]
  (buffer->my-representation buf options))

;; everything that can be converted to a ByteBuffer is transitively fair game now
(convert "abc" MyByteRepresentation)

This mechanism can even be used for types unrelated to byte streams, if you're feeling adventurous.

Transfers

Simple conversions are useful, but sometimes we'll need to do more than just keep the bytes in memory. When you need to write bytes to a file, network socket, or other endpoints, you can use transfer.

clj-commons.byte-streams> (def f (File. "/tmp/salutations"))
#'clj-commons.byte-streams/f
clj-commons.byte-streams> (transfer "hello" f {:append? false})
nil
clj-commons.byte-streams> (to-string f)
"hello"

(transfer source sink options?) allows you pipe anything that can produce bytes into anything that can receive bytes, using the most efficient mechanism available. Custom transfer mechanisms can also be defined:

(def-transfer [InputStream MyByteSink]
  [stream sink options]
  (send-stream-to-my-sink stream sink))

Some utilities

print-bytes will print both hexadecimal and ascii representations of a collection of bytes:

clj-commons.byte-streams> (print-bytes (-> #'print-bytes meta :doc))
50 72 69 6E 74 73 20 6F  75 74 20 74 68 65 20 62      Prints out the b
79 74 65 73 20 69 6E 20  62 6F 74 68 20 68 65 78      ytes in both hex
20 61 6E 64 20 41 53 43  49 49 20 72 65 70 72 65       and ASCII repre
73 65 6E 74 61 74 69 6F  6E 73 2C 20 31 36 20 62      sentations, 16 b
79 74 65 73 20 70 65 72  20 6C 69 6E 65 2E            ytes per line.

(compare-bytes a b) will return a value which is positive if a is lexicographically first, zero if they're equal, and negative otherwise:

clj-commons.byte-streams> (compare-bytes "abc" "abd")
-1

bytes= will return true if two byte streams are equal, and false otherwise.

conversion-path returns all the intermediate steps in transforming one type to another, if one exists:

;; each element is a conversion pair of to/from
clj-commons.byte-streams> (conversion-path java.io.File String)
([java.io.File java.nio.channels.ReadableByteChannel]
 [#'byte-streams/ByteSource java.lang.CharSequence]
 [java.lang.CharSequence java.lang.String])

;; but if a conversion is impossible...
clj-commons.byte-streams> (conversion-path java.io.OutputStream java.io.InputStream)
nil

possible-conversions returns a list of possible conversion targets for a type.

clj-commons.byte-streams> (possible-conversions String)
(java.lang.String java.io.InputStream java.nio.DirectByteBuffer java.nio.ByteBuffer (seq-of java.nio.ByteBuffer) java.io.Reader java.nio.channels.ReadableByteChannel [B java.lang.CharSequence)

byte-streams/optimized-transfer? returns true if there is an optimized transfer method for two types:

clj-commons.byte-streams> (optimized-transfer? String java.io.File)
true

Deprecation notice

There exists an older, top-level namespace called just byte-streams that has been deprecated. Top-level namespaces can cause problems, particularly with Graal. Please switch to clj-commons.byte-streams in all your requires.

License

Copyright ยฉ 2014 Zachary Tellman

Distributed under the MIT License

More Repositories

1

aleph

Asynchronous streaming communication for Clojure - web server, web client, and raw TCP/UDP
Clojure
2,518
star
2

kibit

There's a function for that!
Clojure
1,752
star
3

seesaw

Seesaw turns the Horror of Swing into a friendly, well-documented, Clojure library
Clojure
1,445
star
4

manifold

A compatibility layer for event-driven abstractions
Clojure
1,008
star
5

etaoin

Pure Clojure Webdriver protocol implementation
Clojure
893
star
6

marginalia

Ultra-lightweight literate programming for clojure inspired by docco
HTML
808
star
7

secretary

A client-side router for ClojureScript.
Clojure
773
star
8

hickory

HTML as data
Clojure
622
star
9

claypoole

Claypoole: Threadpool tools for Clojure
Clojure
607
star
10

pretty

Library for helping print things prettily, in Clojure - ANSI fonts, formatted exceptions
Clojure
587
star
11

rewrite-clj

Rewrite Clojure code and edn
Clojure
576
star
12

potemkin

some ideas which are almost good
Clojure
564
star
13

pomegranate

A sane Clojure API for Maven Artifact Resolver + dynamic runtime modification of the classpath
Clojure
500
star
14

gloss

speaks in bytes, so you don't have to
Clojure
480
star
15

camel-snake-kebab

A Clojure[Script] library for word case conversions
Clojure
464
star
16

cljss

Clojure Style Sheets โ€” CSS-in-JS for ClojureScript
Clojure
451
star
17

clooj

clooj, a lightweight IDE for clojure
Clojure
416
star
18

durable-queue

a disk-backed queue for clojure
Clojure
381
star
19

useful

Some Clojure functions we use all the time, and so can you.
Clojure
365
star
20

metrics-clojure

A thin faรงade around Coda Hale's metrics library.
Clojure
341
star
21

citrus

State management library for Rum
Clojure
273
star
22

ordered

Ordered sets and maps, implemented in pure clojure
Clojure
253
star
23

clj-ssh

SSH commands via jsch
Clojure
227
star
24

dirigiste

centrally-planned object and thread pools
Java
204
star
25

primitive-math

for the discerning arithmetician
Clojure
170
star
26

iapetos

A Clojure Prometheus Client
Clojure
169
star
27

humanize

Produce human readable strings in clojure
Clojure
154
star
28

digest

Digest algorithms (md5, sha1 ...) for Clojure
Clojure
151
star
29

clj-yaml

YAML encoding and decoding for Clojure
Clojure
115
star
30

byte-transforms

methods for hashing, compressing, and encoding bytes
Clojure
104
star
31

ring-buffer

A persistent ring-buffer in Clojure
Clojure
96
star
32

tentacles

An Octocat is nothing without his tentacles
Clojure
77
star
33

fs

File system utilities for Clojure. (forked from Raynes/fs)
Clojure
72
star
34

lein-marginalia

A Marginalia plugin to Leiningen
HTML
68
star
35

meta

A meta-repo for clj-commons discussions
46
star
36

ring-gzip-middleware

GZIP your Ring responses
Clojure
41
star
37

rewrite-cljs

Traverse and rewrite Clojure/ClojureScript/EDN from ClojureScript
Clojure
41
star
38

formatter

Building blocks and discussion for building a common Clojure code formatter
36
star
39

vizdeps

Visualize Leiningen dependencies using Graphviz
Clojure
32
star
40

zprint-clj

Node.js wrapper for ZPrint Clojure source code formatter
Clojure
13
star
41

infra

Infrastructure for clj-commons
Clojure
2
star
42

clj-commons.github.io

Clojure Commons Site
HTML
1
star