aboutsummaryrefslogtreecommitdiffstats
path: root/src/data_snk.cpp
blob: bb5226f0c8e322564c7820422692a9e4862dc987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*************************************************
* DataSink Source File                           *
* (C) 1999-2007 Jack Lloyd                       *
*     2005 Matthew Gregan                        *
*************************************************/

#include <botan/data_snk.h>
#include <fstream>

namespace Botan {

/*************************************************
* Write to a stream                              *
*************************************************/
void DataSink_Stream::write(const byte out[], u32bit length)
   {
   sink->write(reinterpret_cast<const char*>(out), length);
   if(!sink->good())
      throw Stream_IO_Error("DataSink_Stream: Failure writing to " +
                            identifier);
   }

/*************************************************
* DataSink_Stream Constructor                    *
*************************************************/
DataSink_Stream::DataSink_Stream(std::ostream& out,
                                 const std::string& name) :
   identifier(name != "" ? name : "<std::ostream>"), owner(false)
   {
   sink = &out;
   }

/*************************************************
* DataSink_Stream Constructor                    *
*************************************************/
DataSink_Stream::DataSink_Stream(const std::string& path,
                                 bool use_binary) :
   identifier(path), owner(true)
   {
   if(use_binary)
      sink = new std::ofstream(path.c_str(), std::ios::binary);
   else
      sink = new std::ofstream(path.c_str());

   if(!sink->good())
      throw Stream_IO_Error("DataSink_Stream: Failure opening " + path);
   }

/*************************************************
* DataSink_Stream Destructor                     *
*************************************************/
DataSink_Stream::~DataSink_Stream()
   {
   if(owner)
      delete sink;
   sink = 0;
   }

}