Update of DRM framework.

  - Change "void" type of return value to "int" for returning status.
  - Add some of overloaded Java APIs which accept database Uri as input.
  - Add asynchronous APIs
  - Add OnEventListener and OnErrorListener for asynchronous APIs
  - Disable debug log
  - Change decrypt() API to accept an optional buffer needed by some of DRM schemes

Changes are incorporated by Sony Corporation.

Change-Id: I414a165e22cc79be6ea7cd28041788aa2b6b8f7c
diff --git a/drm/common/DrmConstraints.cpp b/drm/common/DrmConstraints.cpp
index 11ce410..4a4d798 100644
--- a/drm/common/DrmConstraints.cpp
+++ b/drm/common/DrmConstraints.cpp
@@ -75,12 +75,10 @@
 DrmConstraints::KeyIterator::KeyIterator(const DrmConstraints::KeyIterator& keyIterator)
     : mDrmConstraints(keyIterator.mDrmConstraints),
       mIndex(keyIterator.mIndex) {
-    LOGV("DrmConstraints::KeyIterator::KeyIterator");
 }
 
 DrmConstraints::KeyIterator& DrmConstraints::KeyIterator::operator=(
     const DrmConstraints::KeyIterator& keyIterator) {
-    LOGV("DrmConstraints::KeyIterator::operator=");
     mDrmConstraints = keyIterator.mDrmConstraints;
     mIndex = keyIterator.mIndex;
     return *this;
@@ -94,12 +92,10 @@
 DrmConstraints::Iterator::Iterator(const DrmConstraints::Iterator& iterator) :
     mDrmConstraints(iterator.mDrmConstraints),
     mIndex(iterator.mIndex) {
-    LOGV("DrmConstraints::Iterator::Iterator");
 }
 
 DrmConstraints::Iterator& DrmConstraints::Iterator::operator=(
     const DrmConstraints::Iterator& iterator) {
-    LOGV("DrmConstraints::Iterator::operator=");
     mDrmConstraints = iterator.mDrmConstraints;
     mIndex = iterator.mIndex;
     return *this;
diff --git a/drm/common/DrmEngineBase.cpp b/drm/common/DrmEngineBase.cpp
index 70398e8..17cdf54 100644
--- a/drm/common/DrmEngineBase.cpp
+++ b/drm/common/DrmEngineBase.cpp
@@ -52,7 +52,7 @@
     return onProcessDrmInfo(uniqueId, drmInfo);
 }
 
-void DrmEngineBase::saveRights(
+status_t DrmEngineBase::saveRights(
             int uniqueId, const DrmRights& drmRights,
             const String8& rightsPath, const String8& contentPath) {
     return onSaveRights(uniqueId, drmRights, rightsPath, contentPath);
@@ -74,14 +74,14 @@
     return onCheckRightsStatus(uniqueId, path, action);
 }
 
-void DrmEngineBase::consumeRights(
+status_t DrmEngineBase::consumeRights(
     int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
-    onConsumeRights(uniqueId, decryptHandle, action, reserve);
+    return onConsumeRights(uniqueId, decryptHandle, action, reserve);
 }
 
-void DrmEngineBase::setPlaybackStatus(
+status_t DrmEngineBase::setPlaybackStatus(
     int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) {
-    onSetPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
+    return onSetPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
 }
 
 bool DrmEngineBase::validateAction(
@@ -90,16 +90,16 @@
     return onValidateAction(uniqueId, path, action, description);
 }
 
-void DrmEngineBase::removeRights(int uniqueId, const String8& path) {
-    onRemoveRights(uniqueId, path);
+status_t DrmEngineBase::removeRights(int uniqueId, const String8& path) {
+    return onRemoveRights(uniqueId, path);
 }
 
-void DrmEngineBase::removeAllRights(int uniqueId) {
-    onRemoveAllRights(uniqueId);
+status_t DrmEngineBase::removeAllRights(int uniqueId) {
+    return onRemoveAllRights(uniqueId);
 }
 
-void DrmEngineBase::openConvertSession(int uniqueId, int convertId) {
-    onOpenConvertSession(uniqueId, convertId);
+status_t DrmEngineBase::openConvertSession(int uniqueId, int convertId) {
+    return onOpenConvertSession(uniqueId, convertId);
 }
 
 DrmConvertedStatus* DrmEngineBase::convertData(
@@ -120,24 +120,24 @@
     return onOpenDecryptSession(uniqueId, decryptHandle, fd, offset, length);
 }
 
-void DrmEngineBase::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
-    onCloseDecryptSession(uniqueId, decryptHandle);
+status_t DrmEngineBase::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
+    return onCloseDecryptSession(uniqueId, decryptHandle);
 }
 
-void DrmEngineBase::initializeDecryptUnit(
+status_t DrmEngineBase::initializeDecryptUnit(
     int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, const DrmBuffer* headerInfo) {
-    onInitializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
+    return onInitializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
 }
 
 status_t DrmEngineBase::decrypt(
     int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
-    const DrmBuffer* encBuffer, DrmBuffer** decBuffer) {
-    return onDecrypt(uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer);
+    const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
+    return onDecrypt(uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
 }
 
-void DrmEngineBase::finalizeDecryptUnit(
+status_t DrmEngineBase::finalizeDecryptUnit(
     int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
-    onFinalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
+    return onFinalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
 }
 
 ssize_t DrmEngineBase::pread(
diff --git a/drm/common/DrmInfoEvent.cpp b/drm/common/DrmInfoEvent.cpp
index eb58129..8d115a8 100644
--- a/drm/common/DrmInfoEvent.cpp
+++ b/drm/common/DrmInfoEvent.cpp
@@ -14,10 +14,6 @@
  * limitations under the License.
  */
 
-#define LOG_NDEBUG 0
-#define LOG_TAG "DrmInfoEvent"
-#include "utils/Log.h"
-
 #include <utils/String8.h>
 #include <drm/DrmInfoEvent.h>
 
diff --git a/drm/common/DrmRights.cpp b/drm/common/DrmRights.cpp
index dc1e6c5..3aecb3d 100644
--- a/drm/common/DrmRights.cpp
+++ b/drm/common/DrmRights.cpp
@@ -15,14 +15,21 @@
  */
 
 #include <drm/DrmRights.h>
+#include <ReadWriteUtils.h>
 
 using namespace android;
 
 DrmRights::DrmRights(const String8& rightsFilePath, const String8& mimeType,
-            const String8& accountId, const String8& subscriptionId) {
-    /**
-     * TODO Read DrmRights from rights file
-     */
+            const String8& accountId, const String8& subscriptionId) :
+    mMimeType(mimeType),
+    mAccountId(accountId),
+    mSubscriptionId(subscriptionId),
+    mRightsFromFile(NULL) {
+    int rightsLength = 0;
+    if (String8("") != rightsFilePath) {
+        rightsLength = ReadWriteUtils::readBytes(rightsFilePath, &mRightsFromFile);
+    }
+    mData = DrmBuffer(mRightsFromFile, rightsLength);
 }
 
 DrmRights::DrmRights(const DrmBuffer& rightsData, const String8& mimeType,
@@ -30,7 +37,12 @@
     mData(rightsData),
     mMimeType(mimeType),
     mAccountId(accountId),
-    mSubscriptionId(subscriptionId) {
+    mSubscriptionId(subscriptionId),
+    mRightsFromFile(NULL) {
+}
+
+DrmRights::~DrmRights() {
+    delete[] mRightsFromFile; mRightsFromFile = NULL;
 }
 
 const DrmBuffer& DrmRights::getData(void) const {
diff --git a/drm/common/DrmSupportInfo.cpp b/drm/common/DrmSupportInfo.cpp
index 35e83fc..ffc8953 100644
--- a/drm/common/DrmSupportInfo.cpp
+++ b/drm/common/DrmSupportInfo.cpp
@@ -42,7 +42,7 @@
 }
 
 bool DrmSupportInfo::isSupportedMimeType(const String8& mimeType) const {
-    for (int i = 0; i < mMimeTypeVector.size(); i++) {
+    for (unsigned int i = 0; i < mMimeTypeVector.size(); i++) {
         const String8 item = mMimeTypeVector.itemAt(i);
 
         if (String8("") != mimeType && item.find(mimeType) != -1) {
@@ -53,7 +53,7 @@
 }
 
 bool DrmSupportInfo::isSupportedFileSuffix(const String8& fileType) const {
-    for (int i = 0; i < mFileSuffixVector.size(); i++) {
+    for (unsigned int i = 0; i < mFileSuffixVector.size(); i++) {
         const String8 item = mFileSuffixVector.itemAt(i);
 
         if (String8("") != fileType && item.find(fileType) != -1) {
diff --git a/drm/common/IDrmIOService.cpp b/drm/common/IDrmIOService.cpp
index 7ce45e7..e44ca55 100644
--- a/drm/common/IDrmIOService.cpp
+++ b/drm/common/IDrmIOService.cpp
@@ -14,10 +14,6 @@
  * limitations under the License.
  */
 
-#define LOG_NDEBUG 0
-#define LOG_TAG "IDrmIOService"
-#include <utils/Log.h>
-
 #include <stdint.h>
 #include <sys/types.h>
 #include <binder/Parcel.h>
diff --git a/drm/common/IDrmManagerService.cpp b/drm/common/IDrmManagerService.cpp
index 4fc828a..c28527c 100644
--- a/drm/common/IDrmManagerService.cpp
+++ b/drm/common/IDrmManagerService.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#define LOG_NDEBUG 0
+//#define LOG_NDEBUG 0
 #define LOG_TAG "IDrmManagerService(Native)"
 #include <utils/Log.h>
 
@@ -36,6 +36,23 @@
 
 using namespace android;
 
+int BpDrmManagerService::addUniqueId(int uniqueId) {
+    LOGV("add uniqueid");
+    Parcel data, reply;
+    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
+    data.writeInt32(uniqueId);
+    remote()->transact(ADD_UNIQUEID, data, &reply);
+    return reply.readInt32();
+}
+
+void BpDrmManagerService::removeUniqueId(int uniqueId) {
+    LOGV("remove uniqueid");
+    Parcel data, reply;
+    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
+    data.writeInt32(uniqueId);
+    remote()->transact(REMOVE_UNIQUEID, data, &reply);
+}
+
 status_t BpDrmManagerService::loadPlugIns(int uniqueId) {
     LOGV("load plugins");
     Parcel data, reply;
@@ -237,7 +254,7 @@
     return drmInfo;
 }
 
-void BpDrmManagerService::saveRights(
+status_t BpDrmManagerService::saveRights(
             int uniqueId, const DrmRights& drmRights,
             const String8& rightsPath, const String8& contentPath) {
     LOGV("Save Rights");
@@ -264,6 +281,7 @@
     data.writeString8((contentPath == String8("")) ? String8("NULL") : contentPath);
 
     remote()->transact(SAVE_RIGHTS, data, &reply);
+    return reply.readInt32();
 }
 
 String8 BpDrmManagerService::getOriginalMimeType(int uniqueId, const String8& path) {
@@ -307,10 +325,10 @@
     return reply.readInt32();
 }
 
-void BpDrmManagerService::consumeRights(
+status_t BpDrmManagerService::consumeRights(
             int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
     LOGV("consumeRights");
-        Parcel data, reply;
+    Parcel data, reply;
 
     data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
     data.writeInt32(uniqueId);
@@ -330,9 +348,10 @@
     data.writeInt32(static_cast< int>(reserve));
 
     remote()->transact(CONSUME_RIGHTS, data, &reply);
+    return reply.readInt32();
 }
 
-void BpDrmManagerService::setPlaybackStatus(
+status_t BpDrmManagerService::setPlaybackStatus(
             int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) {
     LOGV("setPlaybackStatus");
     Parcel data, reply;
@@ -355,6 +374,7 @@
     data.writeInt32(position);
 
     remote()->transact(SET_PLAYBACK_STATUS, data, &reply);
+    return reply.readInt32();
 }
 
 bool BpDrmManagerService::validateAction(
@@ -375,7 +395,7 @@
     return static_cast<bool>(reply.readInt32());
 }
 
-void BpDrmManagerService::removeRights(int uniqueId, const String8& path) {
+status_t BpDrmManagerService::removeRights(int uniqueId, const String8& path) {
     LOGV("removeRights");
     Parcel data, reply;
 
@@ -384,9 +404,10 @@
     data.writeString8(path);
 
     remote()->transact(REMOVE_RIGHTS, data, &reply);
+    return reply.readInt32();
 }
 
-void BpDrmManagerService::removeAllRights(int uniqueId) {
+status_t BpDrmManagerService::removeAllRights(int uniqueId) {
     LOGV("removeAllRights");
     Parcel data, reply;
 
@@ -394,6 +415,7 @@
     data.writeInt32(uniqueId);
 
     remote()->transact(REMOVE_ALL_RIGHTS, data, &reply);
+    return reply.readInt32();
 }
 
 int BpDrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
@@ -517,15 +539,12 @@
     Parcel data, reply;
 
     const String16 interfaceDescriptor = IDrmManagerService::getInterfaceDescriptor();
-    LOGV("BpDrmManagerService::openDecryptSession: InterfaceDescriptor name is %s",
-        interfaceDescriptor.string());
     data.writeInterfaceToken(interfaceDescriptor);
     data.writeInt32(uniqueId);
     data.writeFileDescriptor(fd);
     data.writeInt32(offset);
     data.writeInt32(length);
 
-    LOGV("try to invoke remote onTransact() with code OPEN_DECRYPT_SESSION");
     remote()->transact(OPEN_DECRYPT_SESSION, data, &reply);
 
     DecryptHandle* handle = NULL;
@@ -546,7 +565,7 @@
     return handle;
 }
 
-void BpDrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
+status_t BpDrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
     LOGV("closeDecryptSession");
     Parcel data, reply;
 
@@ -571,9 +590,10 @@
         delete decryptHandle->decryptInfo; decryptHandle->decryptInfo = NULL;
     }
     delete decryptHandle; decryptHandle = NULL;
+    return reply.readInt32();
 }
 
-void BpDrmManagerService::initializeDecryptUnit(
+status_t BpDrmManagerService::initializeDecryptUnit(
             int uniqueId, DecryptHandle* decryptHandle,
             int decryptUnitId, const DrmBuffer* headerInfo) {
     LOGV("initializeDecryptUnit");
@@ -598,11 +618,12 @@
     data.write(headerInfo->data, headerInfo->length);
 
     remote()->transact(INITIALIZE_DECRYPT_UNIT, data, &reply);
+    return reply.readInt32();
 }
 
 status_t BpDrmManagerService::decrypt(
             int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
-            const DrmBuffer* encBuffer, DrmBuffer** decBuffer) {
+            const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
     LOGV("decrypt");
     Parcel data, reply;
 
@@ -626,6 +647,11 @@
     data.writeInt32(encBuffer->length);
     data.write(encBuffer->data, encBuffer->length);
 
+    if (NULL != IV) {
+        data.writeInt32(IV->length);
+        data.write(IV->data, IV->length);
+    }
+
     remote()->transact(DECRYPT, data, &reply);
 
     const status_t status = reply.readInt32();
@@ -638,7 +664,7 @@
     return status;
 }
 
-void BpDrmManagerService::finalizeDecryptUnit(
+status_t BpDrmManagerService::finalizeDecryptUnit(
             int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
     LOGV("finalizeDecryptUnit");
     Parcel data, reply;
@@ -660,6 +686,7 @@
     data.writeInt32(decryptUnitId);
 
     remote()->transact(FINALIZE_DECRYPT_UNIT, data, &reply);
+    return reply.readInt32();
 }
 
 ssize_t BpDrmManagerService::pread(
@@ -702,6 +729,23 @@
     LOGV("Entering BnDrmManagerService::onTransact with code %d", code);
 
     switch (code) {
+    case ADD_UNIQUEID:
+    {
+        LOGV("BnDrmManagerService::onTransact :ADD_UNIQUEID");
+        CHECK_INTERFACE(IDrmManagerService, data, reply);
+        int uniqueId = addUniqueId(data.readInt32());
+        reply->writeInt32(uniqueId);
+        return DRM_NO_ERROR;
+    }
+
+    case REMOVE_UNIQUEID:
+    {
+        LOGV("BnDrmManagerService::onTransact :REMOVE_UNIQUEID");
+        CHECK_INTERFACE(IDrmManagerService, data, reply);
+        removeUniqueId(data.readInt32());
+        return DRM_NO_ERROR;
+    }
+
     case LOAD_PLUGINS:
     {
         LOGV("BnDrmManagerService::onTransact :LOAD_PLUGINS");
@@ -711,7 +755,6 @@
 
         reply->writeInt32(status);
         return DRM_NO_ERROR;
-
     }
 
     case LOAD_PLUGINS_FROM_PATH:
@@ -745,7 +788,8 @@
         LOGV("BnDrmManagerService::onTransact :UNLOAD_PLUGINS");
         CHECK_INTERFACE(IDrmManagerService, data, reply);
 
-        status_t status = unloadPlugIns(data.readInt32());
+        const int uniqueId = data.readInt32();
+        status_t status = unloadPlugIns(uniqueId);
 
         reply->writeInt32(status);
         return DRM_NO_ERROR;
@@ -923,10 +967,11 @@
                             ((accountId == String8("NULL")) ? String8("") : accountId),
                             ((subscriptionId == String8("NULL")) ? String8("") : subscriptionId));
 
-        saveRights(uniqueId, drmRights,
+        const status_t status = saveRights(uniqueId, drmRights,
                             ((rightsPath == String8("NULL")) ? String8("") : rightsPath),
                             ((contentPath == String8("NULL")) ? String8("") : contentPath));
 
+        reply->writeInt32(status);
         return DRM_NO_ERROR;
     }
 
@@ -985,7 +1030,10 @@
             handle.decryptInfo->decryptBufferLength = bufferLength;
         }
 
-        consumeRights(uniqueId, &handle, data.readInt32(), static_cast<bool>(data.readInt32()));
+        const status_t status
+            = consumeRights(uniqueId, &handle, data.readInt32(),
+                static_cast<bool>(data.readInt32()));
+        reply->writeInt32(status);
 
         delete handle.decryptInfo; handle.decryptInfo = NULL;
         return DRM_NO_ERROR;
@@ -1011,7 +1059,9 @@
             handle.decryptInfo->decryptBufferLength = bufferLength;
         }
 
-        setPlaybackStatus(uniqueId, &handle, data.readInt32(), data.readInt32());
+        const status_t status
+            = setPlaybackStatus(uniqueId, &handle, data.readInt32(), data.readInt32());
+        reply->writeInt32(status);
 
         delete handle.decryptInfo; handle.decryptInfo = NULL;
         return DRM_NO_ERROR;
@@ -1037,7 +1087,8 @@
         LOGV("BnDrmManagerService::onTransact :REMOVE_RIGHTS");
         CHECK_INTERFACE(IDrmManagerService, data, reply);
 
-        removeRights(data.readInt32(), data.readString8());
+        const status_t status = removeRights(data.readInt32(), data.readString8());
+        reply->writeInt32(status);
 
         return DRM_NO_ERROR;
     }
@@ -1047,7 +1098,8 @@
         LOGV("BnDrmManagerService::onTransact :REMOVE_ALL_RIGHTS");
         CHECK_INTERFACE(IDrmManagerService, data, reply);
 
-        removeAllRights(data.readInt32());
+        const status_t status = removeAllRights(data.readInt32());
+        reply->writeInt32(status);
 
         return DRM_NO_ERROR;
     }
@@ -1207,7 +1259,8 @@
             handle->decryptInfo->decryptBufferLength = bufferLength;
         }
 
-        closeDecryptSession(uniqueId, handle);
+        const status_t status = closeDecryptSession(uniqueId, handle);
+        reply->writeInt32(status);
         return DRM_NO_ERROR;
     }
 
@@ -1237,7 +1290,9 @@
         DrmBuffer* headerInfo = NULL;
         headerInfo = new DrmBuffer((char *)data.readInplace(bufferSize), bufferSize);
 
-        initializeDecryptUnit(uniqueId, &handle, decryptUnitId, headerInfo);
+        const status_t status
+            = initializeDecryptUnit(uniqueId, &handle, decryptUnitId, headerInfo);
+        reply->writeInt32(status);
 
         delete handle.decryptInfo; handle.decryptInfo = NULL;
         delete headerInfo; headerInfo = NULL;
@@ -1274,7 +1329,14 @@
         buffer = new char[decBufferSize];
         DrmBuffer* decBuffer = new DrmBuffer(buffer, decBufferSize);
 
-        const status_t status = decrypt(uniqueId, &handle, decryptUnitId, encBuffer, &decBuffer);
+        DrmBuffer* IV = NULL;
+        if (0 != data.dataAvail()) {
+            const int ivBufferlength = data.readInt32();
+            IV = new DrmBuffer((char *)data.readInplace(ivBufferlength), ivBufferlength);
+        }
+
+        const status_t status
+            = decrypt(uniqueId, &handle, decryptUnitId, encBuffer, &decBuffer, IV);
 
         reply->writeInt32(status);
 
@@ -1286,6 +1348,7 @@
         delete encBuffer; encBuffer = NULL;
         delete decBuffer; decBuffer = NULL;
         delete [] buffer; buffer = NULL;
+        delete IV; IV = NULL;
         return DRM_NO_ERROR;
     }
 
@@ -1309,7 +1372,8 @@
             handle.decryptInfo->decryptBufferLength = bufferLength;
         }
 
-        finalizeDecryptUnit(uniqueId, &handle, data.readInt32());
+        const status_t status = finalizeDecryptUnit(uniqueId, &handle, data.readInt32());
+        reply->writeInt32(status);
 
         delete handle.decryptInfo; handle.decryptInfo = NULL;
         return DRM_NO_ERROR;
diff --git a/drm/common/IDrmServiceListener.cpp b/drm/common/IDrmServiceListener.cpp
index 0a69115..6eeea40 100644
--- a/drm/common/IDrmServiceListener.cpp
+++ b/drm/common/IDrmServiceListener.cpp
@@ -14,10 +14,6 @@
  * limitations under the License.
  */
 
-#define LOG_NDEBUG 0
-#define LOG_TAG "IDrmServiceListener"
-#include <utils/Log.h>
-
 #include <stdint.h>
 #include <sys/types.h>
 #include <binder/Parcel.h>
diff --git a/drm/common/ReadWriteUtils.cpp b/drm/common/ReadWriteUtils.cpp
index 4319c1c..7ec4fa2 100644
--- a/drm/common/ReadWriteUtils.cpp
+++ b/drm/common/ReadWriteUtils.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+//#define LOG_NDEBUG 0
+#define LOG_TAG "ReadWriteUtils"
+#include <utils/Log.h>
+
 #include <ReadWriteUtils.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
@@ -22,7 +26,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <utils/FileMap.h>
 #include <utils/String8.h>
 
 using namespace android;
@@ -39,18 +42,39 @@
         struct stat sb;
 
         if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
-            FileMap* fileMap = new FileMap();
-            if (fileMap->create(filePath.string(), fd, 0, sb.st_size, true)) {
-                char* addr = (char*)fileMap->getDataPtr();
-                string.append(addr, sb.st_size);
-                fileMap->release();
+            int length = sb.st_size;
+            char* bytes = new char[length];
+            if (length == read(fd, (void*) bytes, length)) {
+                string.append(bytes, length);
             }
+            delete bytes;
         }
         fclose(file);
     }
     return string;
 }
 
+int ReadWriteUtils::readBytes(const String8& filePath, char** buffer) {
+    FILE* file = NULL;
+    file = fopen(filePath.string(), "r");
+    int length = 0;
+
+    if (NULL != file) {
+        int fd = fileno(file);
+        struct stat sb;
+
+        if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
+            length = sb.st_size;
+            *buffer = new char[length];
+            if (length != read(fd, (void*) *buffer, length)) {
+                length = FAILURE;
+            }
+        }
+        fclose(file);
+    }
+    return length;
+}
+
 void ReadWriteUtils::writeToFile(const String8& filePath, const String8& data) {
     FILE* file = NULL;
     file = fopen(filePath.string(), "w+");
@@ -60,12 +84,8 @@
 
         int size = data.size();
         if (FAILURE != ftruncate(fd, size)) {
-            FileMap* fileMap = NULL;
-            fileMap = new FileMap();
-            if (fileMap->create(filePath.string(), fd, 0, size, false)) {
-                char* addr = (char*)fileMap->getDataPtr();
-                memcpy(addr, data.string(), size);
-                fileMap->release();
+            if (size != write(fd, data.string(), size)) {
+                LOGE("Failed to write the data to: %s", filePath.string());
             }
         }
         fclose(file);
@@ -79,20 +99,9 @@
     if (NULL != file) {
         int fd = fileno(file);
 
-        int offset = lseek(fd, 0, SEEK_END);
-        if (FAILURE != offset) {
-            int newEntrySize = data.size();
-            int fileSize = offset + newEntrySize;
-
-            if (FAILURE != ftruncate(fd, fileSize)) {
-                FileMap* fileMap = NULL;
-                fileMap = new FileMap();
-                if (fileMap->create(filePath.string(), fd, offset, fileSize, false)) {
-                    char* addr = (char*)fileMap->getDataPtr();
-                    memcpy(addr, data.string(), data.size());
-                    fileMap->release();
-                }
-            }
+        int size = data.size();
+        if (size != write(fd, data.string(), size)) {
+            LOGE("Failed to write the data to: %s", filePath.string());
         }
         fclose(file);
     }