javauis/mmapi_akn/baseline/src/outputstreamwriter.cpp
branchRCL_3
changeset 19 04becd199f91
child 25 9ac0a0a7da70
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     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 "com_nokia_microedition_media_protocol_OutputStreamWriter.h"
       
    21 #include "cmmaoutputstream.h"
       
    22 #include "cmmaeventsource.h"
       
    23 
       
    24 // If eventsource is already disposed, then do nothing
       
    25 #define CHECK_HANDLE(x, j) { if ( !( x ) || ( ( x )->Players().Count() == 0 ) ) { return ( j ); } }
       
    26 
       
    27 LOCAL_C void ReadDataL(CMMAEventSource* aEventSource,
       
    28                        TInt aPlayer,
       
    29                        CMMAOutputStream* aOutputStream,
       
    30                        TUint8* aOutputBuffer,
       
    31                        TInt* aOutputBufferSize,
       
    32                        TInt* aReadStatus)
       
    33 {
       
    34     if (!aEventSource->FindPlayer(aPlayer))
       
    35     {
       
    36         // Native object was already deleted
       
    37         User::Leave(KErrBadHandle);
       
    38     }
       
    39 
       
    40     aOutputStream->ReadDataL(aOutputBuffer, aOutputBufferSize, aReadStatus);
       
    41 }
       
    42 
       
    43 JNIEXPORT jint JNICALL Java_com_nokia_microedition_media_protocol_OutputStreamWriter__1readData
       
    44 (JNIEnv* aJni, jclass, jint aOutputStream, jint aEventSource,
       
    45  jbyteArray aOutputBuffer, jintArray aOutputBufferSize, jint aLength, jint aPlayerHandle)
       
    46 {
       
    47     CMMAEventSource* eventSource = JavaUnhand< CMMAEventSource >(aEventSource);
       
    48 
       
    49     CHECK_HANDLE(eventSource, KErrNone);
       
    50 
       
    51     CMMAOutputStream* outputStream = JavaUnhand< 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