aboutsummaryrefslogtreecommitdiffstats
path: root/src/data_snk.cpp
blob: 7f2080723b6b55ceb8f670f697ac02b7b336e41a (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
/*************************************************
* DataSink Source File                           *
* (C) 1999-2007 The Botan Project                *
*************************************************/

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

namespace Botan {

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

/*************************************************
* DataSink_Stream Constructor                    *
*************************************************/
DataSink_Stream::DataSink_Stream(std::ostream& stream) : fsname("std::ostream")
   {
   sink = &stream;
   owns = false;
   }

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

   if(!sink->good())
      throw Stream_IO_Error("DataSink_Stream: Failure opening " + fsname);
   owns = true;
   }

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

}