How to correctly read from RandomAccessFile in Clojure?
I've been trying to develop a little library for reading MPQ files in Clojure, and I decided to use RandomAccessFile as MPQ is a binary format.
However, I'm having trouble with the method RandomAccessFile#read()
, as I'm not really sure if I'm calling it correctly
here's the code:
(ns parser
(:import (java.io RandomAccessFile)))
(with-open [file (RandomAccessFile. "r开发者_开发技巧eplay.SC2Replay" "r")]
(let [x (byte-array 16)]
(.read file [x 0 16])))
when I run the code, I get the
Exception in thread "main" java.lang.ClassCastException: clojure.lang.PersistentVector cannot be cast to [B (parser.clj:0)
at clojure.lang.Compiler.eval(Compiler.java:5440)
at clojure.lang.Compiler.load(Compiler.java:5857)
at clojure.lang.Compiler.loadFile(Compiler.java:5820)
at clojure.main$load_script.invoke(main.clj:221)
at clojure.main$script_opt.invoke(main.clj:273)
at clojure.main$main.doInvoke(main.clj:354)
at clojure.lang.RestFn.invoke(RestFn.java:409)
at clojure.lang.Var.invoke(Var.java:365)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:482)
at clojure.main.main(main.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassCastException: clojure.lang.PersistentVector cannot be cast to [B
at parser$eval7.invoke(parser.clj:7)
at clojure.lang.Compiler.eval(Compiler.java:5424)
... 15 more
exception. I also tried doing this for the temporary variable
(let [x []]
but it both yields the same exception. The same code in Java would be something like this
RandomAccessFile file = new RandomAccessFile("replay.SC2Replay", "r");
byte[] x;
file.read(x, 0, 16);
You're passing in a single vector as the argument, which it's trying to cast to a byte array so it can invoke the single argument read method that takes in a byte array. Just pass in the arguments as is.
(with-open [file (RandomAccessFile. "replay.SC2Replay" "r")]
(let [x (byte-array 16)]
(.read file x 0 16)))
精彩评论