Skip to content

Instantly share code, notes, and snippets.

@seebs
Created March 22, 2022 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seebs/7b717671660fd6a43a9b0ae73341ea3b to your computer and use it in GitHub Desktop.
Save seebs/7b717671660fd6a43a9b0ae73341ea3b to your computer and use it in GitHub Desktop.
(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