API for duck-streams (master branch)

by Stuart Sierra

Usage:
(ns your-namespace
  (:require clojure.contrib.duck-streams))

Overview

This file defines "duck-typed" I/O utility functions for Clojure.
The 'reader' and 'writer' functions will open and return an
instance of java.io.BufferedReader and java.io.PrintWriter,
respectively, for a variety of argument types -- filenames as
strings, URLs, java.io.File's, etc.  'reader' even works on http
URLs.

Note: this is not really "duck typing" as implemented in languages
like Ruby.  A better name would have been "do-what-I-mean-streams"
or "just-give-me-a-stream", but ducks are funnier.

Public Variables and Functions



*append-to-writer*

var

  
If true, writer and spit will open files in append mode.
Defaults to false.  Use append-writer or append-spit.
Source


*buffer-size*

var

  
Size, in bytes or characters, of the buffer used when
copying streams.
Source


*byte-array-type*

var

  
Type object for a Java primitive byte array.
Source


*default-encoding*

var

  
Name of the default encoding to use when reading & writing.
Default is UTF-8.
Source


append-spit

function
Usage: (append-spit f content)
Like spit but appends to file.
Source


append-writer

function
Usage: (append-writer x)
Like writer but opens file for appending.  Does not work on streams
that are already open.
Source


copy

multimethod
Usage: (copy input output)
Copies input to output.  Returns nil.
Input may be an InputStream, Reader, File, byte[], or String.
Output may be an OutputStream, Writer, or File.

Does not close any streams except those it opens itself 
(on a File).

Writing a File fails if the parent directory does not exist.
Source


file-str

function
Usage: (file-str & args)
Concatenates args as strings and returns a java.io.File.  Replaces
all / and \ with File/separatorChar.  Replaces ~ at the start of
the path with the user.home system property.
Source


make-parents

function
Usage: (make-parents file)
Creates all parent directories of file.
Source


pwd

function
Usage: (pwd)
Returns current working directory as a String.  (Like UNIX 'pwd'.)
Note: In Java, you cannot change the current working directory.
Source


read-lines

function
Usage: (read-lines f)
Like clojure.core/line-seq but opens f with reader.  Automatically
closes the reader AFTER YOU CONSUME THE ENTIRE SEQUENCE.
Source


reader

multimethod
Usage: (reader x)
Attempts to coerce its argument into an open
java.io.BufferedReader.  Argument may be an instance of Reader,
BufferedReader, InputStream, File, URI, URL, Socket, or String.

If 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.  Uses *default-encoding* as the text encoding.

Should be used inside with-open to ensure the Reader is properly
closed.
Source


slurp*

function
Usage: (slurp* f)
Like clojure.core/slurp but opens f with reader.
Source


spit

function
Usage: (spit f content)
Opposite of slurp.  Opens f with writer, writes content, then
closes f.
Source


to-byte-array

multimethod
Usage: (to-byte-array arg)
Converts argument into a Java byte array.  Argument may be
a String, File, InputStream, or Reader.  If the argument is already
a byte array, returns it.
Source


with-in-reader

macro
Usage: (with-in-reader f & body)
Opens a PushbackReader on f, binds it to *in*, and evaluates body.
Source


with-out-append-writer

macro
Usage: (with-out-append-writer f & body)
Like with-out-writer but appends to file.
Source


with-out-writer

macro
Usage: (with-out-writer f & body)
Opens a writer on f, binds it to *out*, and evalutes body.
Anything printed within body will be written to f.
Source


write-lines

function
Usage: (write-lines f lines)
Writes lines (a seq) to f, separated by newlines.  f is opened with
writer, and automatically closed at the end of the sequence.
Source


writer

multimethod
Usage: (writer x)
Attempts to coerce its argument into an open java.io.PrintWriter
wrapped around a java.io.BufferedWriter.  Argument may be an
instance of Writer, PrintWriter, BufferedWriter, OutputStream, File,
URI, URL, Socket, or String.

If 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.
Source
Logo & site design by Tom Hickey.
Clojure auto-documentation system by Tom Faulhaber.