Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

moving bytes-and-back so it can be used externally.

  • Loading branch information...
commit 183a9a5d47c06e92ebc2e8312d3bcd7fc9684350 1 parent 6dc7a9c
Jeff Rose authored
Showing with 15 additions and 15 deletions.
  1. +11 −8 src/byte_spec.clj
  2. +4 −7 test/bytes_test.clj
19 src/byte_spec.clj
View
@@ -1,8 +1,8 @@
(ns byte-spec
(:import (java.net URL)
- (java.io FileInputStream FileOutputStream
+ (java.io FileInputStream FileOutputStream
DataInputStream DataOutputStream
- BufferedInputStream BufferedOutputStream
+ BufferedInputStream BufferedOutputStream
ByteArrayOutputStream ByteArrayInputStream)))
; This file implements a DSL for specifying the layout of binary data formats.
@@ -26,7 +26,7 @@
(.write *spec-out* (.getBytes s)))
;; Standard numeric types + Pascal style strings.
-;; pstring => a byte giving the string length followed by the ascii bytes
+;; pstring => a byte giving the string length followed by the ascii bytes
(def READERS {
:int8 #(.readByte *spec-in*)
:int16 #(.readShort *spec-in*)
@@ -81,7 +81,7 @@
(nth specs 2)
nil)
fdefault (coerce-default fdefault ftype)
- spec {:fname fname
+ spec {:fname fname
:ftype ftype
:fdefault fdefault}
specs (if (nil? fdefault)
@@ -114,8 +114,8 @@
(recur (dec i) (conj ary next-val)))
ary)))
-(defn spec-read
- "Returns an instantiation of the provided spec, with data read from
+(defn spec-read
+ "Returns an instantiation of the provided spec, with data read from
a DataInputStream bound to *spec-in*."
[spec]
(loop [specs (:specs spec)
@@ -169,11 +169,11 @@
(defn count-for [fname]
(keyword (.substring (name fname) 2)))
-(defn spec-write
+(defn spec-write
"Serializes the data according to spec, writing bytes onto *spec-out*."
[spec data]
(doseq [{:keys [fname ftype fdefault]} (:specs spec)]
- (cond
+ (cond
; count of another field starting with n-
(.startsWith (name fname) "n-")
(let [wrt (ftype WRITERS)
@@ -205,3 +205,6 @@
(spec-write spec data)))
(.toByteArray bos)))
+(defn bytes-and-back [spec obj]
+ (spec-read-bytes spec (spec-write-bytes spec obj)))
+
11 test/bytes_test.clj
View
@@ -1,11 +1,8 @@
(ns bytes-test
- (:use
+ (:use
clojure.test
byte-spec))
-(defn bytes-and-back [spec obj]
- (spec-read-bytes spec (spec-write-bytes spec obj)))
-
(defspec basic-type-spec
:a :int8
:b :int16
@@ -32,13 +29,13 @@
:c [:string])
(defn- floatify
- "Convert all numbers in col to floats."
+ "Convert all numbers in col to floats."
[col]
(map #(if (number? %1) (float %1) %1) col))
(deftest array-test []
(let [a {:n-a (byte 4)
- :a [1 2 3 4]
+ :a [1 2 3 4]
:n-b (int 6)
:b (floatify [3.23 4.3223 53.32 253.2 53.2 656.5])
:n-c (long 3)
@@ -49,7 +46,7 @@
(is (= 6 (:n-b b)))
(is (= 3 (:n-c b)))))
-(defspec rhythm-spec
+(defspec rhythm-spec
:name :string
:length :int16
:n-triggers :int32
Please sign in to comment.
Something went wrong with that request. Please try again.