From 175b03c13446628f380438c11172a57a4b7e425a Mon Sep 17 00:00:00 2001
From: Doug Coleman <erg@jobim.local>
Date: Wed, 3 Jun 2009 21:21:21 -0500
Subject: [PATCH] add a <limited-file-reader> word to open a limited stream
 from a file correctly

---
 basis/images/png/png.factor             |  3 +--
 basis/io/streams/limited/limited.factor | 11 +++++++++--
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/basis/images/png/png.factor b/basis/images/png/png.factor
index d4b284142f..b8a9a1d569 100755
--- a/basis/images/png/png.factor
+++ b/basis/images/png/png.factor
@@ -104,8 +104,7 @@ ERROR: unimplemented-color-type image ;
     } case ;
 
 : load-png ( path -- image )
-    [ binary <file-reader> ] [ file-info size>> ] bi
-    stream-throws <limited-stream> [
+    binary stream-throws <limited-file-reader> [
         <png-image>
         read-png-header
         read-png-chunks
diff --git a/basis/io/streams/limited/limited.factor b/basis/io/streams/limited/limited.factor
index b1b07a08c0..fd441e4c4d 100755
--- a/basis/io/streams/limited/limited.factor
+++ b/basis/io/streams/limited/limited.factor
@@ -1,8 +1,9 @@
 ! Copyright (C) 2008 Slava Pestov.
 ! Copyright (C) 2009 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: kernel math io io.encodings destructors accessors
-sequences namespaces byte-vectors fry combinators ;
+USING: accessors byte-vectors combinators destructors fry io
+io.encodings io.files io.files.info kernel math namespaces
+sequences ;
 IN: io.streams.limited
 
 TUPLE: limited-stream stream count limit mode stack ;
@@ -16,6 +17,12 @@ SINGLETONS: stream-throws stream-eofs ;
         swap >>stream
         0 >>count ;
 
+: <limited-file-reader> ( path encoding mode -- stream' )
+    [
+        [ <file-reader> ]
+        [ drop file-info size>> ] 2bi
+    ] dip <limited-stream> ;
+
 GENERIC# limit 2 ( stream limit mode -- stream' )
 
 M: decoder limit ( stream limit mode -- stream' )