From dc7f50d1ac3bc7c938cc762a16cf385e2039c427 Mon Sep 17 00:00:00 2001 From: Ferdinand Majerech Date: Tue, 22 Jul 2014 01:01:37 +0200 Subject: [PATCH] A module for backwards compatibility with std.stream. --- source/dyaml/streamcompat.d | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 source/dyaml/streamcompat.d diff --git a/source/dyaml/streamcompat.d b/source/dyaml/streamcompat.d new file mode 100644 index 0000000..448033d --- /dev/null +++ b/source/dyaml/streamcompat.d @@ -0,0 +1,62 @@ +// Copyright Ferdinand Majerech 2014. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/// Backward compatibility with std.stream (which was used by D:YAML API in the past). +module dyaml.streamcompat; + + +import std.stream; + +import tinyendian; + + +/// A streamToBytes wrapper that allocates memory using the GC. +/// +/// See_Also: streamToBytes +ubyte[] streamToBytesGC(Stream stream) @trusted nothrow +{ + try return stream.streamToBytes(new ubyte[stream.available]); + catch(Exception e) + { + assert(false, "Unexpected exception in streamToBytesGC: " ~ e.msg); + } +} + +/// Read all data from a std.stream.Stream into an array of bytes. +/// +/// Params: +/// +/// stream = Stream to read from. Must be readable and seekable. +/// memory = Memory to use. Must be long enough to store the entire stream +/// (memory.length >= stream.available). +/// +/// Returns: A slice of memory containing all contents of the stream on success. +/// NULL if unable to read the entire stream. +ubyte[] streamToBytes(Stream stream, ubyte[] memory) @system nothrow +{ + try + { + assert(stream.readable && stream.seekable, + "Can't read YAML from a stream that is not readable and seekable"); + assert(memory.length >= stream.available, + "Not enough memory passed to streamToBytes"); + + auto buffer = memory[0 .. stream.available]; + size_t bytesRead = 0; + for(; bytesRead < buffer.length;) + { + // Returns 0 on eof + const bytes = stream.readBlock(&buffer[bytesRead], buffer.length - bytesRead); + // Reached EOF before reading buffer.length bytes. + if(bytes == 0) { return null; } + bytesRead += bytes; + } + return buffer; + } + catch(Exception e) + { + assert(false, "Unexpected exception in streamToBytes " ~ e.msg); + } +}