Discussion:
Converting a file to a stream
Félix López
2016-01-12 06:38:11 UTC
Permalink
Hi there,

Is it possible to convert a file to a stream?

I have this code:

(def stoken (gloss/string :utf-8 :delimiters "«"))
(defcodec record-codec [stoken stoken])

(defn read-from-file [offset]
(let [raf (RandomAccessFile. (:db_file conf) "r")
buf (byte-array 4096)
_ (.seek raf offset)
n (.read raf buf)]
(.close raf)
(decode record-codec (java.nio.ByteBuffer/wrap buf) false)))

This method has the problem that I'm only reading 4096, I'm going to change
this method to read the file one byte a at time until I reach the
delimiters and I'm wondering if it would be possible to convert the file to
a stream
and then use decode-stream.

Thanks, Félix
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Félix López
2016-01-12 09:39:36 UTC
Permalink
I have a working version but without gloss:

(defn char-seq
[^java.io.RandomAccessFile rdr]
(let [chr (.read rdr)]
(if (> chr 0)
(cons chr (lazy-seq (char-seq rdr))))))

(defn parse-key-value [bytes-buffer]
(str/split (String. bytes-buffer) #"«"))

(defn read-from-file [offset]
(let [raf (RandomAccessFile. (:db_file conf) "r")
_ (.seek raf offset)
buf (char-seq raf)
data (parse-key-value (byte-array buf))]
(.close raf)
data))
Post by Félix López
Hi there,
Is it possible to convert a file to a stream?
(def stoken (gloss/string :utf-8 :delimiters "«"))
(defcodec record-codec [stoken stoken])
(defn read-from-file [offset]
(let [raf (RandomAccessFile. (:db_file conf) "r")
buf (byte-array 4096)
_ (.seek raf offset)
n (.read raf buf)]
(.close raf)
(decode record-codec (java.nio.ByteBuffer/wrap buf) false)))
This method has the problem that I'm only reading 4096, I'm going to
change this method to read the file one byte a at time until I reach the
delimiters and I'm wondering if it would be possible to convert the file to
a stream
and then use decode-stream.
Thanks, Félix
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Zach Tellman
2016-01-12 19:38:10 UTC
Permalink
You can just call `(decode-all frame (java.io.File. path))`, and it should
do what you want.
Post by Félix López
(defn char-seq
[^java.io.RandomAccessFile rdr]
(let [chr (.read rdr)]
(if (> chr 0)
(cons chr (lazy-seq (char-seq rdr))))))
(defn parse-key-value [bytes-buffer]
(str/split (String. bytes-buffer) #"«"))
(defn read-from-file [offset]
(let [raf (RandomAccessFile. (:db_file conf) "r")
_ (.seek raf offset)
buf (char-seq raf)
data (parse-key-value (byte-array buf))]
(.close raf)
data))
Post by Félix López
Hi there,
Is it possible to convert a file to a stream?
(def stoken (gloss/string :utf-8 :delimiters "«"))
(defcodec record-codec [stoken stoken])
(defn read-from-file [offset]
(let [raf (RandomAccessFile. (:db_file conf) "r")
buf (byte-array 4096)
_ (.seek raf offset)
n (.read raf buf)]
(.close raf)
(decode record-codec (java.nio.ByteBuffer/wrap buf) false)))
This method has the problem that I'm only reading 4096, I'm going to
change this method to read the file one byte a at time until I reach the
delimiters and I'm wondering if it would be possible to convert the file to
a stream
and then use decode-stream.
Thanks, Félix
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Félix López
2016-01-13 07:54:27 UTC
Permalink
thanks!
Post by Zach Tellman
You can just call `(decode-all frame (java.io.File. path))`, and it should
do what you want.
Post by Félix López
(defn char-seq
[^java.io.RandomAccessFile rdr]
(let [chr (.read rdr)]
(if (> chr 0)
(cons chr (lazy-seq (char-seq rdr))))))
(defn parse-key-value [bytes-buffer]
(str/split (String. bytes-buffer) #"«"))
(defn read-from-file [offset]
(let [raf (RandomAccessFile. (:db_file conf) "r")
_ (.seek raf offset)
buf (char-seq raf)
data (parse-key-value (byte-array buf))]
(.close raf)
data))
Post by Félix López
Hi there,
Is it possible to convert a file to a stream?
(def stoken (gloss/string :utf-8 :delimiters "«"))
(defcodec record-codec [stoken stoken])
(defn read-from-file [offset]
(let [raf (RandomAccessFile. (:db_file conf) "r")
buf (byte-array 4096)
_ (.seek raf offset)
n (.read raf buf)]
(.close raf)
(decode record-codec (java.nio.ByteBuffer/wrap buf) false)))
This method has the problem that I'm only reading 4096, I'm going to
change this method to read the file one byte a at time until I reach the
delimiters and I'm wondering if it would be possible to convert the file to
a stream
and then use decode-stream.
Thanks, Félix
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...