|
33 | 33 | (parse-result (.decode decoder in out false)))
|
34 | 34 |
|
35 | 35 | (defn flush
|
36 |
| - ([decoder out] (flush decoder (ByteBuffer/allocate 0) out)) |
37 |
| - ([^CharsetDecoder decoder ^ByteBuffer in ^CharBuffer out] |
38 |
| - (and |
39 |
| - (parse-result (.decode decoder in out true)) |
40 |
| - (parse-result (.flush decoder out))))) |
41 |
| - |
42 |
| -(defn has-remaining-bytes? [^ByteBuffer byte-buffer] |
43 |
| - {:pre [(some? byte-buffer)]} |
44 |
| - (.hasRemaining byte-buffer)) |
| 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))) |
45 | 39 |
|
46 |
| -(defn merge-byte-buffers [^ByteBuffer l ^ByteBuffer r] |
47 |
| - {:pre [(some? l) (some? r)]} |
48 |
| - (-> (ByteBuffer/allocate (+ (.remaining l) (.remaining r))) |
49 |
| - (.put l) |
50 |
| - (.put r) |
51 |
| - .flip)) |
| 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))) |
52 | 45 |
|
53 | 46 | (defn lazy-char-buffer-sequence
|
54 | 47 | [^CharsetDecoder decoder
|
|
76 | 69 | (lazy-char-buffer-sequence decoder chunk-size extra-bytes close-fn byte-source))
|
77 | 70 |
|
78 | 71 | (if-let [in (byte-source chunk-size)]
|
79 |
| - (let [expanded-in (if (some-> extra-bytes has-remaining-bytes?) |
80 |
| - ;; in case of underflow we need to pass new buffer |
81 |
| - ;; containing remaining bytes from the initial input |
82 |
| - ;; along with some new bytes to the CharsetDecoder |
83 |
| - (merge-byte-buffers extra-bytes in) |
84 |
| - in) |
85 |
| - result (decode decoder expanded-in out)] |
| 72 | + (let [in (if (and extra-bytes (.hasRemaining extra-bytes)) |
| 73 | + (concat-bytes extra-bytes in) |
| 74 | + in) |
| 75 | + result (decode decoder in out)] |
86 | 76 | (cons
|
87 | 77 | (.flip out)
|
88 | 78 | (lazy-char-buffer-sequence
|
89 | 79 | decoder
|
90 | 80 | chunk-size
|
91 |
| - (when (has-remaining-bytes? expanded-in) |
92 |
| - expanded-in) |
| 81 | + (when (.hasRemaining ^ByteBuffer in) in) |
93 | 82 | close-fn
|
94 | 83 | byte-source)))
|
95 | 84 | (do
|
96 |
| - (if (some? extra-bytes) |
97 |
| - (flush decoder extra-bytes out) |
98 |
| - (flush decoder out)) |
| 85 | + (flush decoder extra-bytes out) |
99 | 86 | (when close-fn (close-fn))
|
100 | 87 | (.flip out)))))))
|
101 | 88 |
|
102 | 89 | (defn decode-byte-source
|
103 | 90 | [byte-source
|
104 | 91 | close-fn
|
105 | 92 | {:keys [chunk-size encoding on-encoding-error]
|
106 |
| - :or {chunk-size 4096 |
| 93 | + :or {chunk-size 1024 |
107 | 94 | on-encoding-error :replace
|
108 | 95 | encoding "UTF-8"}}]
|
109 | 96 | (let [action (coding-error-action on-encoding-error)
|
|
0 commit comments