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 22 namespace OHOS { 23 namespace ImagePlugin { 24 enum { 25 BUFFER_SOURCE_TYPE, 26 INPUT_STREAM_TYPE, 27 FILE_STREAM_TYPE, 28 }; 29 30 struct DataStreamBuffer { 31 // Out: output a pointer containing a data buffer. 32 // the buffer is managed by SourceStream, and the user does not need to alloc for a buffer himself. 33 // and the buffer is guaranteed to remain valid until the next operation on the SourceStream object. 34 const uint8_t *inputStreamBuffer = nullptr; 35 // Out: output buffer size. 36 uint32_t bufferSize = 0; 37 // Out: output actual valid data size in the buffer. 38 uint32_t dataSize = 0; 39 }; 40 41 class InputDataStream : NoCopyable { 42 public: 43 // extracts desiredSize bytes from the InputDataStream. 44 virtual bool Read(uint32_t desiredSize, DataStreamBuffer &outData) = 0; 45 46 // need to copy desiredSize bytes from the InputDataStream to outBuffer. 47 virtual bool Read(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) = 0; 48 49 // output the remaining data in the InputDataStream, without extracting it. 50 virtual bool Peek(uint32_t desiredSize, DataStreamBuffer &outData) = 0; 51 52 // need to copy desiredSize bytes from the InputDataStream to outBuffer and without extracting it. 53 virtual bool Peek(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) = 0; 54 55 // get the position of the current byte in the InputDataStream. 56 virtual uint32_t Tell() = 0; 57 58 // sets the position of the next byte to be extracted from the input stream. 59 virtual bool Seek(uint32_t position) = 0; 60 61 // get inherited class type GetStreamType()62 virtual uint32_t GetStreamType() 63 { 64 return -1; 65 } 66 67 // get raw pointer for BUFFER TYPE GetDataPtr()68 virtual uint8_t *GetDataPtr() 69 { 70 return nullptr; 71 } 72 73 // whether the stream data is completed or not. IsStreamCompleted()74 virtual bool IsStreamCompleted() 75 { 76 return true; 77 } 78 79 // get stream size GetStreamSize()80 virtual size_t GetStreamSize() 81 { 82 return 0; 83 } 84 ~InputDataStream()85 virtual ~InputDataStream() {} 86 }; 87 } // namespace ImagePlugin 88 } // namespace OHOS 89 90 #endif // INPUT_DATA_STREAM_H 91