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
23 #include <dpl/waitable_handle.h>
24 #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)
39 BinaryQueueAutoPtr buffer;
43 // Try to get data immediately
44 buffer = input->Read(DEFAULT_COPY_BUFFER_SIZE);
46 // Do we need to wait for data ?
49 WaitForSingleHandle(input->WaitableReadHandle(), WaitMode::Read);
61 while (!buffer->Empty())
63 // Try to write all data immediately
64 size_t count = output->Write(*buffer, buffer->Size());
66 // Do we need to wait for writing data ?
69 WaitForSingleHandle(output->WaitableWriteHandle(), WaitMode::Write);
74 buffer->Consume(count);
78 Catch (DPL::Exception)
84 void Copy(AbstractWaitableInput *input, AbstractWaitableOutput *output, size_t totalBytes)
88 size_t bytesLeft = totalBytes;
92 BinaryQueueAutoPtr buffer;
94 // Copy at most left bytes
95 size_t bytesToCopy = bytesLeft > DEFAULT_COPY_BUFFER_SIZE ? DEFAULT_COPY_BUFFER_SIZE : bytesLeft;
99 // Try to get data immediately
100 buffer = input->Read(bytesToCopy);
102 // Do we need to wait for data ?
105 WaitForSingleHandle(input->WaitableReadHandle(), WaitMode::Read);
110 ThrowMsg(CopyFailed, "Unexpected end of abstract input");
116 // Write out all data
117 while (!buffer->Empty())
119 // Try to write all data immediately
120 size_t count = output->Write(*buffer, buffer->Size());
122 // Do we need to wait for writing data ?
125 WaitForSingleHandle(output->WaitableWriteHandle(), WaitMode::Write);
130 buffer->Consume(count);
135 Catch (DPL::Exception)