-
-
Save zoren/4eb9543395d743670154b7b10059e608 to your computer and use it in GitHub Desktop.
Get a clojure reader using the encoding by the BOM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns util | |
(:require [clojure.java.io :as io]) | |
(:import org.apache.commons.io.input.BOMInputStream | |
org.apache.commons.io.ByteOrderMark)) | |
(defn bom-reader | |
"Returns a BOM contextual reader with the proper encoding set (= BOM), defaults to UTF-8" | |
[input-stream] | |
(let [bom-array | |
(into-array [ByteOrderMark/UTF_16LE | |
ByteOrderMark/UTF_16BE | |
ByteOrderMark/UTF_8 | |
ByteOrderMark/UTF_32BE | |
ByteOrderMark/UTF_32LE]) | |
bom-stream (BOMInputStream. input-stream false bom-array) | |
char-set-name (.getCharsetName (or (.getBOM bom-stream) ByteOrderMark/UTF_8))] | |
(io/reader bom-stream :encoding char-set-name))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note the byte order mark is not included in the stream. Contrary to @biggert original.