From 196cfd9cac36a69c5d54a3e7fcc03063aae18d68 Mon Sep 17 00:00:00 2001 From: Andrei Alexandrescu Date: Thu, 23 Apr 2009 09:04:18 +0000 Subject: [PATCH] added File.byChunk --- std/stdio.d | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/std/stdio.d b/std/stdio.d index e75fc0adc..524ef7acd 100644 --- a/std/stdio.d +++ b/std/stdio.d @@ -754,6 +754,33 @@ to this file. */ } } +/** +Iterates through a file a chunk at a time by using $(D foreach). + +Example: + +--------- +void main() +{ + foreach (ubyte[] buffer; stdin.byChunk(4096)) + { + ... use buffer ... + } +} +--------- + +The content of $(D buffer) is reused across calls. In the example +above, $(D buffer.length) is 4096 for all iterations, except for the +last one, in which case $(D buffer.length) may be less than 4096 (but +always greater than zero). + +In case of an I/O error, an $(D StdioException) is thrown. + */ + chunks byChunk(size_t chunkSize) + { + return chunks(this, chunkSize); + } + /** $(D Range) that locks the file and allows fast writing to it. */