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