2012-04-29 1 views
1

私はClojureのに新たなんだと私はClojureのは、次のJavaコードと同等である私にとっては簡単な作業、実行する必要があります。(「RW」「ファイル」)のClojure - れるMappedByteBufferに(getChannel.mapによって返された)DirectByteBufferをキャストするにはどのように?

れるMappedByteBufferアウト=新しいのRandomAccessFileをさらに、getChannel( ).map(FileChannel.MapMode.READ_WRITE、0、100);

Clojureのは動的言語、地図である(ただし)はれるMappedByteBufferの代わりDirectByteBufferを返します。 MappedByteBufferのメンバーであるsetInt()メソッドを使用したいと思います。 Clojureに、DirectByteBufferの代わりにMappedByteBufferでメソッドを使用するよう指示する方法はありますか?

ありがとうございます!

ところで、これは私の試みです:ClojureのはDirectMapBufferに.setIntを探しているので

(defprotocol MfileP 
    (at  [this pos]) 
    (get-i [this]) 
    (set-i [this val]) 
    (resize [this size]) 
    (fsize [this])) 

(defrecord Mfile [fc buf] MfileP 
    (at  [this pos] (.position buf pos)) 
    (get-i [this]  (.getInt buf)) 
    (set-i [this val] (.setInt buf val)) 
    (resize [this size] (assoc this :buf (.map fc FileChannel$MapMode/READ_WRITE 0 size))) 
    (fsize [this]  (.size fc))) 

(defn open [path] 
    (let [fc (.getChannel (new RandomAccessFile path "rw"))] 
    (let [buf (.map fc FileChannel$MapMode/READ_WRITE 0 (.size fc))] 
     (Mfile. fc buf)))) 

フォーム(設定-i)は、例外がスローされます。

答えて

4

あなたは、あなたがmap()DirectByteBufferを返していることが確立したと思うか言うことはありません。そうではありません - 抽象クラスMappedByteBufferのサブクラスを間違いなく返しています。

は、JDKのドキュメントに応じて何の方法MappedByteBuffer#setInt(int)ありません。

あなたは、インターフェイスにコーディングする必要があります。

参照してください:

java.nio.FileChannel#map(...) javadocs

java.nio.MappedByteBuffer javadocs

関連する問題