Back
writer (clj)
(source)function
(writer x & opts)
Attempts to coerce its argument into an open java.io.Writer.
Default implementations always return a java.io.BufferedWriter.
Default implementations are provided for Writer, BufferedWriter,
OutputStream, File, URI, URL, Socket, and String.
If the argument is a String, it tries to resolve it first as a URI, then
as a local file name. URIs with a 'file' protocol are converted to
local file names.
Should be used inside with-open to ensure the Writer is properly
closed.
Examples
clojure
"(ns autodoc.build-html
\"This is the namespace that builds the HTML pages themselves.
It is implemented with a number of custom enlive templates.\"
{:skip-wiki true, :author \"Tom Faulhaber\"}
(:refer-clojure :exclude [empty complement])
(:import [java.util.jar JarFile]
[java.io File FileWriter BufferedWriter StringReader
BufferedInputStream BufferedOutputStream
ByteArrayOutputStream FileReader FileInputStream]
[java.util.regex Pattern])
(:require [clojure.string :as str])
(:use [net.cgrand.enlive-html :exclude (deftemplate)]
[clojure.java.io :only (as-file file writer)]
[clojure.java.shell :only (sh)]
[clojure.pprint :only (pprint cl-format pprint-ident
pprint-logical-block set-pprint-dispatch
get-pretty-writer fresh-line)]
[clojure.data.json :only (pprint-json)]
[autodoc.collect-info :only (contrib-info)]
[autodoc.params :only (params expand-classpath)])
(:use clojure.set clojure.java.io clojure.data clojure.java.browse
clojure.inspector clojure.zip clojure.stacktrace))")
(defn- flush-alerting-writer
[o]
(let [flush-count-atom (atom 0)]
[
(proxy [java.io.BufferedWriter] [o]
(flush []
(proxy-super flush)
(swap! flush-count-atom inc)))
flush-count-atom]))
(deftest test-flush-underlying-prn
[]
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
(binding [*out* out
*flush-on-newline* true]
(prn (range 50))
(prn (range 50)))
(is (= @flush-count-atom 2) "println flushes on newline")))
(deftest test-flush-underlying-pprint
[]
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
(binding [*out* out
*flush-on-newline* true]
(pprint (range 50))
(pprint (range 50)))
(is (= @flush-count-atom 2) "pprint flushes on newline")))
(deftest test-noflush-underlying-prn
[]
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
(binding [*out* out
*flush-on-newline* nil]
(prn (range 50))
(prn (range 50)))
(is (= @flush-count-atom 0) "println flushes on newline")))
(deftest test-noflush-underlying-pprint
[]
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
(binding [*out* out
*flush-on-newline* nil]
(pprint (range 50))
(pprint (range 50)))
(is (= @flush-count-atom 0) "pprint flushes on newline")))
jepsen-io/jepsen
(ns jepsen.report
"Prints out stuff."
(:require [jepsen.util :as util]
[clojure.java.io :as io]
[clojure.pprint :refer [pprint]]))
(defmacro to
"Binds stdout to a file for the duration of body."
[filename & body]
`(let [filename# ~filename]
(io/make-parents filename#)
(with-open [w# (io/writer filename#)]
(try
(binding [*out* w#] ~@body)
(finally
(println "Report written to" filename#))))))
ego/awesome-mojo
(import '[java.io OutputStream])
(require '[clojure.java.io :as io])
(def devnull (io/writer (OutputStream/nullOutputStream)))
chrovis/cljam
(ns cljam.io.vcf-bench
(:require [cljam.io.vcf :as vcf]
[cljam.test-common :as tcommon]
[cljam.util :as util]
[clojure.java.io :as cio]
[libra.bench :refer [are defbench]]
[libra.criterium :as c]))
(defbench encode-variant-small-bcf-bench
(are [f]
(util/with-temp-dir [d "encode-variant-small-bench"]
(with-open [r (vcf/reader f)
w (vcf/writer (cio/file d "out.bcf")
(vcf/meta-info r)
(vcf/header r))]
(let [vs (vec (vcf/read-variants r))]
(c/quick-bench
(vcf/write-variants w vs)))))
tcommon/test-bcf-complex-file))
(defbench encode-variant-large-bcf-bench
(tcommon/prepare-cavia!)
(are [f]
(util/with-temp-dir [d "encode-variant-large-bench"]
(with-open [r (vcf/reader f)
w (vcf/writer (cio/file d "out.bcf")
(vcf/meta-info r)
(vcf/header r))]
(let [vs (vec (vcf/read-variants-randomly r {:chr "chr1" :end 30000000} {}))]
(c/quick-bench
(vcf/write-variants w vs)))))
tcommon/test-large-bcf-file))
chrovis/cljam
(ns cljam.io.sam-bench
(:require [libra.bench :refer [defbench are]]
[libra.criterium :as c]
[clojure.java.io :as cio]
[cljam.test-common :as tcommon]
[cljam.util :as util]
[cljam.io.sam :as sam]))
(defbench encode-alignment-short-bench
(are [f]
(util/with-temp-dir [d "encode-alignment-short-bench"]
(with-open [r (sam/reader f)
w (sam/writer (cio/file d "out.bam"))]
(let [header (sam/read-header r)
xs (vec (sam/read-alignments r))]
(sam/write-header w header)
(sam/write-refs w header)
(doseq [x xs o (:options x)] o)
(c/quick-bench
(sam/write-alignments w xs header)))))
tcommon/test-sam-file
tcommon/medium-sam-file))
(defbench encode-alignment-long-bench
(tcommon/prepare-cavia!)
(are [f]
(util/with-temp-dir [d "encode-alignment-long-bench"]
(with-open [r (sam/reader f)
w (sam/writer (cio/file d "out.bam"))]
(let [header (sam/read-header r)
xs (vec (take 2000000 (sam/read-alignments r)))]
(sam/write-header w header)
(sam/write-refs w header)
(doseq [x xs o (:options x)] o)
(c/quick-bench
(sam/write-alignments w xs header)))))
tcommon/large-bam-file))
swannodette/swannodette.github.com
(require '[clojure.java.io :as io]
'[cognitect.transit :as t])
(import [java.io ByteArrayOutputStream])
(def writer (t/writer out :json))
(t/write writer cache)