1 | // |
2 | // Binary.cpp |
3 | // |
4 | // Library: MongoDB |
5 | // Package: MongoDB |
6 | // Module: Binary |
7 | // |
8 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/MongoDB/Binary.h" |
16 | |
17 | |
18 | namespace Poco { |
19 | namespace MongoDB { |
20 | |
21 | |
22 | Binary::Binary(): |
23 | _buffer(0), |
24 | _subtype(0) |
25 | { |
26 | } |
27 | |
28 | |
29 | Binary::Binary(Poco::Int32 size, unsigned char subtype): |
30 | _buffer(size), |
31 | _subtype(subtype) |
32 | { |
33 | } |
34 | |
35 | |
36 | Binary::Binary(const UUID& uuid): |
37 | _buffer(128 / 8), |
38 | _subtype(0x04) |
39 | { |
40 | unsigned char szUUID[16]; |
41 | uuid.copyTo((char*) szUUID); |
42 | _buffer.assign(szUUID, 16); |
43 | } |
44 | |
45 | |
46 | |
47 | Binary::Binary(const std::string& data, unsigned char subtype): |
48 | _buffer(reinterpret_cast<const unsigned char*>(data.data()), data.size()), |
49 | _subtype(subtype) |
50 | { |
51 | } |
52 | |
53 | |
54 | Binary::Binary(const void* data, Poco::Int32 size, unsigned char subtype): |
55 | _buffer(reinterpret_cast<const unsigned char*>(data), size), |
56 | _subtype(subtype) |
57 | { |
58 | } |
59 | |
60 | |
61 | Binary::~Binary() |
62 | { |
63 | } |
64 | |
65 | |
66 | std::string Binary::toString(int indent) const |
67 | { |
68 | std::ostringstream oss; |
69 | Base64Encoder encoder(oss); |
70 | MemoryInputStream mis((const char*) _buffer.begin(), _buffer.size()); |
71 | StreamCopier::copyStream(mis, encoder); |
72 | return oss.str(); |
73 | } |
74 | |
75 | |
76 | UUID Binary::uuid() const |
77 | { |
78 | if (_subtype == 0x04 && _buffer.size() == 16) |
79 | { |
80 | UUID uuid; |
81 | uuid.copyFrom((const char*) _buffer.begin()); |
82 | return uuid; |
83 | } |
84 | throw BadCastException("Invalid subtype" ); |
85 | } |
86 | |
87 | |
88 | } } // namespace Poco::MongoDB |
89 | |