• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <img_utils/FileOutput.h>
18 
19 #include <utils/Log.h>
20 
21 namespace android {
22 namespace img_utils {
23 
FileOutput(String8 path)24 FileOutput::FileOutput(String8 path) : mFp(NULL), mPath(path), mOpen(false) {}
25 
~FileOutput()26 FileOutput::~FileOutput() {
27     if (mOpen) {
28         ALOGW("%s: Destructor called with %s still open.", __FUNCTION__, mPath.string());
29         close();
30     }
31 }
32 
open()33 status_t FileOutput::open() {
34     if (mOpen) {
35         ALOGW("%s: Open called when file %s already open.", __FUNCTION__, mPath.string());
36         return OK;
37     }
38     mFp = ::fopen(mPath, "wb");
39     if (!mFp) {
40         ALOGE("%s: Could not open file %s", __FUNCTION__, mPath.string());
41         return BAD_VALUE;
42     }
43     mOpen = true;
44     return OK;
45 }
46 
write(const uint8_t * buf,size_t offset,size_t count)47 status_t FileOutput::write(const uint8_t* buf, size_t offset, size_t count) {
48     if (!mOpen) {
49         ALOGE("%s: Could not write file %s, file not open.", __FUNCTION__, mPath.string());
50         return BAD_VALUE;
51     }
52 
53     ::fwrite(buf + offset, sizeof(uint8_t), count, mFp);
54 
55     int error = ::ferror(mFp);
56     if (error != 0) {
57         ALOGE("%s: Error %d occurred while writing file %s.", __FUNCTION__, error, mPath.string());
58         return BAD_VALUE;
59     }
60     return OK;
61 }
62 
close()63 status_t FileOutput::close() {
64     if(!mOpen) {
65         ALOGW("%s: Close called when file %s already close.", __FUNCTION__, mPath.string());
66         return OK;
67     }
68 
69     status_t ret = OK;
70     if(::fclose(mFp) != 0) {
71         ALOGE("%s: Failed to close file %s.", __FUNCTION__, mPath.string());
72         ret = BAD_VALUE;
73     }
74     mOpen = false;
75     return ret;
76 }
77 
78 } /*namespace img_utils*/
79 } /*namespace android*/
80