/*************************************************
* Pipe I/O Source File                           *
* (C) 1999-2003 The Botan Project                *
*************************************************/

#include <botan/pipe.h>
#include <iostream>

namespace Botan {

/*************************************************
* Write data from a pipe into an ostream         *
*************************************************/
std::ostream& operator<<(std::ostream& stream, Pipe& pipe)
   {
   SecureBuffer<byte, DEFAULT_BUFFERSIZE> buffer;
   while(stream.good() && pipe.remaining())
      {
      u32bit got = pipe.read(buffer, buffer.size());
      stream.write((const char*)buffer.ptr(), got);
      }
   if(!stream.good())
      throw Stream_IO_Error("Pipe output operator (iostream) has failed");
   return stream;
   }

/*************************************************
* Read data from an istream into a pipe          *
*************************************************/
std::istream& operator>>(std::istream& stream, Pipe& pipe)
   {
   SecureBuffer<byte, DEFAULT_BUFFERSIZE> buffer;
   while(stream.good())
      {
      stream.read((char*)buffer.ptr(), buffer.size());
      pipe.write(buffer, stream.gcount());
      }
   if(stream.bad() || (stream.fail() && !stream.eof()))
      throw Stream_IO_Error("Pipe input operator (iostream) has failed");
   return stream;
   }

}
