javauis/mmapi_qt/baseline/src/outputstreamwriter.cpp
changeset 23 98ccebc37403
child 56 abc41079b313
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 /*
       
     2 * Copyright (c) 2002-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:  This class has JNI wrappers for CMMAOutputStream
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //#include "jutils.h"
       
    20 //#include "mmapiutils.h"
       
    21 #include "com_nokia_microedition_media_protocol_OutputStreamWriter.h"
       
    22 #include "cmmaoutputstream.h"
       
    23 #include "mmafunctionserver.h"
       
    24 
       
    25 // If eventsource is already disposed, then do nothing
       
    26 #define CHECK_HANDLE(x, j) { if ( !( x ) || ( ( x )->Players().Count() == 0 ) ) { return ( j ); } }
       
    27 
       
    28 LOCAL_C void ReadDataL(MMAFunctionServer* aEventSource,
       
    29                        TInt aPlayer,
       
    30                        CMMAOutputStream* aOutputStream,
       
    31                        TUint8* aOutputBuffer,
       
    32                        TInt* aOutputBufferSize,
       
    33                        TInt* aReadStatus)
       
    34 {
       
    35     if (!aEventSource->FindPlayer(aPlayer))
       
    36     {
       
    37         // Native object was already deleted
       
    38         User::Leave(KErrBadHandle);
       
    39     }
       
    40 
       
    41     aOutputStream->ReadDataL(aOutputBuffer, aOutputBufferSize, aReadStatus);
       
    42 }
       
    43 
       
    44 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_protocol_OutputStreamWriter__1readData
       
    45 (JNIEnv* aJni, jclass, jint aOutputStream, jint aEventSource,
       
    46  jbyteArray aOutputBuffer, jintArray aOutputBufferSize, jint aLength, jint aPlayerHandle)
       
    47 {
       
    48     MMAFunctionServer* eventSource = reinterpret_cast< MMAFunctionServer* >(aEventSource);
       
    49     CHECK_HANDLE(eventSource, KErrNone);
       
    50 
       
    51     CMMAOutputStream* outputStream = reinterpret_cast< CMMAOutputStream* >(aOutputStream);
       
    52 
       
    53     TInt readStatus;
       
    54     jbyte* outputBuffer = aJni->GetByteArrayElements(aOutputBuffer, NULL);
       
    55 
       
    56     // NULL is returned if there is not enough memory
       
    57     if (!outputBuffer)
       
    58     {
       
    59         return KErrNoMemory;
       
    60     }
       
    61 
       
    62     jint* outputBufferSize = aJni->GetIntArrayElements(aOutputBufferSize, NULL);
       
    63     if (!outputBufferSize)
       
    64     {
       
    65         // outputBuffer was already allocated
       
    66         aJni->ReleaseByteArrayElements(aOutputBuffer, outputBuffer, JNI_ABORT);
       
    67         return KErrNoMemory;
       
    68     }
       
    69 
       
    70     *outputBufferSize = aLength; // size of the buffer
       
    71 
       
    72     TInt err = eventSource->ExecuteTrap(ReadDataL,
       
    73                                         eventSource,
       
    74                                         aPlayerHandle,
       
    75                                         outputStream,
       
    76                                         (TUint8*) outputBuffer,
       
    77                                         outputBufferSize, // returns readed size
       
    78                                         &readStatus);
       
    79 
       
    80     aJni->ReleaseByteArrayElements(aOutputBuffer, outputBuffer, JNI_COMMIT);
       
    81     aJni->ReleaseIntArrayElements(aOutputBufferSize, outputBufferSize, JNI_COMMIT);
       
    82 
       
    83     if (err != KErrNone)
       
    84     {
       
    85         return err;
       
    86     }
       
    87 
       
    88     // Tells if there's more data available or not
       
    89     return readStatus;
       
    90 }
       
    91 
       
    92 //  END OF FILE