Skip to content

Commit 700b3e4

Browse files
committed
Copy namespaces into clj-commons
Update primitive-math to use clj-commons.primitive-math in new nses.
1 parent db2e306 commit 700b3e4

13 files changed

+2128
-0
lines changed

src/clj_commons/byte_streams.clj

Lines changed: 1019 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package clj_commons.byte_streams;
2+
3+
import java.io.InputStream;
4+
import java.io.IOException;
5+
import java.nio.ByteBuffer;
6+
7+
public class ByteBufferInputStream extends InputStream {
8+
9+
private ByteBuffer _buf;
10+
11+
public ByteBufferInputStream(ByteBuffer buf) {
12+
_buf = buf;
13+
}
14+
15+
public void close() {
16+
}
17+
18+
public int available() {
19+
return _buf.remaining();
20+
}
21+
22+
public boolean markSupported() {
23+
return true;
24+
}
25+
26+
public void mark(int readlimit) {
27+
_buf.mark();
28+
}
29+
30+
public void reset() {
31+
_buf.reset();
32+
}
33+
34+
public long skip(long n) {
35+
int nP = Math.min((int)n, _buf.remaining());
36+
_buf.position(_buf.position() + nP);
37+
return (long)nP;
38+
}
39+
40+
public int read() throws IOException {
41+
if (!_buf.hasRemaining()) {
42+
return -1;
43+
} else {
44+
return (int) _buf.get() & 0xFF;
45+
}
46+
}
47+
48+
public int read(byte[] bytes, int offset, int length) throws IOException {
49+
length = Math.min(length, _buf.remaining());
50+
if (length == 0) {
51+
return -1;
52+
} else {
53+
_buf.get(bytes, offset, length);
54+
return length;
55+
}
56+
}
57+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package clj_commons.byte_streams;
2+
3+
import java.io.IOException;
4+
5+
public class InputStream extends java.io.InputStream {
6+
7+
public interface Streamable {
8+
int available();
9+
void close();
10+
long skip(long n);
11+
int read() throws IOException;
12+
int read(byte[] bytes, int offset, int length) throws IOException;
13+
}
14+
15+
private Streamable _s;
16+
17+
public InputStream(Streamable s) {
18+
_s = s;
19+
}
20+
21+
public void close() {
22+
_s.close();
23+
}
24+
25+
public int available() {
26+
return _s.available();
27+
}
28+
29+
public boolean markSupported() {
30+
return false;
31+
}
32+
33+
public void mark(int readlimit) {
34+
throw new UnsupportedOperationException();
35+
}
36+
37+
public void reset() {
38+
throw new UnsupportedOperationException();
39+
}
40+
41+
public long skip(long n) {
42+
return _s.skip(n);
43+
}
44+
45+
public int read() throws IOException {
46+
return _s.read();
47+
}
48+
49+
public int read(byte[] bytes, int offset, int length) throws IOException {
50+
return _s.read(bytes, offset, length);
51+
}
52+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package clj_commons.byte_streams;
2+
3+
public class Utils {
4+
public static byte[] byteArray(int length) {
5+
return new byte[length];
6+
}
7+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
(ns clj-commons.byte-streams.char-sequence
2+
(:refer-clojure :exclude [flush])
3+
(:import
4+
[java.util.concurrent.locks
5+
ReentrantLock]
6+
[java.io
7+
ByteArrayOutputStream]
8+
[java.nio
9+
ByteBuffer
10+
CharBuffer]
11+
[java.nio.charset
12+
Charset
13+
CharsetDecoder
14+
CoderResult
15+
CodingErrorAction]))
16+
17+
(set! *unchecked-math* true)
18+
19+
(defn coding-error-action [action]
20+
(case
21+
:report CodingErrorAction/REPORT
22+
:ignore CodingErrorAction/IGNORE
23+
:replace CodingErrorAction/REPLACE))
24+
25+
(defn parse-result [^CoderResult result]
26+
(cond
27+
(.isUnderflow result) :underflow
28+
(.isOverflow result) :overflow
29+
:else (throw (IllegalArgumentException. "Malformed byte-stream input to CharsetDecoder"))))
30+
31+
(defn decode
32+
[^CharsetDecoder decoder ^ByteBuffer in ^CharBuffer out]
33+
(parse-result (.decode decoder in out false)))
34+
35+
(defn flush
36+
[^CharsetDecoder decoder ^ByteBuffer in ^CharBuffer out]
37+
(parse-result (.decode decoder (or in (ByteBuffer/allocate 0)) out true))
38+
(parse-result (.flush decoder out)))
39+
40+
(defn concat-bytes [^ByteBuffer a ^ByteBuffer b]
41+
(let [buf (ByteBuffer/allocate (+ (.remaining a) (.remaining b)))]
42+
(.put buf a)
43+
(.put buf b)
44+
(.flip buf)))
45+
46+
(defn lazy-char-buffer-sequence
47+
[^CharsetDecoder decoder
48+
chunk-size
49+
^ByteBuffer extra-bytes
50+
close-fn
51+
byte-source]
52+
(lazy-seq
53+
(let [num-bytes (+ (long
54+
(if extra-bytes
55+
(.remaining extra-bytes)
56+
0))
57+
(long chunk-size))
58+
len (long
59+
(Math/ceil
60+
(/ num-bytes
61+
(.averageCharsPerByte decoder))))
62+
out (CharBuffer/allocate len)]
63+
64+
(if (and extra-bytes (= :overflow (decode decoder extra-bytes out)))
65+
66+
;; we didn't even exhaust the overflow bytes, try again
67+
(cons
68+
out
69+
(lazy-char-buffer-sequence decoder chunk-size extra-bytes close-fn byte-source))
70+
71+
(if-let [in (byte-source chunk-size)]
72+
(let [in (if (and extra-bytes (.hasRemaining extra-bytes))
73+
(concat-bytes extra-bytes in)
74+
in)
75+
result (decode decoder in out)]
76+
(cons
77+
(.flip out)
78+
(lazy-char-buffer-sequence
79+
decoder
80+
chunk-size
81+
(when (.hasRemaining ^ByteBuffer in) in)
82+
close-fn
83+
byte-source)))
84+
(do
85+
(flush decoder extra-bytes out)
86+
(when close-fn (close-fn))
87+
(.flip out)))))))
88+
89+
(defn decode-byte-source
90+
[byte-source
91+
close-fn
92+
{:keys [chunk-size encoding on-encoding-error]
93+
:or {chunk-size 1024
94+
on-encoding-error :replace
95+
encoding "UTF-8"}}]
96+
(let [action (coding-error-action on-encoding-error)
97+
decoder (doto (.newDecoder (Charset/forName encoding))
98+
(.onMalformedInput action)
99+
(.onUnmappableCharacter action))
100+
s (lazy-char-buffer-sequence decoder chunk-size nil close-fn byte-source)]
101+
(reify
102+
java.io.Closeable
103+
(close [_] (when close-fn (close-fn)))
104+
105+
CharSequence
106+
(charAt [_ idx]
107+
(loop [remaining idx, s s]
108+
(if (empty? s)
109+
(throw (IndexOutOfBoundsException. (str idx)))
110+
(let [^CharBuffer buf (first s)]
111+
(if (< (.remaining buf) remaining)
112+
(.charAt buf remaining)
113+
(recur (- remaining (.remaining buf)) (rest s)))))))
114+
(length [_]
115+
(reduce + (map #(.remaining ^CharBuffer %) s)))
116+
#_(subSequence [_ start end]
117+
)
118+
(toString [_]
119+
(let [buf (StringBuffer.)]
120+
(doseq [b s]
121+
(.append buf b))
122+
(.toString buf))))))

0 commit comments

Comments
 (0)