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>
29 namespace // anonymous
31 const size_t DEFAULT_COPY_BUFFER_SIZE = 16768;
32 } // namespace anonymous
34 void Copy(AbstractWaitableInput *input, AbstractWaitableOutput *output)
40 BinaryQueueAutoPtr buffer;
44 // Try to get data immediately
45 buffer = input->Read(DEFAULT_COPY_BUFFER_SIZE);
47 // Do we need to wait for data ?
50 WaitForSingleHandle(input->WaitableReadHandle(), WaitMode::Read);
62 while (!buffer->Empty())
64 // Try to write all data immediately
65 size_t count = output->Write(*buffer, buffer->Size());
67 // Do we need to wait for writing data ?
70 WaitForSingleHandle(output->WaitableWriteHandle(), WaitMode::Write);
75 buffer->Consume(count);
79 Catch (DPL::Exception)
85 void Copy(AbstractWaitableInput *input, AbstractWaitableOutput *output, size_t totalBytes)
89 size_t bytesLeft = totalBytes;
93 BinaryQueueAutoPtr buffer;
95 // Copy at most left bytes
96 size_t bytesToCopy = bytesLeft > DEFAULT_COPY_BUFFER_SIZE ? DEFAULT_COPY_BUFFER_SIZE : bytesLeft;
100 // Try to get data immediately
101 buffer = input->Read(bytesToCopy);
103 // Do we need to wait for data ?
106 WaitForSingleHandle(input->WaitableReadHandle(), WaitMode::Read);
111 ThrowMsg(CopyFailed, "Unexpected end of abstract input");
117 // Write out all data
118 while (!buffer->Empty())
120 // Try to write all data immediately
121 size_t count = output->Write(*buffer, buffer->Size());
123 // Do we need to wait for writing data ?
126 WaitForSingleHandle(output->WaitableWriteHandle(), WaitMode::Write);
131 buffer->Consume(count);
136 Catch (DPL::Exception)