1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef INPUT_DATA_STREAM_H
17 #define INPUT_DATA_STREAM_H
18 
19 #include <cstdint>
20 #include "nocopyable.h"
21 #include "output_data_stream.h"
22 
23 namespace OHOS {
24 namespace ImagePlugin {
25 enum {
26     BUFFER_SOURCE_TYPE,
27     INPUT_STREAM_TYPE,
28     FILE_STREAM_TYPE,
29 };
30 
31 struct DataStreamBuffer {
32     // Out: output a pointer containing a data buffer.
33     // the buffer is managed by SourceStream, and the user does not need to alloc for a buffer himself.
34     // and the buffer is guaranteed to remain valid until the next operation on the SourceStream object.
35     const uint8_t *inputStreamBuffer = nullptr;
36     // Out: output buffer size.
37     uint32_t bufferSize = 0;
38     // Out: output actual valid data size in the buffer.
39     uint32_t dataSize = 0;
40 };
41 
42 class InputDataStream : NoCopyable {
43 public:
44     // extracts desiredSize bytes from the InputDataStream.
45     virtual bool Read(uint32_t desiredSize, DataStreamBuffer &outData) = 0;
46 
47     // need to copy desiredSize bytes from the InputDataStream to outBuffer.
48     virtual bool Read(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) = 0;
49 
50     // output the remaining data in the InputDataStream, without extracting it.
51     virtual bool Peek(uint32_t desiredSize, DataStreamBuffer &outData) = 0;
52 
53     // need to copy desiredSize bytes from the InputDataStream to outBuffer and without extracting it.
54     virtual bool Peek(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) = 0;
55 
56     // get the position of the current byte in the InputDataStream.
57     virtual uint32_t Tell() = 0;
58 
59     // sets the position of the next byte to be extracted from the input stream.
60     virtual bool Seek(uint32_t position) = 0;
61 
62     // get inherited class type
GetStreamType()63     virtual uint32_t GetStreamType()
64     {
65         return -1;
66     }
67 
68     // get raw pointer for BUFFER TYPE
GetDataPtr()69     virtual uint8_t *GetDataPtr()
70     {
71         return nullptr;
72     }
73 
74     // get raw pointer mmap populate for FILE TYPE
GetDataPtr(bool populate)75     virtual uint8_t *GetDataPtr(bool populate)
76     {
77         return nullptr;
78     }
79 
80     // whether the stream data is completed or not.
IsStreamCompleted()81     virtual bool IsStreamCompleted()
82     {
83         return true;
84     }
85 
86     // get stream size
GetStreamSize()87     virtual size_t GetStreamSize()
88     {
89         return 0;
90     }
91 
ToOutputDataStream()92     virtual OutputDataStream* ToOutputDataStream()
93     {
94         return nullptr;
95     }
96 
~InputDataStream()97     virtual ~InputDataStream() {}
98 };
99 } // namespace ImagePlugin
100 } // namespace OHOS
101 
102 #endif // INPUT_DATA_STREAM_H
103