xref: /haiku/src/apps/haikudepot/util/DataIOUtils.cpp (revision a5061ecec55353a5f394759473f1fd6df04890da)
1 /*
2  * Copyright 2018-2020, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #include "DataIOUtils.h"
6 
7 
8 #define BUFFER_SIZE 1024
9 
10 
11 /*static*/ status_t
12 DataIOUtils::CopyAll(BDataIO* target, BDataIO* source)
13 {
14 	status_t result = B_OK;
15 	uint8 buffer[BUFFER_SIZE];
16 	size_t sizeRead = 0;
17 
18 	do {
19 		result = source->ReadExactly(buffer, BUFFER_SIZE, &sizeRead);
20 
21 		switch (result)
22 		{
23 			case B_OK:
24 			case B_PARTIAL_READ:
25 				result = target->WriteExactly(buffer, sizeRead);
26 				break;
27 		}
28 	} while(result == B_OK && sizeRead > 0);
29 
30 	return result;
31 }
32 
33 
34 ConstraintedDataIO::ConstraintedDataIO(BDataIO* delegate, size_t limit)
35 	:
36 	fDelegate(delegate),
37 	fLimit(limit)
38 {
39 }
40 
41 
42 ConstraintedDataIO::~ConstraintedDataIO()
43 {
44 }
45 
46 
47 ssize_t
48 ConstraintedDataIO::Read(void* buffer, size_t size)
49 {
50 	if (size > fLimit)
51 		size = fLimit;
52 
53 	ssize_t actualRead = fDelegate->Read(buffer, size);
54 
55 	if (actualRead > 0)
56 		fLimit -= actualRead;
57 
58 	return actualRead;
59 }
60 
61 
62 ssize_t
63 ConstraintedDataIO::Write(const void* buffer, size_t size)
64 {
65 	return B_NOT_SUPPORTED;
66 }
67 
68 
69 status_t
70 ConstraintedDataIO::Flush()
71 {
72 	return B_OK;
73 }
74