2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FMedia_Util.cpp
20 * @brief This file contains the utility APIs of Media namespace.
24 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
27 #include "FMedia_MediaUtil.h"
29 using namespace Tizen::Base;
30 using namespace Tizen::Io;
32 namespace Tizen { namespace Media {
34 Tizen::Base::ByteBuffer*
35 _MediaUtil::FileToBufferN(const Tizen::Base::String &path, int maxSize)
38 std::unique_ptr<ByteBuffer> pBuf;
42 SysTryReturn(NID_MEDIA, !path.IsEmpty(), null, E_FILE_NOT_FOUND,
43 "[E_FILE_NOT_FOUND] path is empty");
44 SysTryReturn(NID_MEDIA, path.GetLength() > 0 && path.GetLength() <= PATH_MAX, null, E_INVALID_ARG,
45 "[E_INVALID_ARG] Given filePath length is zero or exceeds system limitations.");
46 SysSecureTryReturn(NID_MEDIA, File::IsFileExist(path), null, E_FILE_NOT_FOUND,
47 "[E_FILE_NOT_FOUND] filePath:%ls", path.GetPointer());
49 r = file.Construct(path, L"rb");
50 SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
51 "[%s] file.Construct failed. File name (%ls)", GetErrorMessage(r), path.GetPointer());
53 file.Seek(FILESEEKPOSITION_END, 0);
55 file.Seek(FILESEEKPOSITION_BEGIN, 0);
57 SysSecureTryReturn(NID_MEDIA, size > 0, null, E_DATA_NOT_FOUND,
58 "[E_DATA_NOT_FOUND] File size is 0: %ls", path.GetPointer());
60 pBuf.reset(new (std::nothrow) ByteBuffer());
61 SysTryReturn(NID_MEDIA, pBuf.get() != null, null, E_OUT_OF_MEMORY,
62 "[E_OUT_OF_MEMORY] Failed to create new ByteBuffer.");
64 if (maxSize == 0 || size <= maxSize) // read whole data
66 r = pBuf->Construct(size);
67 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ByteBuffer Construct failed for %d bytes.",
68 GetErrorMessage(r), size);
70 r = file.Read(*pBuf.get());
71 SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. File.Read failed: %ls",
72 GetErrorMessage(r), path.GetPointer());
76 r = pBuf->Construct(maxSize);
77 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ByteBuffer Construct failed for %d bytes.",
78 GetErrorMessage(r), maxSize);
80 r = file.Read(*pBuf.get());
81 SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. File.Read failed: %ls",
82 GetErrorMessage(r), path.GetPointer());
87 SetLastResult(E_SUCCESS);
88 return pBuf.release();
92 _MediaUtil::BufferToFile(const Tizen::Base::ByteBuffer &srcBuf,
93 const Tizen::Base::String &dstPath, bool overwrite)
99 if (File::IsFileExist(dstPath))
103 File::Remove(dstPath);
107 return E_FILE_ALREADY_EXIST;
111 r = file.Construct(dstPath, "wb", true);
112 SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. File.Construct failed for %ls",
113 GetErrorMessage(r), dstPath.GetPointer());
114 r = file.Write(srcBuf);
115 SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. File.Write failed for %ls",
116 GetErrorMessage(r), dstPath.GetPointer());