Botan  2.6.0
Crypto and TLS for C++11
data_snk.cpp
Go to the documentation of this file.
1 /*
2 * DataSink
3 * (C) 1999-2007 Jack Lloyd
4 * 2005 Matthew Gregan
5 * 2017 Philippe Lieser
6 *
7 * Botan is released under the Simplified BSD License (see license.txt)
8 */
9 
10 #include <botan/data_snk.h>
11 #include <botan/exceptn.h>
12 #include <ostream>
13 
14 #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
15  #include <fstream>
16 #endif
17 
18 namespace Botan {
19 
20 /*
21 * Write to a stream
22 */
23 void DataSink_Stream::write(const uint8_t out[], size_t length)
24  {
25  m_sink.write(cast_uint8_ptr_to_char(out), length);
26  if(!m_sink.good())
27  throw Stream_IO_Error("DataSink_Stream: Failure writing to " +
28  m_identifier);
29  }
30 
31 /*
32 * Flush the stream
33 */
35  {
36  m_sink.flush();
37  }
38 
39 /*
40 * DataSink_Stream Constructor
41 */
43  const std::string& name) :
44  m_identifier(name),
45  m_sink(out)
46  {
47  }
48 
49 #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
50 
51 /*
52 * DataSink_Stream Constructor
53 */
54 DataSink_Stream::DataSink_Stream(const std::string& path,
55  bool use_binary) :
56  m_identifier(path),
57  m_sink_memory(new std::ofstream(path, use_binary ? std::ios::binary : std::ios::out)),
58  m_sink(*m_sink_memory)
59  {
60  if(!m_sink.good())
61  {
62  throw Stream_IO_Error("DataSink_Stream: Failure opening " + path);
63  }
64  }
65 #endif
66 
67 /*
68 * DataSink_Stream Destructor
69 */
71  {
72  // for ~unique_ptr
73  }
74 
75 }
void end_msg() override
Definition: data_snk.cpp:34
Definition: bigint.h:719
DataSink_Stream(std::ostream &stream, const std::string &name="<std::ostream>")
Definition: data_snk.cpp:42
Definition: alg_id.cpp:13
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:136
void write(const uint8_t[], size_t) override
Definition: data_snk.cpp:23