Created
March 22, 2022 15:41
-
-
Save seebs/7b717671660fd6a43a9b0ae73341ea3b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(require '[clojure.string :as str]) | |
(import '(java.nio ByteBuffer)) | |
(def testdata "00 05 61 61 61 61 61 00 05 61 61 61 61 61") | |
(defn parse [^ByteBuffer stream] nil) | |
(defn load-nbt-string | |
[^ByteBuffer stream] | |
(let [l (.getShort stream) data (byte-array l)] | |
(.get stream data) | |
(String. data))) | |
(defn load-nbt-byte-array | |
[^ByteBuffer stream] | |
(let [l (.getInt stream) data (byte-array l)] | |
(.get stream data) | |
data)) | |
(let [ | |
hexBytes (str/split testdata #" ") | |
bytes (map #(Integer/parseInt % 16) hexBytes) | |
ba (byte-array (count bytes) bytes) | |
buf (. ByteBuffer (wrap ba))] | |
; if these are both load-nbt-string, it works | |
(prn (load-nbt-byte-array buf) (load-nbt-string buf))) | |
; instead: | |
; Syntax error (BufferUnderflowException) compiling at (foo.clj:20:1). | |
; null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment