Module: bulk-io-internal Author: Seth LaForge Synopsis: bulk-io, we haul s... Copyright: Original Code is Copyright (c) 1995-2004 Functional Objects, Inc. All rights reserved. License: Functional Objects Library Public License Version 1.0 Dual-license: GNU Lesser General Public License Warranty: Distributed WITHOUT WARRANTY OF ANY KIND // Aaaarrrrghhhh!!! Why isn't this exported? define constant = ; define sealed class () constant slot mapped-file :: , required-init-keyword: mapped-file:; slot stream-position :: = 0; slot stream-size :: ; end class ; define method initialize (this :: , #key #all-keys) => () next-method(); this.stream-size := this.mapped-file.size; end; // Note - this doesn't define the entire or // protocols, but it should do enough for dood. // Note: closes the too. define method close (this :: , #key #all-keys) => () close-mapped-memory(this.mapped-file, final-size: this.size); end method close; define method read-element (this :: , #key on-end-of-stream = unsupplied()) => (elem) block () this.mapped-file[this.stream-position] afterwards this.stream-position := this.stream-position + 1; exception () if (unsupplied?(on-end-of-stream)) error(make(, stream: this)); else on-end-of-stream end if; end block; end method read-element; define method read (this :: , n :: , #key on-end-of-stream) => (r) ignore(on-end-of-stream); let r = make(, size: n); read-into!(this, n, r); r end method read; define method read-byte-string (this :: , n :: , #key on-end-of-stream) => (r) ignore(on-end-of-stream); let r = make(, size: n); read-into!(this, n, r); r end method read-byte-string; define method read-into! (this :: , n :: , seq :: , #key start = 0, on-end-of-stream) => (count) ignore(on-end-of-stream); block () copy-bytes(this.mapped-file, this.stream-position, seq, start, n); this.stream-position := this.stream-position + n; n exception () error(make(, stream: this)); end block; end method read-into!; define method write-element (this :: , elem :: ) => () this.mapped-file[this.stream-position] := elem; this.stream-position := this.stream-position + 1; if (this.stream-position > this.stream-size) this.stream-size := this.stream-position; end if; end method write-element; define method write (this :: , seq :: , #key start = 0, end: last = unsupplied()) => () if (unsupplied?(last)) last := seq.size; end if; let n = last - start; copy-bytes(seq, start, this.mapped-file, this.stream-position, n); this.stream-position := this.stream-position + n; if (this.stream-position > this.stream-size) this.stream-size := this.stream-position; end if; end method write; define method read-word-32 (this :: ) => (r :: ) block () word-32-element(this.mapped-file, this.stream-position) afterwards this.stream-position := this.stream-position + 4; exception () error(make(, stream: this)); end block; end method read-word-32; define method write-word-32 (this :: , value :: ) => () word-32-element(this.mapped-file, this.stream-position) := value; this.stream-position := this.stream-position + 4; if (this.stream-position > this.stream-size) this.stream-size := this.stream-position; end if; end method write-word-32; define method read-word-64 (this :: ) => (r :: ) block () word-64-element(this.mapped-file, this.stream-position) afterwards this.stream-position := this.stream-position + 8; exception () error(make(, stream: this)); end block; end method read-word-64; define method write-word-64 (this :: , value :: ) => () word-64-element(this.mapped-file, this.stream-position) := value; this.stream-position := this.stream-position + 8; if (this.stream-position > this.stream-size) this.stream-size := this.stream-position; end if; end method write-word-64; define method force-output (this :: ) => () flush-mapped-memory(this.mapped-file); end method force-output; define method adjust-stream-position (this :: , delta :: , #key from = #"current") => (new-position :: ) let origin :: = select (from) #"start" => 0; #"current" => this.stream-position; #"end" => this.stream-size; end select; this.stream-position := origin + delta; if (this.stream-position > this.stream-size) this.stream-size := this.stream-position; end if; this.stream-position end method adjust-stream-position;