Botan 3.1.1
Crypto and TLS for C&
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
12#include <botan/exceptn.h>
13#include <botan/internal/fmt.h>
14#include <ostream>
15
16#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
17 #include <fstream>
18#endif
19
20namespace Botan {
21
22/*
23* Write to a stream
24*/
25void DataSink_Stream::write(const uint8_t out[], size_t length) {
26 m_sink.write(cast_uint8_ptr_to_char(out), length);
27 if(!m_sink.good()) {
28 throw Stream_IO_Error("DataSink_Stream: Failure writing to " + m_identifier);
29 }
30}
31
32/*
33* Flush the stream
34*/
36 m_sink.flush();
37}
38
39/*
40* DataSink_Stream Constructor
41*/
42DataSink_Stream::DataSink_Stream(std::ostream& out, std::string_view name) : m_identifier(name), m_sink(out) {}
43
44#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
45
46/*
47* DataSink_Stream Constructor
48*/
49DataSink_Stream::DataSink_Stream(std::string_view path, bool use_binary) :
50 m_identifier(path),
51 m_sink_memory(std::make_unique<std::ofstream>(std::string(path), use_binary ? std::ios::binary : std::ios::out)),
52 m_sink(*m_sink_memory) {
53 if(!m_sink.good()) {
54 throw Stream_IO_Error(fmt("DataSink_Stream: Failure opening path '{}'", path));
55 }
56}
57#endif
58
60
61} // namespace Botan
void write(const uint8_t[], size_t) override
Definition: data_snk.cpp:25
void end_msg() override
Definition: data_snk.cpp:35
DataSink_Stream(std::ostream &stream, std::string_view name="<std::ostream>")
Definition: data_snk.cpp:42
std::string name
Definition: alg_id.cpp:13
std::string fmt(std::string_view format, const T &... args)
Definition: fmt.h:53
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:181
Definition: bigint.h:1030