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 FRAMEWORKS_INNERKITSIMPL_STREAM_INCLUDE_FILE_SOURCE_STREAM_H 17 #define FRAMEWORKS_INNERKITSIMPL_STREAM_INCLUDE_FILE_SOURCE_STREAM_H 18 19 #include <cstdio> 20 #include <memory> 21 #include <string> 22 #include "image/input_data_stream.h" 23 #include "source_stream.h" 24 25 namespace OHOS { 26 namespace Media { 27 class FileSourceStream : public SourceStream { 28 public: 29 static std::unique_ptr<FileSourceStream> CreateSourceStream(const std::string &pathName); 30 static std::unique_ptr<FileSourceStream> CreateSourceStream(const int fd); 31 static std::unique_ptr<FileSourceStream> CreateSourceStream( 32 const int fd, int32_t offset, int32_t size); 33 FileSourceStream(std::FILE *file, size_t size, size_t offset, size_t original); 34 ~FileSourceStream() override; 35 36 bool Read(uint32_t desiredSize, ImagePlugin::DataStreamBuffer &outData) override; 37 bool Read(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) override; 38 bool Peek(uint32_t desiredSize, ImagePlugin::DataStreamBuffer &outData) override; 39 bool Peek(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) override; 40 uint32_t Tell() override; 41 bool Seek(uint32_t position) override; 42 size_t GetStreamSize() override; 43 uint8_t *GetDataPtr() override; 44 uint32_t GetStreamType() override; 45 ImagePlugin::OutputDataStream* ToOutputDataStream() override; 46 int GetMMapFd(); 47 48 private: 49 DISALLOW_COPY_AND_MOVE(FileSourceStream); 50 bool GetData(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize); 51 bool GetData(uint32_t desiredSize, ImagePlugin::DataStreamBuffer &outData); 52 void ResetReadBuffer(); 53 std::FILE *filePtr_ = nullptr; 54 size_t fileSize_ = 0; 55 size_t fileOffset_ = 0; 56 size_t fileOriginalOffset_ = 0; 57 uint8_t *readBuffer_ = nullptr; 58 uint8_t *fileData_ = nullptr; 59 int mmapFd_ = -1; 60 bool mmapFdPassedOn_ = false; 61 }; 62 } // namespace Media 63 } // namespace OHOS 64 65 #endif // FRAMEWORKS_INNERKITSIMPL_STREAM_INCLUDE_FILE_SOURCE_STREAM_H 66