2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of copy
24 #include <dpl/waitable_handle.h>
25 #include <dpl/binary_queue.h>
28 namespace // anonymous
30 const size_t DEFAULT_COPY_BUFFER_SIZE = 16768;
31 } // namespace anonymous
33 void Copy(AbstractWaitableInput *input, AbstractWaitableOutput *output)
38 BinaryQueueAutoPtr buffer;
41 // Try to get data immediately
42 buffer = input->Read(DEFAULT_COPY_BUFFER_SIZE);
44 // Do we need to wait for data ?
47 input->WaitableReadHandle(), WaitMode::Read);
51 if (buffer->Empty()) {
59 while (!buffer->Empty()) {
60 // Try to write all data immediately
61 size_t count = output->Write(*buffer, buffer->Size());
63 // Do we need to wait for writing data ?
66 output->WaitableWriteHandle(), WaitMode::Write);
71 buffer->Consume(count);
81 void Copy(AbstractWaitableInput *input,
82 AbstractWaitableOutput *output,
87 size_t bytesLeft = totalBytes;
89 while (bytesLeft > 0) {
90 BinaryQueueAutoPtr buffer;
92 // Copy at most left bytes
93 size_t bytesToCopy = bytesLeft >
94 DEFAULT_COPY_BUFFER_SIZE ? DEFAULT_COPY_BUFFER_SIZE : bytesLeft;
97 // Try to get data immediately
98 buffer = input->Read(bytesToCopy);
100 // Do we need to wait for data ?
103 input->WaitableReadHandle(), WaitMode::Read);
107 if (buffer->Empty()) {
108 ThrowMsg(CopyFailed, "Unexpected end of abstract input");
115 // Write out all data
116 while (!buffer->Empty()) {
117 // Try to write all data immediately
118 size_t count = output->Write(*buffer, buffer->Size());
120 // Do we need to wait for writing data ?
123 output->WaitableWriteHandle(), WaitMode::Write);
128 buffer->Consume(count);
133 Catch(DPL::Exception)