Line data Source code
1 : /**
2 : Copyright (c) 2016-2022 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #include "SPIO.h"
25 : #include "SPBuffer.h"
26 :
27 : namespace STAPPLER_VERSIONIZED stappler::io {
28 :
29 642544 : size_t Producer::read(const Buffer &buf, size_t nbytes) const {
30 642544 : auto pbuf = buf.prepare(nbytes);
31 642544 : auto size = read_ptr(ptr, pbuf, nbytes);
32 642544 : buf.save(pbuf, nbytes, size);
33 642544 : return size;
34 : }
35 :
36 424750 : size_t Consumer::write(const Buffer &buf) const {
37 424750 : return write_ptr(ptr, buf.data(), buf.size());
38 : }
39 :
40 25 : size_t read(const Producer &from, const Callback<void(const Buffer &)> &f) {
41 25 : StackBuffer<(size_t)1_KiB> buf;
42 25 : size_t ret = 0;
43 25 : size_t cap = buf.capacity();
44 25 : size_t c = cap;
45 191450 : while (cap == c) {
46 191425 : c = from.read(buf, cap);
47 191425 : if (c > 0) {
48 191425 : ret += c;
49 191425 : if (f) {
50 191425 : f(buf);
51 : }
52 : }
53 : }
54 25 : return ret;
55 : }
56 :
57 25 : size_t read(const Producer &from, const Buffer &buf, const Callback<void(const Buffer &)> &f) {
58 25 : size_t ret = 0;
59 25 : size_t cap = buf.capacity();
60 25 : size_t c = cap;
61 19175 : while (cap == c) {
62 19150 : c = from.read(buf, cap);
63 19150 : if (c > 0) {
64 19150 : ret += c;
65 19150 : if (f) {
66 19150 : f(buf);
67 : }
68 : }
69 : }
70 25 : return ret;
71 : }
72 :
73 250 : size_t read(const Producer &from, const Consumer &to) {
74 250 : StackBuffer<(size_t)1_KiB> buf;
75 250 : size_t ret = 0;
76 250 : size_t cap = buf.capacity();
77 250 : size_t c = cap;
78 195275 : while (cap == c) {
79 195025 : c = from.read(buf, cap);
80 195025 : if (c > 0) {
81 195025 : ret += c;
82 195025 : to.write(buf);
83 : }
84 : }
85 250 : return ret;
86 : }
87 25 : size_t read(const Producer &from, const Consumer &to, const Callback<void(const Buffer &)> &f) {
88 25 : StackBuffer<(size_t)1_KiB> buf;
89 25 : return io::read(from, to, buf, f);
90 : }
91 25 : size_t read(const Producer &from, const Consumer &to, const Buffer & buf) {
92 25 : size_t ret = 0;
93 25 : size_t cap = buf.capacity();
94 25 : size_t c = cap;
95 19175 : while (cap == c) {
96 19150 : c = from.read(buf, cap);
97 19150 : if (c > 0) {
98 19150 : ret += c;
99 19150 : to.write(buf);
100 : }
101 : }
102 25 : return ret;
103 : }
104 50 : size_t read(const Producer &from, const Consumer &to, const Buffer & buf, const Callback<void(const Buffer &)> &f) {
105 50 : size_t ret = 0;
106 50 : size_t cap = buf.capacity();
107 50 : size_t c = cap;
108 210625 : while (cap == c) {
109 210575 : c = from.read(buf, cap);
110 210575 : if (c > 0) {
111 210575 : ret += c;
112 210575 : if (f) {
113 210575 : f(buf);
114 : }
115 210575 : to.write(buf);
116 : }
117 : }
118 50 : return ret;
119 : }
120 :
121 : }
|