javacommons/utils/src.s60/javaoslayer.cpp
changeset 21 2a9601315dfc
child 23 98ccebc37403
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2007-2007 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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <dirent.h>
       
    19 #include <memory>
       
    20 #include <string.h> // Needed for strcmp
       
    21 #include <iostream>
       
    22 #include <fstream>
       
    23 #include <iomanip>
       
    24 #include <sstream>
       
    25 #include <e32err.h>
       
    26 #include <f32file.h>
       
    27 #include <hal.h>
       
    28 #include <flogger.h>
       
    29 #include "logger.h"
       
    30 #include "javaoslayer.h"
       
    31 #include "javasymbianoslayer.h"
       
    32 #include "libraryloaderexception.h"
       
    33 #include "dynamiclibloader.h"
       
    34 #include "javacommonutils.h"
       
    35 #include "s60commonutils.h"
       
    36 
       
    37 using namespace java::util;
       
    38 
       
    39 const char * const BOOT_CLASSPATH_MIDP_FILE = "midpodclist";
       
    40 const char * const BOOT_CLASSPATH_INSTALLER_FILE = "installerodclist";
       
    41 const char * const BOOT_CLASSPATH_TCKRUNNER_FILE = "tckrunnerodclist";
       
    42 
       
    43 _LIT(KStartUpTraceDir, "java\\full");
       
    44 _LIT(KStartUpTraceFile, "startup.txt");
       
    45 _LIT(KTraceSpaceExtra, "                                         ");
       
    46 
       
    47 OS_EXPORT void JavaOsLayer::startUpTrace(const std::string& header,
       
    48         int freeMem, int totalMem)
       
    49 {
       
    50     if (header.length() == 0)
       
    51     {
       
    52         _LIT(KDash, "-");
       
    53         RFileLogger::Write(KStartUpTraceDir, KStartUpTraceFile,
       
    54                            EFileLoggingModeAppend, KDash);
       
    55     }
       
    56     else
       
    57     {
       
    58         TTime time;
       
    59         time.HomeTime();
       
    60         const int maxStrLen = 41;
       
    61         TBuf<maxStrLen>  str;
       
    62         _LIT(KFormatStr, "%-B%:0%J%:1%T%:2%S%.%*C2%:3%+B");
       
    63         TRAP_IGNORE(time.FormatL(str, KFormatStr));
       
    64         int mem = 0;
       
    65         int err = HAL::Get(HALData::EMemoryRAMFree, mem);
       
    66         if (err != KErrNone)
       
    67         {
       
    68             mem = err;
       
    69         }
       
    70         HBufC* buf = stringToDes(header.c_str());
       
    71         const int maxLen = 41;
       
    72         int headerLen = Min(buf->Length(), maxLen);
       
    73         TPtrC headerPtr = buf->Left(headerLen);
       
    74         TPtrC spacePtr = KTraceSpaceExtra().Right(maxLen - headerLen);
       
    75 
       
    76         int heapUsageTotal;
       
    77         User::AllocSize(heapUsageTotal);
       
    78 
       
    79         _LIT(KOutStr, "%S%S Time(%S), SysMem(%d), pUsedHeap(%d), free(%d), total(%d)");
       
    80         RFileLogger::WriteFormat(KStartUpTraceDir, KStartUpTraceFile,
       
    81                                  EFileLoggingModeAppend,
       
    82                                  KOutStr,
       
    83                                  &headerPtr, &spacePtr, &str, mem, heapUsageTotal, freeMem, totalMem);
       
    84         delete buf;
       
    85     }
       
    86 }
       
    87 
       
    88 
       
    89 OS_EXPORT void JavaOsLayer::getOsSpecificLibName(std::string& result,
       
    90         const char* libName)
       
    91 {
       
    92 //    JELOG2(EUtils);
       
    93     if (libName)
       
    94     {
       
    95         result = libName;
       
    96     }
       
    97     else
       
    98     {
       
    99         ELOG(EUtils, "JavaOsLayer::getOsSpecificLibName() libname was null");
       
   100     }
       
   101 }
       
   102 
       
   103 OS_EXPORT void JavaOsLayer::getOsSpecificJavaRootPath(std::string& path)
       
   104 {
       
   105 //    JELOG2(EUtils);
       
   106     path = "c:\\java";
       
   107 }
       
   108 
       
   109 OS_EXPORT std::string& JavaOsLayer::getBinRoot(std::string& path, bool append)
       
   110 {
       
   111 //    JELOG2(EUtils);
       
   112     const char* start = ":\\sys\\bin\\";
       
   113     if (append)
       
   114     {
       
   115         path += start;
       
   116     }
       
   117     else
       
   118     {
       
   119         path = start+1;
       
   120     }
       
   121     return path;
       
   122 }
       
   123 
       
   124 OS_EXPORT std::string& JavaOsLayer::getResRoot(std::string& path, bool append)
       
   125 {
       
   126 //    JELOG2(EUtils);
       
   127     const char* start = ":\\resource\\java\\";
       
   128     if (append)
       
   129     {
       
   130         path += start;
       
   131     }
       
   132     else
       
   133     {
       
   134         path = start+1;
       
   135     }
       
   136     return path;
       
   137 }
       
   138 
       
   139 OS_EXPORT std::string& JavaOsLayer::getMidpRoot(std::string& path, bool append)
       
   140 {
       
   141 //    JELOG2(EUtils);
       
   142     const char* start = ":\\private\\102033E6\\";
       
   143 //    const char* start = ":\\private\\200211D9\\";
       
   144     if (append)
       
   145     {
       
   146         path += start;
       
   147     }
       
   148     else
       
   149     {
       
   150         path = start+1;
       
   151     }
       
   152     /*
       
   153         TUint32 uid = RProcess().SecureId().iId;
       
   154         std::ostringstream oss;
       
   155         oss<<std::hex<<uid;
       
   156         path += oss.str();
       
   157         path += "]\\";
       
   158     */
       
   159     return path;
       
   160 }
       
   161 
       
   162 OS_EXPORT std::string& JavaOsLayer::getJavaCaptainRoot(std::string& path, bool append)
       
   163 {
       
   164 //    JELOG2(EUtils);
       
   165     const char* start = ":\\private\\200211DC\\";
       
   166     if (append)
       
   167     {
       
   168         path += start;
       
   169     }
       
   170     else
       
   171     {
       
   172         path = start+1;
       
   173     }
       
   174     return path;
       
   175 }
       
   176 
       
   177 OS_EXPORT DriveId JavaOsLayer::getMidpDrive()
       
   178 {
       
   179 //    JELOG2(EUtils);
       
   180     int driveNum = 1;
       
   181     TUint16 driveLetter = RProcess().FileName()[0];
       
   182     RFs::CharToDrive(driveLetter, driveNum);
       
   183     char* drive = new char[2];
       
   184     drive[0] = *((char*)&driveLetter);
       
   185     drive[1] = 0;
       
   186     return DriveId(drive, driveNum);
       
   187 }
       
   188 
       
   189 
       
   190 OS_EXPORT void* JavaOsLayer::dlopen(const char* libName)
       
   191 {
       
   192     JELOG2(EUtils);
       
   193     RLibrary* libAccess = 0;
       
   194     if (libName)
       
   195     {
       
   196         libAccess = new RLibrary();
       
   197         if (libAccess)
       
   198         {
       
   199             HBufC* buf = stringToDes(libName);
       
   200             if (buf)
       
   201             {
       
   202                 int status = libAccess->Load(*buf);
       
   203                 LOG2(EUtils, EInfo, "JavaOsLayer::dlopen(%s), Handle: %X",
       
   204                      libName, libAccess);
       
   205                 delete buf;
       
   206                 if (status != KErrNone)
       
   207                 {
       
   208                     delete libAccess;
       
   209                     libAccess = 0;
       
   210                     throw LibraryLoaderException(OPENING_LIBRARY_FAILED, status,
       
   211                                                  "Error opening Symbian lib.",
       
   212                                                  __FILE__, __FUNCTION__, __LINE__);
       
   213 
       
   214                 }
       
   215                 else
       
   216                 {
       
   217                     //The code below makes RLibrary accessible from any thread.
       
   218                     // copies the handle without doing Open()
       
   219                     RLibrary duplicate=*libAccess;
       
   220                     // overwrite the original
       
   221                     status = libAccess->Duplicate(RThread());
       
   222                     duplicate.Close(); // close the original
       
   223                 }
       
   224             }
       
   225         }
       
   226     }
       
   227     else
       
   228     {
       
   229         ELOG(EUtils, "JavaOsLayer::dlopen() libname was null");
       
   230     }
       
   231     return libAccess;
       
   232 }
       
   233 
       
   234 OS_EXPORT void* JavaOsLayer::dlsym(void* handle, const char* name,
       
   235                                    bool containsByteCode)
       
   236 {
       
   237     JELOG2(EUtils);
       
   238     void* func = 0;
       
   239     if (handle && name)
       
   240     {
       
   241         RLibrary*  libAccess = reinterpret_cast<RLibrary*>(handle);
       
   242         LookupFunc lookup = (LookupFunc)libAccess->Lookup(containsByteCode?2:1);
       
   243         if (lookup)
       
   244         {
       
   245             func = (void*) lookup(name);
       
   246             LOG2(EUtils, EInfo, "JavaOsLayer::dlsym() fuction name: %s, "
       
   247                  "fPtr: %X", name, func);
       
   248             if (func == 0)
       
   249             {
       
   250                 ELOG1(EUtils, "dlsym failed for %s.", name);
       
   251             }
       
   252         }
       
   253         else
       
   254         {
       
   255             ELOG1(EUtils,
       
   256                   "No lookup method in ordinal 1 when looking for %s.", name);
       
   257         }
       
   258     }
       
   259     else
       
   260     {
       
   261         ELOG2(EUtils, "JavaOsLayer::dlsym() null argument handle: %X, name %X",
       
   262               handle, name);
       
   263     }
       
   264     return func;
       
   265 }
       
   266 
       
   267 OS_EXPORT int JavaOsLayer::dlclose(void* handle)
       
   268 {
       
   269     JELOG2(EUtils);
       
   270     if (handle)
       
   271     {
       
   272         RLibrary*  libAccess = reinterpret_cast<RLibrary*>(handle);
       
   273         libAccess->Close();
       
   274         delete libAccess;
       
   275     }
       
   276     else
       
   277     {
       
   278         ELOG(EUtils, "dlclose failed, null handle");
       
   279     }
       
   280     return 0;
       
   281 }
       
   282 OS_EXPORT void JavaOsLayer::bootClassPath(std::list<std::wstring>& odcFiles,
       
   283         std::list<std::wstring>& bcpEntites,
       
   284         const int pathType)
       
   285 {
       
   286     /*
       
   287      * getResRoot requires drive letter but VerifiedFileNameL adds correct one.
       
   288      * C drive used that it is checked first and if file is found that is used
       
   289      * after that z is checked.
       
   290      */
       
   291     std::string path = "c";
       
   292     JavaOsLayer::getResRoot(path, true);
       
   293 
       
   294     if (BOOT_CLASSPATH_MIDP == pathType)
       
   295     {
       
   296         path.append(BOOT_CLASSPATH_MIDP_FILE);
       
   297     }
       
   298     else if (BOOT_CLASSPATH_INSTALLER == pathType)
       
   299     {
       
   300         path.append(BOOT_CLASSPATH_INSTALLER_FILE);
       
   301     }
       
   302     else if (BOOT_CLASSPATH_TCKRUNNER == pathType)
       
   303     {
       
   304         path.append(BOOT_CLASSPATH_TCKRUNNER_FILE);
       
   305     }
       
   306     else
       
   307     {
       
   308         path.append(BOOT_CLASSPATH_MIDP_FILE);
       
   309     }
       
   310 
       
   311     std::auto_ptr<HBufC>pathDes(stringToDes(path.c_str()));
       
   312     TFileName realPathName;
       
   313     TRAPD(err, realPathName = S60CommonUtils::VerifiedFileNameL(*pathDes));
       
   314 
       
   315     if (KErrNone == err)
       
   316     {
       
   317         std::wstring temp((wchar_t*)realPathName.PtrZ());
       
   318         std::string odcFileName(temp.begin(), temp.end());
       
   319 
       
   320         std::vector<std::string> romClasspathEntries;
       
   321         std::string dir_path("Z:\\resource\\java\\jvm\\lib\\jrt");
       
   322 
       
   323         listDirectory(dir_path, romClasspathEntries);  // This is always needed.
       
   324 
       
   325         if (odcFileName[0] == 'c' || odcFileName[0] == 'C')  // IAD deployed
       
   326         {
       
   327             std::vector<std::string> updatedClasspathEntries;
       
   328             listOdcFile(path, updatedClasspathEntries);  // Read all ODC files from odc file
       
   329 
       
   330             std::string::size_type const delimiter = path.rfind("\\");
       
   331             path = path.substr(0, delimiter);  // IAD ODC file directory
       
   332 
       
   333             populateCPEntries((path + "\\jvm\\lib\\jrt\\"), updatedClasspathEntries, odcFiles, bcpEntites);
       
   334 
       
   335             std::vector<std::string> romDeltaEntries;
       
   336 
       
   337             // Add all ROM entries that are not deployed using IAD.
       
   338             deltaCPEntries(romClasspathEntries, updatedClasspathEntries, romDeltaEntries);
       
   339 
       
   340             // Add ROM only entries
       
   341             populateCPEntries((dir_path + "\\"), romDeltaEntries, odcFiles, bcpEntites);
       
   342         }
       
   343         else  // Classpath only at ROM so using dir listing.
       
   344         {
       
   345             populateCPEntries((dir_path + "\\"), romClasspathEntries, odcFiles, bcpEntites);
       
   346         }
       
   347     }
       
   348     else
       
   349     {
       
   350         ELOG1(EUtils, "Cannot get real path name for: '%s'", path.c_str());
       
   351     }
       
   352 }
       
   353 
       
   354 void JavaOsLayer::listDirectory(std::string& aDirPath, std::vector<std::string>& aEntries)
       
   355 {
       
   356     DIR *pDIR = opendir(aDirPath.c_str());    // open directory
       
   357 
       
   358     if (pDIR == NULL)
       
   359     {
       
   360         std::string errorMsg("Cannot read ROM ODC files: ");
       
   361         errorMsg.append(strerror(errno));
       
   362         ELOG(EUtils, errorMsg.c_str());
       
   363         throw ExceptionBase(errorMsg, __FILE__, __FUNCTION__, __LINE__);
       
   364     }
       
   365 
       
   366     struct dirent* pDirEnt = readdir(pDIR);
       
   367     while (pDirEnt != NULL)
       
   368     {
       
   369         aEntries.push_back(std::string(pDirEnt->d_name));
       
   370         pDirEnt = readdir(pDIR);
       
   371     }
       
   372 
       
   373     closedir(pDIR);    // Release the open directory
       
   374 }
       
   375 
       
   376 void JavaOsLayer::listOdcFile(std::string& filePath, std::vector<std::string>& entries)
       
   377 {
       
   378     std::ifstream odcFile;
       
   379     odcFile.open(filePath.c_str(), std::ifstream::in);
       
   380 
       
   381     if (odcFile)
       
   382     {
       
   383         std::string line;
       
   384         while (std::getline(odcFile, line))
       
   385         {
       
   386             // Trim enter if exists.
       
   387             std::string::size_type const enter = line.find_last_not_of("\n");
       
   388             entries.push_back(line.substr(0, enter));
       
   389         }
       
   390         odcFile.close();
       
   391     }
       
   392     else
       
   393     {
       
   394         ELOG1(EUtils, "Cannot read file: '%s'", filePath.c_str());
       
   395     }
       
   396 }
       
   397 
       
   398 void JavaOsLayer::populateCPEntries(const std::string& pathPrefix,
       
   399                                     const std::vector<std::string>& entries,
       
   400                                     std::list<std::wstring>& odcFiles,
       
   401                                     std::list<std::wstring>& bcpEntites)
       
   402 {
       
   403     std::vector<std::string>::const_iterator iter;
       
   404 
       
   405     for (iter = entries.begin(); iter != entries.end(); ++iter)
       
   406     {
       
   407         std::string entryName((*iter));
       
   408         entryName.insert(0, pathPrefix);    // Full path needed.
       
   409 
       
   410         std::wstring wideEntryName = L"";
       
   411 
       
   412         try
       
   413         {
       
   414             wideEntryName = JavaCommonUtils::utf8ToWstring(entryName.c_str());
       
   415         }
       
   416         catch (ExceptionBase& eb)
       
   417         {
       
   418             // Suppress error to skip invalid ones.
       
   419             ELOG2(EUtils, "utf8->wstring failed on cp entry: '%s' exp: %s ",
       
   420                   entryName.c_str(), eb.toString().c_str());
       
   421         }
       
   422         size_t pos = wideEntryName.rfind(L".odc");
       
   423         bool isOdcFile = (pos != std::string::npos) &&
       
   424                          (pos == (wideEntryName.length() - 4));
       
   425         if (isOdcFile)
       
   426         {
       
   427             odcFiles.push_back(wideEntryName);
       
   428         }
       
   429         else
       
   430         {
       
   431             bcpEntites.push_back(wideEntryName);
       
   432         }
       
   433     }
       
   434 }
       
   435 
       
   436 void JavaOsLayer::deltaCPEntries(const std::vector<std::string>& romEntries,
       
   437                                  const std::vector<std::string>& iadEntries,
       
   438                                  std::vector<std::string>& deltaEntries)
       
   439 {
       
   440     std::vector<std::string>::const_iterator iter;  // ROM CP iter
       
   441     std::vector<std::string>::const_iterator matchIter; // IAD CP iter
       
   442     std::vector<std::string> romDeltaEntries;
       
   443 
       
   444     // Iterate through all rom entries
       
   445     for (iter = romEntries.begin(); iter != romEntries.end(); ++iter)
       
   446     {
       
   447         bool match = false;
       
   448         // and check if they do not match to IAD ones add to romDeltaEntries.
       
   449         for (matchIter = iadEntries.begin(); matchIter != iadEntries.end(); ++matchIter)
       
   450         {
       
   451             if ((*matchIter) == (*iter))
       
   452             {
       
   453                 match = true;
       
   454                 break;
       
   455             }
       
   456         }
       
   457 
       
   458         if (!match)
       
   459         {
       
   460             deltaEntries.push_back((*iter));
       
   461         }
       
   462     }
       
   463 }
       
   464 
       
   465 OS_EXPORT FuncPtr findMethod(const char* funcName,
       
   466                              const FuncTable funcTable[],
       
   467                              int tableSize)
       
   468 {
       
   469 //    JELOG2(EUtils);
       
   470     int       res = 0;
       
   471     int       mid = 0;
       
   472     int       top = 0;
       
   473     int       bottom = tableSize-1;
       
   474 
       
   475     if (funcName == 0)
       
   476     {
       
   477         ELOG(EUtils, "findMethod failed, null funcName");
       
   478         return 0;
       
   479     }
       
   480     if (funcTable == 0)
       
   481     {
       
   482         ELOG(EUtils, "findMethod failed, null funcTable");
       
   483         return 0;
       
   484     }
       
   485 
       
   486     // Loop while the number of the items left in the list is greater
       
   487     // than 2.  Each iteration will split the number of items left to search
       
   488     // in half
       
   489     while ((bottom - top) > 1)
       
   490     {
       
   491         // This case handles the normal serach case where the number of
       
   492         // items left to search is greater than 2
       
   493         mid = (top + bottom) / 2;
       
   494         res = strcmp(funcName, funcTable[mid].mFuncName);
       
   495         if (res == 0)
       
   496         {
       
   497             return((FuncPtr) funcTable[mid].mFuncAddr);
       
   498         }
       
   499         if (res > 0)
       
   500         {
       
   501             top = mid;
       
   502         }
       
   503         else
       
   504         {
       
   505             bottom = mid;
       
   506         }
       
   507     }
       
   508 
       
   509     // If there are two items left in the list then the bottom item should be
       
   510     // checked for a match
       
   511     if (bottom != top)
       
   512     {
       
   513         // Check the bottom item to see if it is a match
       
   514         res=strcmp(funcName, funcTable[bottom].mFuncName);
       
   515         if (res == 0)
       
   516         {
       
   517             return ((FuncPtr) funcTable[bottom].mFuncAddr);
       
   518         }
       
   519     }
       
   520 
       
   521     // Check the top item to see if it is a match
       
   522     res=strcmp(funcName, funcTable[top].mFuncName);
       
   523 
       
   524     if (res == 0)
       
   525     {
       
   526         return ((FuncPtr) funcTable[top].mFuncAddr);
       
   527     }
       
   528 
       
   529     // Neither the top or bottom items were a match so the
       
   530     // method must not exist in the file
       
   531     return 0;
       
   532 }
       
   533 
       
   534 OS_EXPORT HBufC* stringToDes(const char* str)
       
   535 {
       
   536 //    JELOG2(EUtils);
       
   537     HBufC* resultBuf = 0;
       
   538     try
       
   539     {
       
   540         if (str)
       
   541         {
       
   542             int len = strlen(str);
       
   543             resultBuf = HBufC::New(len + 1);
       
   544             TPtr ptr = resultBuf->Des();
       
   545             TPtr8 ptr8((TUint8 *)str, len);
       
   546             ptr8.SetLength(len);
       
   547             ptr.Copy(ptr8);
       
   548             ptr.ZeroTerminate();
       
   549         }
       
   550     }
       
   551     catch (...)
       
   552     {
       
   553         ELOG(EUtils, "stringToDes, exception");
       
   554     }
       
   555     return resultBuf;
       
   556 }
       
   557 
       
   558 OS_EXPORT wchar_t* desToWstring(TPtr16& aDes)
       
   559 {
       
   560     return (wchar_t*) aDes.PtrZ();
       
   561 }
       
   562 
       
   563 OS_EXPORT TAppVersion wstringToAppVersion(const std::wstring& aVersionString)
       
   564 {
       
   565     TAppVersion appVersion;
       
   566     char* temp = JavaCommonUtils::wstringToUtf8(aVersionString);
       
   567     std::string versionStr = temp;
       
   568 
       
   569     std::string::size_type idx = 0;
       
   570     idx = versionStr.find(".", idx);
       
   571 
       
   572     // 00.00.00 --> 00 00.00
       
   573     if (idx != std::string::npos)
       
   574     {
       
   575         versionStr.replace(idx, 1, " ");
       
   576     }
       
   577 
       
   578     idx = versionStr.find(".", idx + 1);
       
   579 
       
   580     // 00 00.00 --> 00 00 00
       
   581     if (idx != std::string::npos)
       
   582     {
       
   583         versionStr.replace(idx, 1, " ");
       
   584     }
       
   585 
       
   586     int major = 0;
       
   587     int minor = 0;
       
   588     int build = 0;
       
   589     int result = sscanf(
       
   590                      versionStr.c_str(), "%d %d %d", &major, &minor, &build);
       
   591 
       
   592     if (major > 0)
       
   593     {
       
   594         appVersion.iMajor = major;
       
   595     }
       
   596 
       
   597     if (minor > 0)
       
   598     {
       
   599         appVersion.iMinor = minor;
       
   600     }
       
   601 
       
   602     if (build > 0)
       
   603     {
       
   604         appVersion.iBuild = build;
       
   605     }
       
   606     delete[] temp;
       
   607 
       
   608     return appVersion;
       
   609 }
       
   610 
       
   611 OS_EXPORT HBufC* wstringToBuf(const std::wstring& aString)
       
   612 {
       
   613     HBufC* stringBuf = HBufC::New(aString.size());
       
   614     if (stringBuf != 0)
       
   615     {
       
   616         TPtr16 namePtr(stringBuf->Des());
       
   617         namePtr.Append((const TUint16*)aString.c_str(), aString.size());
       
   618     }
       
   619     return stringBuf;
       
   620 }
       
   621 
       
   622 /**
       
   623  *
       
   624  */
       
   625 OS_EXPORT java::util::Uid& TUidToUid(const TUid& aId,java::util::Uid& aOutUid)
       
   626 {
       
   627 //  JELOG2(EUtils);
       
   628     if (0 == aId.iUid)
       
   629         return aOutUid;
       
   630 
       
   631     std::wstringstream stream;
       
   632     stream.fill('0');
       
   633     stream << std::setw(8) << std::hex << (int)aId.iUid; // codescanner::leave
       
   634 
       
   635     std::wstring idAsStr;
       
   636     idAsStr.reserve(11);
       
   637     idAsStr.append(L"[");
       
   638     idAsStr.append(stream.str());
       
   639     idAsStr.append(L"]");
       
   640     Uid tmpUid(idAsStr);
       
   641     aOutUid = tmpUid;
       
   642     return aOutUid;
       
   643 }
       
   644 
       
   645 /**
       
   646  *
       
   647  */
       
   648 OS_EXPORT TInt uidToTUid(const java::util::Uid& aUid,TUid& aOutId)
       
   649 {
       
   650 //  JELOG2(EUtils);
       
   651     long long tmpInt = 0;
       
   652     std::wstring uidAsStr = aUid.toString();
       
   653     if (0 == uidAsStr.size())
       
   654         return KErrArgument;
       
   655 
       
   656     JavaCommonUtils::trimWstring(uidAsStr,L'\n');
       
   657     JavaCommonUtils::trimWstring(uidAsStr,L'\t');
       
   658     JavaCommonUtils::trimWstring(uidAsStr,L' ');
       
   659 
       
   660     if ((10 != uidAsStr.size()) && (8 != uidAsStr.size()))
       
   661         return KErrArgument;
       
   662 
       
   663     if (('[' == uidAsStr.at(0)) && (']' == uidAsStr.at(uidAsStr.size()-1)))
       
   664     {
       
   665         if (10 != uidAsStr.size())
       
   666             return KErrArgument;
       
   667         std::wstring plainNumStr = uidAsStr.substr(1,uidAsStr.size()-2);
       
   668         std::wstringstream stream(plainNumStr);
       
   669         stream >> std::hex >> tmpInt; // codescanner::leave
       
   670         if ((false == stream.eof()) || (stream.fail()))
       
   671             return KErrArgument;
       
   672     }
       
   673     else
       
   674     {
       
   675         std::wstringstream stream(uidAsStr);
       
   676         stream >> std::hex >> tmpInt; // codescanner::leave
       
   677         if ((false == stream.eof()) || (stream.fail()))
       
   678             return KErrArgument;
       
   679     }
       
   680 
       
   681     if ((tmpInt > 0xEFFFFFFF) || (tmpInt < 0))
       
   682         return KErrArgument;
       
   683     aOutId.iUid = tmpInt;
       
   684     return KErrNone;
       
   685 }
       
   686