1//
2// StreamCopier.cpp
3//
4// Library: Foundation
5// Package: Streams
6// Module: StreamCopier
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/StreamCopier.h"
16#include "Poco/Buffer.h"
17
18
19namespace Poco {
20
21
22std::streamsize StreamCopier::copyStream(std::istream& istr, std::ostream& ostr, std::size_t bufferSize)
23{
24 poco_assert (bufferSize > 0);
25
26 Buffer<char> buffer(bufferSize);
27 std::streamsize len = 0;
28 istr.read(buffer.begin(), bufferSize);
29 std::streamsize n = istr.gcount();
30 while (n > 0)
31 {
32 len += n;
33 ostr.write(buffer.begin(), n);
34 if (istr && ostr)
35 {
36 istr.read(buffer.begin(), bufferSize);
37 n = istr.gcount();
38 }
39 else n = 0;
40 }
41 return len;
42}
43
44
45#if defined(POCO_HAVE_INT64)
46Poco::UInt64 StreamCopier::copyStream64(std::istream& istr, std::ostream& ostr, std::size_t bufferSize)
47{
48 poco_assert (bufferSize > 0);
49
50 Buffer<char> buffer(bufferSize);
51 Poco::UInt64 len = 0;
52 istr.read(buffer.begin(), bufferSize);
53 std::streamsize n = istr.gcount();
54 while (n > 0)
55 {
56 len += n;
57 ostr.write(buffer.begin(), n);
58 if (istr && ostr)
59 {
60 istr.read(buffer.begin(), bufferSize);
61 n = istr.gcount();
62 }
63 else n = 0;
64 }
65 return len;
66}
67#endif
68
69
70std::streamsize StreamCopier::copyToString(std::istream& istr, std::string& str, std::size_t bufferSize)
71{
72 poco_assert (bufferSize > 0);
73
74 Buffer<char> buffer(bufferSize);
75 std::streamsize len = 0;
76 istr.read(buffer.begin(), bufferSize);
77 std::streamsize n = istr.gcount();
78 while (n > 0)
79 {
80 len += n;
81 str.append(buffer.begin(), static_cast<std::string::size_type>(n));
82 if (istr)
83 {
84 istr.read(buffer.begin(), bufferSize);
85 n = istr.gcount();
86 }
87 else n = 0;
88 }
89 return len;
90}
91
92
93#if defined(POCO_HAVE_INT64)
94Poco::UInt64 StreamCopier::copyToString64(std::istream& istr, std::string& str, std::size_t bufferSize)
95{
96 poco_assert (bufferSize > 0);
97
98 Buffer<char> buffer(bufferSize);
99 Poco::UInt64 len = 0;
100 istr.read(buffer.begin(), bufferSize);
101 std::streamsize n = istr.gcount();
102 while (n > 0)
103 {
104 len += n;
105 str.append(buffer.begin(), static_cast<std::string::size_type>(n));
106 if (istr)
107 {
108 istr.read(buffer.begin(), bufferSize);
109 n = istr.gcount();
110 }
111 else n = 0;
112 }
113 return len;
114}
115#endif
116
117
118std::streamsize StreamCopier::copyStreamUnbuffered(std::istream& istr, std::ostream& ostr)
119{
120 char c = 0;
121 std::streamsize len = 0;
122 istr.get(c);
123 while (istr && ostr)
124 {
125 ++len;
126 ostr.put(c);
127 istr.get(c);
128 }
129 return len;
130}
131
132
133#if defined(POCO_HAVE_INT64)
134Poco::UInt64 StreamCopier::copyStreamUnbuffered64(std::istream& istr, std::ostream& ostr)
135{
136 char c = 0;
137 Poco::UInt64 len = 0;
138 istr.get(c);
139 while (istr && ostr)
140 {
141 ++len;
142 ostr.put(c);
143 istr.get(c);
144 }
145 return len;
146}
147#endif
148
149
150} // namespace Poco
151