~scompall/stream-stream/typed

« back to all changes in this revision

Viewing changes to src/com/nocandysw/stream_stream.clj

  • Committer: Stephen Compall
  • Date: 2012-12-13 02:23:23 UTC
  • mfrom: (50.1.21 mainline)
  • Revision ID: scompall@nocandysw.com-20121213022323-a1ggt648dy9wtqrs
Catch up with 0.3.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
;;; stream_stream.clj: Delineating nonfunctional stream code.
2
 
;;; Copyright (C) 2011  Stephen Compall
 
2
;;; Copyright (C) 2011, 2012  Stephen Compall
3
3
;;;
4
4
;;; Licensed under the Apache License, Version 2.0 (the "License");
5
5
;;; you may not use this file except in compliance with the License.
28
28
streams, and output streams.  You will be most interested in
29
29
`read-block-seq', `write-block-seq', and `block-seq->seq', though
30
30
`->char-array-seq-sequence' may come in handy."
31
 
  (:use clojure.template
32
 
        [typed.core :only (ann tc-ignore)])
 
31
  (:use [typed.core :only (ann tc-ignore)])
33
32
  (:require [typed.core :as ty])
34
33
  (:import (java.io InputStream OutputStream Reader StringWriter Writer)
35
34
           java.util.Arrays
147
146
 
148
147
(defn- ^IChunk block->chunk
149
148
  [block]
150
 
  (if (.isArray block)
151
 
    (->iarray-chunk block)
152
 
    (ArrayChunk. (to-array block))))
 
149
  (let [cb (class block)]
 
150
    (if (and cb (.isArray cb) (-> cb .getComponentType .isPrimitive))
 
151
      (->iarray-chunk block 0)
 
152
      (ArrayChunk. (to-array block)))))
153
153
 
154
154
(defn block-seq->seq
155
 
  "Build a chunked seq around a sequence of primitive data blocks.  Be
156
 
warned: the chunks will likely be quite large, taking away much of the
157
 
laziness you're used to."
 
155
  "Like mapcat identity, but build a chunk out of each entry, aliasing 
 
156
any elements that are Java arrays.
 
157
 
 
158
Be warned: for seqs of blocks built by `read-block-seq', the chunks
 
159
will likely be quite large, taking away much of the laziness you're
 
160
used to."
158
161
  [blocks]
159
162
  (mapcat #(chunk-cons (block->chunk %) nil) blocks))
160
163
 
183
186
(defn- block-subs
184
187
  "Helper for char-array-seq-sequence."
185
188
  [cass blocks start end]
186
 
  (if (or (neg? start) (neg? end) (> start end))
187
 
    (throw (IndexOutOfBoundsException.
188
 
            (format "%s[%d..%d]" (class cass) start end)))
189
 
    (->char-array-seq-sequence
190
 
     ((fn more [[block & blocks :as blocks?] start skipped]
191
 
        (lazy-seq
192
 
         (throw (Exception. "TODO"))))
193
 
      blocks start 0))))
 
189
  (letfn [(oob []
 
190
            (throw (IndexOutOfBoundsException.
 
191
                    (format "%s[%d..%d]" (class cass) start end))))]
 
192
    (if (or (neg? start) (neg? end) (> start end))
 
193
      (oob)
 
194
      (->char-array-seq-sequence
 
195
       ((fn more [[^"[C" block & blocks :as blocks?] start skipped]
 
196
          (lazy-seq
 
197
           (let [blocksz (count block)
 
198
                 nextpt (+ blocksz skipped)]
 
199
             (cond (and (empty? blocks?) (< skipped end)) (oob),
 
200
                   (= skipped end) nil,
 
201
                   (>= start nextpt)
 
202
                   (more blocks start nextpt),
 
203
                   (>= end nextpt)
 
204
                   (cons (if (= start skipped)
 
205
                           block
 
206
                           (Arrays/copyOfRange block (- start skipped) blocksz))
 
207
                         (more blocks nextpt nextpt)),
 
208
                   :else                ;(< skipped end nextpt)
 
209
                   [(Arrays/copyOfRange block (- start skipped) (- end skipped))]))))
 
210
        blocks start 0)))))
194
211
 
195
212
)                                       ;end tc-ignore
196
213