javamanager/javainstaller/installer/src.s60/utils/filewriter.cpp
changeset 21 2a9601315dfc
child 35 85266cc22c7f
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  The JNI code for Java Installer class FileWriter.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "com_nokia_mj_impl_installer_utils_FileWriter.h"
       
    19 #include "javacommonutils.h"
       
    20 #include "javasymbianoslayer.h" // for CleanupResetAndDestroyPushL
       
    21 #include "logger.h"
       
    22 #include "s60commonutils.h"
       
    23 
       
    24 #include <caf/caf.h>
       
    25 #include <caf/supplier.h>
       
    26 #ifdef RD_JAVA_S60_RELEASE_5_0
       
    27 #include <middleware/Oma2Agent.h>
       
    28 #else
       
    29 #include <mw/Oma2Agent.h>
       
    30 #endif // RD_JAVA_S60_RELEASE_5_0
       
    31 
       
    32 // NAMESPACE DECLARATION
       
    33 using namespace java;
       
    34 using namespace java::util;
       
    35 using namespace ContentAccess;
       
    36 
       
    37 IMPORT_C HBufC* CreateHBufCFromJavaStringLC(JNIEnv* aEnv, jstring aString);
       
    38 
       
    39 /**
       
    40  * See JNI method __1create.
       
    41  * This is an internal wrapper method for handling Leaves.
       
    42  */
       
    43 jint CreateL(
       
    44     JNIEnv *aEnv, jclass, jstring aMimeType,
       
    45     jstring aOutputPath, jstring aSuggestedFilename)
       
    46 {
       
    47     // Convert java strings to Symbian descriptors.
       
    48     HBufC *mimeType = CreateHBufCFromJavaStringLC(aEnv, aMimeType);
       
    49     HBufC *outputPath = CreateHBufCFromJavaStringLC(aEnv, aOutputPath);
       
    50     HBufC *suggestedFilename = CreateHBufCFromJavaStringLC(aEnv, aSuggestedFilename);
       
    51 
       
    52     // Create the content access agent.
       
    53     CSupplier* supplier = CSupplier::NewLC();
       
    54     CMetaDataArray* metaData = CMetaDataArray::NewLC();
       
    55 
       
    56     // Tell the agent which MIME type to use for the encrypted data.
       
    57     _LIT(KContentType, "Content-type");
       
    58     metaData->AddL(KContentType, *mimeType);
       
    59     supplier->SetOutputDirectoryL(*outputPath);
       
    60 
       
    61     // The KOmaImportContentType is a OMA DRM agent specific MIME type which
       
    62     // indicates that plain content is to be encrypted
       
    63     CImportFile* file = supplier->ImportFileL(
       
    64                             KOmaImportContentType, *metaData, *suggestedFilename);
       
    65 
       
    66     CleanupStack::PopAndDestroy(metaData);
       
    67     CleanupStack::PopAndDestroy(supplier);
       
    68     CleanupStack::PopAndDestroy(suggestedFilename);
       
    69     CleanupStack::PopAndDestroy(outputPath);
       
    70     CleanupStack::PopAndDestroy(mimeType);
       
    71 
       
    72     // Return handle to CImportFile. Utilize the fact that in Symbian
       
    73     // all pointer addresses are MOD 4 so the last 2 bits are 0
       
    74     // and can be shifted out. This way the returned handle is
       
    75     // always positive whereas Symbian error codes are always negative.
       
    76     return reinterpret_cast<TUint>(file)>>2;
       
    77 }
       
    78 
       
    79 /*
       
    80  * Class:     com_nokia_mj_impl_installer_utils_FileWriter
       
    81  * Method:    _create
       
    82  * Signature: (Ljava/lang/String;Ljava/lang/String;)I
       
    83  */
       
    84 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_installer_utils_FileWriter__1create
       
    85 (JNIEnv *aEnv, jclass aClass, jstring aMimeType, jstring aOutputPath, jstring aSuggestedFilename)
       
    86 {
       
    87     TInt result = KErrNone;
       
    88     TRAPD(err, result = CreateL(
       
    89                             aEnv, aClass, aMimeType, aOutputPath, aSuggestedFilename));
       
    90     if (KErrNone == err)
       
    91     {
       
    92         err = result;
       
    93     }
       
    94     return err;
       
    95 }
       
    96 
       
    97 /*
       
    98  * Class:     com_nokia_mj_impl_installer_utils_FileWriter
       
    99  * Method:    _write
       
   100  * Signature: (I[BII)I
       
   101  */
       
   102 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_installer_utils_FileWriter__1write
       
   103 (JNIEnv *aEnv, jclass, jint aHandle, jbyteArray aBytes, jint aOffset, jint aLength)
       
   104 {
       
   105     // Convert handle to pointer.
       
   106     CImportFile *file = reinterpret_cast<CImportFile*>(aHandle<<2);
       
   107 
       
   108     const TInt KWriteBufferSize = 2048;
       
   109     TBuf8<KWriteBufferSize> writeBuffer;
       
   110     TInt err = KErrNone;
       
   111     TInt index = aOffset;
       
   112     TInt limit = aOffset + aLength;
       
   113     TInt length = KWriteBufferSize;
       
   114     while (index < limit && KErrNone == err)
       
   115     {
       
   116         if (limit - index < KWriteBufferSize)
       
   117         {
       
   118             length = limit - index;
       
   119         }
       
   120         // Convert bytes from jbyteArray to TDesC8.
       
   121         (void)S60CommonUtils::CopyToNative(
       
   122             *aEnv, aBytes, index, length, writeBuffer);
       
   123         // Write data.
       
   124         err = file->WriteData(writeBuffer);
       
   125         index += length;
       
   126     }
       
   127 
       
   128     return err;
       
   129 }
       
   130 
       
   131 /**
       
   132  * See JNI method __1close.
       
   133  * This is an internal wrapper method for handling Leaves.
       
   134  */
       
   135 jint CloseL(JNIEnv *aEnv, jclass aClass, jint aHandle, jobject aOutputFilename)
       
   136 {
       
   137     // Convert handle to pointer.
       
   138     CImportFile *file = reinterpret_cast<CImportFile*>(aHandle<<2);
       
   139 
       
   140     // Complete writing and get output filename from the agent.
       
   141     TInt err = file->WriteDataComplete();
       
   142     // Initialize output filename to aOutputFilename parameter.
       
   143     if (KErrNone == err)
       
   144     {
       
   145         // Get class handle to StringBuffer
       
   146         aClass = aEnv->GetObjectClass(aOutputFilename);
       
   147         // Get method ID to StringBuffer StringBuffer.append(String)
       
   148         jmethodID methodID = aEnv->GetMethodID(
       
   149                                  aClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
       
   150         if (0 == methodID)
       
   151         {
       
   152             ELOG(EJavaInstaller,
       
   153                  "FileWriter__1close: StringBuffer.append not found");
       
   154             err = KErrGeneral;
       
   155         }
       
   156         else
       
   157         {
       
   158             CSupplierOutputFile &output = file->OutputFileL(0);
       
   159             TPtrC outputFilename = output.FileName();
       
   160             // Create new Java String.
       
   161             jstring jOutputFilename =
       
   162                 aEnv->NewString(outputFilename.Ptr(), outputFilename.Length());
       
   163             aEnv->CallObjectMethod(aOutputFilename, methodID, jOutputFilename);
       
   164         }
       
   165     }
       
   166 
       
   167     // Delete the CImportFile instance which is no longer used.
       
   168     delete file;
       
   169     return err;
       
   170 }
       
   171 
       
   172 /*
       
   173  * Class:     com_nokia_mj_impl_installer_utils_FileWriter
       
   174  * Method:    _close
       
   175  * Signature: (ILjava/lang/StringBuffer;)I
       
   176  */
       
   177 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_installer_utils_FileWriter__1close
       
   178 (JNIEnv *aEnv, jclass aClass, jint aHandle, jobject aOutputFilename)
       
   179 {
       
   180     TInt result = KErrNone;
       
   181     TRAPD(err, result = CloseL(aEnv, aClass, aHandle, aOutputFilename));
       
   182     if (KErrNone == err)
       
   183     {
       
   184         err = result;
       
   185     }
       
   186     return err;
       
   187 }