1 /*-------------------------------------------------------------------------
2 * drawElements Stream Library
3 * ---------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Stream copying thread
22 *//*--------------------------------------------------------------------*/
23 #include "deStreamCpyThread.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
cpyStream(void * arg)28 void cpyStream (void* arg)
29 {
30 deStreamCpyThread* thread = (deStreamCpyThread*)arg;
31 deUint8* buffer = malloc(sizeof(deUint8) * (size_t)thread->bufferSize);
32
33 for(;;)
34 {
35 deInt32 read = 0;
36 deInt32 written = 0;
37 deStreamResult readResult = DE_STREAMRESULT_ERROR;
38
39 readResult = deInStream_read(thread->input, buffer, thread->bufferSize, &read);
40 DE_ASSERT(readResult != DE_STREAMRESULT_ERROR);
41 while (written < read)
42 {
43 deInt32 wrote = 0;
44 deOutStream_write(thread->output, buffer, read - written, &wrote);
45
46 /* \todo [mika] Handle errors */
47 written += wrote;
48 }
49
50 if (readResult == DE_STREAMRESULT_END_OF_STREAM)
51 {
52 break;
53 }
54 }
55
56 deOutStream_flush(thread->output);
57 free(buffer);
58 }
59
deStreamCpyThread_create(deInStream * input,deOutStream * output,deInt32 bufferSize)60 deStreamCpyThread* deStreamCpyThread_create (deInStream* input, deOutStream* output, deInt32 bufferSize)
61 {
62 deStreamCpyThread* thread = malloc(sizeof(deStreamCpyThread));
63
64 DE_ASSERT(thread);
65 DE_ASSERT(input);
66 DE_ASSERT(output);
67
68 thread->input = input;
69 thread->output = output;
70 thread->bufferSize = bufferSize;
71 thread->thread = deThread_create(cpyStream, thread, DE_NULL);
72
73 return thread;
74 }
75
deStreamCpyThread_destroy(deStreamCpyThread * thread)76 void deStreamCpyThread_destroy (deStreamCpyThread* thread)
77 {
78 deThread_destroy(thread->thread);
79
80 free(thread);
81 }
82
deStreamCpyThread_join(deStreamCpyThread * thread)83 void deStreamCpyThread_join (deStreamCpyThread* thread)
84 {
85 deThread_join(thread->thread);
86 }
87