blob: 16b5b34131450655e0da3d4dcec4a3bd1993aa78 [file] [log] [blame]
aimitakeshi27ed8ad2010-07-29 10:12:27 +09001/*
2 * Copyright (C) 2010 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
Takeshi Aimi2272ee22010-09-20 23:40:41 +090017//#define LOG_NDEBUG 0
18#define LOG_TAG "ReadWriteUtils"
19#include <utils/Log.h>
20
aimitakeshi27ed8ad2010-07-29 10:12:27 +090021#include <ReadWriteUtils.h>
22#include <sys/mman.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <fcntl.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <unistd.h>
aimitakeshi27ed8ad2010-07-29 10:12:27 +090029#include <utils/String8.h>
30
31using namespace android;
32
Chih-Hung Hsieh92c6b822016-05-17 15:20:14 -070033#define FAILURE (-1)
aimitakeshi27ed8ad2010-07-29 10:12:27 +090034
35String8 ReadWriteUtils::readBytes(const String8& filePath) {
36 FILE* file = NULL;
37 file = fopen(filePath.string(), "r");
38
39 String8 string("");
40 if (NULL != file) {
41 int fd = fileno(file);
42 struct stat sb;
43
44 if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
Gloria Wanga2cd44c2010-11-19 15:19:36 -080045 off64_t length = sb.st_size;
Takeshi Aimi2272ee22010-09-20 23:40:41 +090046 char* bytes = new char[length];
47 if (length == read(fd, (void*) bytes, length)) {
48 string.append(bytes, length);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090049 }
Hung Nguyen0bf43842012-06-05 13:19:53 +020050 delete[] bytes;
aimitakeshi27ed8ad2010-07-29 10:12:27 +090051 }
52 fclose(file);
53 }
54 return string;
55}
56
Takeshi Aimi2272ee22010-09-20 23:40:41 +090057int ReadWriteUtils::readBytes(const String8& filePath, char** buffer) {
58 FILE* file = NULL;
59 file = fopen(filePath.string(), "r");
Gloria Wanga2cd44c2010-11-19 15:19:36 -080060 off64_t length = 0;
Takeshi Aimi2272ee22010-09-20 23:40:41 +090061
62 if (NULL != file) {
63 int fd = fileno(file);
64 struct stat sb;
65
66 if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
67 length = sb.st_size;
68 *buffer = new char[length];
69 if (length != read(fd, (void*) *buffer, length)) {
70 length = FAILURE;
71 }
72 }
73 fclose(file);
74 }
75 return length;
76}
77
aimitakeshi27ed8ad2010-07-29 10:12:27 +090078void ReadWriteUtils::writeToFile(const String8& filePath, const String8& data) {
79 FILE* file = NULL;
80 file = fopen(filePath.string(), "w+");
81
82 if (NULL != file) {
83 int fd = fileno(file);
84
85 int size = data.size();
86 if (FAILURE != ftruncate(fd, size)) {
Takeshi Aimi2272ee22010-09-20 23:40:41 +090087 if (size != write(fd, data.string(), size)) {
Steve Block29357bc2012-01-06 19:20:56 +000088 ALOGE("Failed to write the data to: %s", filePath.string());
aimitakeshi27ed8ad2010-07-29 10:12:27 +090089 }
90 }
91 fclose(file);
92 }
93}
94
95void ReadWriteUtils::appendToFile(const String8& filePath, const String8& data) {
96 FILE* file = NULL;
97 file = fopen(filePath.string(), "a+");
98
99 if (NULL != file) {
100 int fd = fileno(file);
101
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900102 int size = data.size();
103 if (size != write(fd, data.string(), size)) {
Steve Block29357bc2012-01-06 19:20:56 +0000104 ALOGE("Failed to write the data to: %s", filePath.string());
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900105 }
106 fclose(file);
107 }
108}
109