You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+19-19Lines changed: 19 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -8,17 +8,17 @@ Java has a lot of different ways to represent a stream of bytes. Depending on t
8
8
9
9
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](http://aleph.io/codox/byte-streams/).
10
10
11
-
### usage
11
+
### Usage
12
12
13
-
```clj
14
13
[byte-streams "0.2.4"]
14
+
```clojure
15
15
```
16
16
17
-
### converting types
17
+
### Converting types
18
18
19
19
To convert one byte representation to another, use `byte-streams/convert`:
`(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:
@@ -37,14 +37,14 @@ While we can't turn a string into a `ByteBuffer`, we can turn a string into a `b
37
37
38
38
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:
39
39
40
-
```clj
40
+
```clojure
41
41
byte-stream> (to-input-stream (repeat"hello"))
42
42
#<InputStream byte_streams.InputStream@3962a02c>
43
43
```
44
44
45
45
And then we can turn that into a lazy sequence of `ByteBuffers`:
46
46
47
-
```clj
47
+
```clojure
48
48
byte-streams> (take2
49
49
(convert *1
50
50
(seq-of java.nio.ByteBuffer)
@@ -61,11 +61,11 @@ Notice that we describe a sequence of a type as `(seq-of type)`, and that we've
61
61
62
62
To create a [Manifold stream](https://github.com/ztellman/manifold), use `(stream-of type)`. To convert a core.async channel, convert it using `manifold.stream/->source`.
63
63
64
-
### custom conversions
64
+
### Custom conversions
65
65
66
66
While there are conversions definedfor all common byte types, this can be extended to other libraries via `byte-streams/def-conversion`:
67
67
68
-
```clj
68
+
```clojure
69
69
;; a conversion from byte-buffers to my-byte-representation
70
70
(def-conversion [ByteBuffer MyByteRepresentation]
71
71
[buf options]
@@ -77,11 +77,11 @@ While there are conversions defined for all common byte types, this can be exten
77
77
78
78
This mechanism can even be used for types unrelated to byte streams, if you're feeling adventurous.
79
79
80
-
### transfers
80
+
### Transfers
81
81
82
82
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 `byte-streams/transfer`.
83
83
84
-
```clj
84
+
```clojure
85
85
byte-streams> (deff (File."/tmp/salutations"))
86
86
#'byte-streams/f
87
87
byte-streams> (transfer"hello" f {:append?false})
@@ -92,17 +92,17 @@ byte-streams> (to-string f)
92
92
93
93
`(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:
94
94
95
-
```clj
95
+
```clojure
96
96
(def-transfer [InputStream MyByteSink]
97
97
[stream sink options]
98
98
(send-stream-to-my-sink stream sink))
99
99
```
100
100
101
-
### some utilities
101
+
### Some utilities
102
102
103
103
`byte-streams/print-bytes` will print both hexadecimal and ascii representations of a collection of bytes:
104
104
105
-
```clj
105
+
```clojure
106
106
byte-streams> (print-bytes (-> #'print-bytes meta :doc))
107
107
5072696E 7473206F 7574207468652062 Prints out the b
108
108
7974657320696E 20626F 746820686578 ytes in both hex
@@ -113,7 +113,7 @@ byte-streams> (print-bytes (-> #'print-bytes meta :doc))
113
113
114
114
`(byte-streams/compare-bytes a b)` will return a value which is positive if `a` is lexicographically first, zero if they're equal, and negative otherwise:
0 commit comments