Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
cloojure committed Jan 18, 2020
1 parent 995e277 commit ed5409c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
72 changes: 36 additions & 36 deletions src/clj/tupelo/io.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
[arg] (instance? DataOutputStream arg))

;---------------------------------------------------------------------------------------------------

(s/defn create-temp-file :- java.io.File
"Given a Path dir unique ID string (e.g. 'my.dummy.file'), returns a java File object
for a temporary that will be deleted upon JVM exit."
Expand Down Expand Up @@ -66,7 +65,8 @@
(Files/createTempDirectory dir (str prefix "-") attrs))))

(s/defn delete-directory
"Recursively deletes a directory and all its contents. Returns count of objects deleted."
"Recursively deletes a directory and all its contents. Returns count of objects deleted.
Idempotent in case dir is already deleted."
[dir-name :- s/Str]
(with-nil-default 0
(let [file-obj (io/file dir-name)
Expand All @@ -80,144 +80,134 @@
num-files))))

;---------------------------------------------------------------------------------------------------

(s/defn read-bytes ; #todo type?
"Reads N bytes from a DataInputStream, returning a byte array."
[N :- s/Int
input-stream :- InputStream]
(let [bytarr (byte-array N)]
(.read (validate input-stream? input-stream) bytarr)
(.read ^InputStream (validate input-stream? input-stream) bytarr)
bytarr))

(s/defn write-bytes ; #todo type?
"Writes a byte array to a DataInputStream."
"Writes a byte array to a DataOutputStream."
[out-stream :- OutputStream
bytarr :- s/Any] ; #todo type
(.write (validate output-stream? out-stream) bytarr)
(.write ^OutputStream (validate output-stream? out-stream) bytarr)
bytarr)

;---------------------------------------------------------------------------------------------------
(s/defn read-string-bytes :- s/Str
"Reads nchars bytes from a DataInputStream, returning a String."
[nchars :- s/Int
dis :- DataInputStream]
(String. (read-bytes nchars (validate data-input-stream? dis))))
(String. (read-bytes nchars ^DataInputStream (validate data-input-stream? dis))))

(s/defn read-byte :- s/Int ; #todo need test
"Reads 1 byte (signed) from a DataInputStream"
[dis :- DataInputStream]
(long (.readByte (validate data-input-stream? dis) )))
(long (.readByte ^DataInputStream (validate data-input-stream? dis) )))

(s/defn read-byte-unsigned :- s/Int ; #todo need test
"Reads 1 byte (unsigned) from a DataInputStream"
[dis :- DataInputStream]
(long (.readUnsignedByte (validate data-input-stream? dis) )))
(long (.readUnsignedByte ^DataInputStream (validate data-input-stream? dis) )))

(s/defn read-short :- s/Int ; #todo need test
"Reads 2 bytes (signed) from a DataInputStream"
[dis :- DataInputStream]
(long (.readShort (validate data-input-stream? dis))))
(long (.readShort ^DataInputStream (validate data-input-stream? dis))))

(s/defn read-short-unsigned :- s/Int ; #todo need test
"Reads 2 bytes (unsigned) from a DataInputStream"
[dis :- DataInputStream]
(long (.readUnsignedShort (validate data-input-stream? dis))))
(long (.readUnsignedShort ^DataInputStream (validate data-input-stream? dis))))

(s/defn read-integer :- s/Int ; #todo need test
"Reads 4 bytes (signed) from a DataInputStream"
[dis :- DataInputStream]
(long (.readInt (validate data-input-stream? dis))))
(long (.readInt ^DataInputStream (validate data-input-stream? dis))))

(s/defn read-long :- s/Int ; #todo need test
"Reads 8 bytes (signed) from a DataInputStream"
[dis :- DataInputStream]
(long (.readLong (validate data-input-stream? dis))))
(long (.readLong ^DataInputStream (validate data-input-stream? dis))))

(s/defn read-integer-unsigned :- s/Int ; #todo need test
"Reads 4 bytes (unsigned) from a DataInputStream"
[dis :- DataInputStream]
(long (BigInteger. ^bytes (glue zeros-4
(read-bytes 4 (validate data-input-stream? dis))))))
(read-bytes 4 ^DataInputStream (validate data-input-stream? dis))))))

(s/defn read-long-unsigned :- s/Int ; #todo need test
"Reads 8 bytes (unsigned) from a DataInputStream, returning a BigInteger"
[dis :- DataInputStream]
(BigInteger. ^bytes (glue zeros-4
(read-bytes 8 (validate data-input-stream? dis)))))


; #todo finish
(s/defn read-float :- s/Num ; #todo need test
"Reads a 4 byte single-precision floating-point value from a DataInputStream"
[dis :- DataInputStream]
(.readFloat (validate data-input-stream? dis)))

(s/defn read-double :- s/Num ; #todo need test
"Reads an 8 byte double-precision floating-point value from a DataInputStream"
[dis :- DataInputStream]
(.readDouble (validate data-input-stream? dis)))
(read-bytes 8 ^DataInputStream (validate data-input-stream? dis)))))

;---------------------------------------------------------------------------------------------------
(s/defn write-string-bytes :- s/Str
"Converts an ASCII String to bytes and writes them to a DataInputStream (w/o length header)."
[dos :- DataOutputStream
str-val :- s/Str]
(.writeBytes (validate data-output-stream? dos) str-val)
(.writeBytes ^DataOutputStream (validate data-output-stream? dos) str-val)
str-val)

(s/defn write-byte :- s/Int ; #todo need test
"Writes 1 byte (signed) to a DataOutputStream."
[dos :- DataOutputStream
val :- s/Int]
(.writeByte (validate data-output-stream? dos)
(.writeByte ^DataOutputStream (validate data-output-stream? dos)
(validate types/within-bounds-byte? val))
val)

(s/defn write-byte-unsigned :- s/Int ; #todo need test
"Writes 1 byte (unsigned) to a DataOutputStream"
[dos :- DataOutputStream
val :- s/Int]
(.writeByte (validate data-output-stream? dos)
(.writeByte ^DataOutputStream (validate data-output-stream? dos)
(validate types/within-bounds-byte-unsigned? val))
val)

(s/defn write-short :- s/Int ; #todo need test
"Writes 2 bytes (signed) to a DataOutputStream"
[dos :- DataOutputStream
val :- s/Int]
(.writeShort (validate data-output-stream? dos)
(.writeShort ^DataOutputStream (validate data-output-stream? dos)
(validate types/within-bounds-short? val))
val)

(s/defn write-short-unsigned :- s/Int ; #todo need test
"Writes 2 bytes (unsigned) to a DataOutputStream"
[dos :- DataOutputStream
val :- s/Int]
(.writeShort (validate data-output-stream? dos)
(.writeShort ^DataOutputStream (validate data-output-stream? dos)
(validate types/within-bounds-short-unsigned? val))
val)

(s/defn write-integer :- s/Int ; #todo need test
"Writes 4 bytes (signed) to a DataOutputStream"
[dos :- DataOutputStream
val :- s/Int]
(.writeInt (validate data-output-stream? dos)
(.writeInt ^DataOutputStream (validate data-output-stream? dos)
(validate types/within-bounds-integer? val))
val)

(s/defn write-integer-unsigned :- s/Int ; #todo need test
"Writes 4 bytes (signed) to a DataOutputStream"
[dos :- DataOutputStream
val :- s/Int]
(.writeInt (validate data-output-stream? dos)
(validate types/within-bounds-integer-unsigned? val))
(validate types/within-bounds-integer-unsigned? val)
(let [val-biginteger (biginteger val)
byte-vec (take-last 4 (glue zeros-8 (.toByteArray ^BigInteger val-biginteger)))
bytarr (byte-array byte-vec)]
(write-bytes dos bytarr))
val)

(s/defn write-long :- s/Int ; #todo need test
"Writes 8 bytes (signed) to a DataOutputStream"
[dos :- DataOutputStream
val :- s/Int]
(.writeLong (validate data-output-stream? dos)
(.writeLong ^DataOutputStream (validate data-output-stream? dos)
(validate types/within-bounds-long? val))
val)

Expand All @@ -232,6 +222,16 @@
(write-bytes dos bytarr))
val)

;---------------------------------------------------------------------------------------------------
(s/defn read-float :- s/Num ; #todo need test
"Reads a 4 byte single-precision floating-point value from a DataInputStream"
[dis :- DataInputStream]
(.readFloat ^DataInputStream (validate data-input-stream? dis)))

(s/defn read-double :- s/Num ; #todo need test
"Reads an 8 byte double-precision floating-point value from a DataInputStream"
[dis :- DataInputStream]
(.readDouble ^DataInputStream (validate data-input-stream? dis)))

(s/defn write-float :- s/Num ; #todo need test
"Writes a 4 byte single-precision floating-point value to a DataInputStream"
Expand Down
20 changes: 17 additions & 3 deletions test/clj/tst/tupelo/io.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
; file epl-v10.html at the root of this distribution. By using this software in any
; fashion, you are agreeing to be bound by the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns tst.tupelo.io
(ns ^:test-refresh/focus tst.tupelo.io
(:use tupelo.io tupelo.core tupelo.test)
(:refer-clojure :exclude [read-string])
(:require
Expand Down Expand Up @@ -61,6 +61,12 @@
(is= (write-short-unsigned dos types/SHORT_UNSIGNED_MIN_VALUE) types/SHORT_UNSIGNED_MIN_VALUE)
(is= (write-short-unsigned dos types/SHORT_UNSIGNED_MAX_VALUE) types/SHORT_UNSIGNED_MAX_VALUE)

(is= (write-integer-unsigned dos types/INTEGER_UNSIGNED_MIN_VALUE) types/INTEGER_UNSIGNED_MIN_VALUE)
(is= (write-integer-unsigned dos types/INTEGER_UNSIGNED_MAX_VALUE) types/INTEGER_UNSIGNED_MAX_VALUE)

(is= (write-long-unsigned dos types/LONG_UNSIGNED_MIN_VALUE) types/LONG_UNSIGNED_MIN_VALUE)
(is= (write-long-unsigned dos types/LONG_UNSIGNED_MAX_VALUE) types/LONG_UNSIGNED_MAX_VALUE)

(is= (write-string-bytes dos "hello") "hello")

;-----------------------------------------------------------------------------
Expand All @@ -80,7 +86,13 @@
(throws? (write-byte-unsigned dos (dec types/BYTE_UNSIGNED_MIN_VALUE)))

(throws? (write-short-unsigned dos (inc types/SHORT_UNSIGNED_MAX_VALUE)))
(throws? (write-short-unsigned dos (dec types/SHORT_UNSIGNED_MIN_VALUE)))))
(throws? (write-short-unsigned dos (dec types/SHORT_UNSIGNED_MIN_VALUE)))

(throws? (write-integer-unsigned dos (inc types/INTEGER_UNSIGNED_MAX_VALUE)))
(throws? (write-integer-unsigned dos (dec types/INTEGER_UNSIGNED_MIN_VALUE)))

(throws? (write-long-unsigned dos (inc types/LONG_UNSIGNED_MAX_VALUE)))
(throws? (write-long-unsigned dos (dec types/LONG_UNSIGNED_MIN_VALUE)))))

(dotest
(with-open [dos (DataOutputStream.
Expand Down Expand Up @@ -227,7 +239,9 @@

(is= 7 (count-files-fn tmp-name))
(is= 7 (delete-directory tmp-name))
(is= 0 (count-files-fn tmp-name))))
(is= 0 (count-files-fn tmp-name))
(is= 0 (delete-directory tmp-name)) ; idempotent
))



0 comments on commit ed5409c

Please sign in to comment.