Botan  1.11.15
src/lib/filters/pipe_io.cpp
Go to the documentation of this file.
00001 /*
00002 * Pipe I/O
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/pipe.h>
00009 #include <iostream>
00010 
00011 namespace Botan {
00012 
00013 /*
00014 * Write data from a pipe into an ostream
00015 */
00016 std::ostream& operator<<(std::ostream& stream, Pipe& pipe)
00017    {
00018    secure_vector<byte> buffer(DEFAULT_BUFFERSIZE);
00019    while(stream.good() && pipe.remaining())
00020       {
00021       size_t got = pipe.read(&buffer[0], buffer.size());
00022       stream.write(reinterpret_cast<const char*>(&buffer[0]), got);
00023       }
00024    if(!stream.good())
00025       throw Stream_IO_Error("Pipe output operator (iostream) has failed");
00026    return stream;
00027    }
00028 
00029 /*
00030 * Read data from an istream into a pipe
00031 */
00032 std::istream& operator>>(std::istream& stream, Pipe& pipe)
00033    {
00034    secure_vector<byte> buffer(DEFAULT_BUFFERSIZE);
00035    while(stream.good())
00036       {
00037       stream.read(reinterpret_cast<char*>(&buffer[0]), buffer.size());
00038       pipe.write(&buffer[0], stream.gcount());
00039       }
00040    if(stream.bad() || (stream.fail() && !stream.eof()))
00041       throw Stream_IO_Error("Pipe input operator (iostream) has failed");
00042    return stream;
00043    }
00044 
00045 }