javauis/mmapi_qt/baseline/src/cmmaoutputstream.cpp
changeset 26 dc7c549001d5
parent 23 98ccebc37403
child 67 63b81d807542
equal deleted inserted replaced
23:98ccebc37403 26:dc7c549001d5
    50     return self;
    50     return self;
    51 }
    51 }
    52 
    52 
    53 
    53 
    54 void CMMAOutputStream::CreateL(CMMAOutputStream** aOutputStream,
    54 void CMMAOutputStream::CreateL(CMMAOutputStream** aOutputStream,
    55 																MMAFunctionServer* aEventServer,
    55                                MMAFunctionServer* aEventServer,
    56                                JNIEnv* aJniEnv,
    56                                JNIEnv* aJniEnv,
    57                                MMMAEventPoster* aEventPoster,
    57                                MMMAEventPoster* aEventPoster,
    58                                jobject aJavaOutputStreamWriter)
    58                                jobject aJavaOutputStreamWriter)
    59 {
    59 {
    60 
    60 
    61     // JNI interface pointer can't be passed to different thread, so
    61     // JNI interface pointer can't be passed to different thread, so
    62     // it is needed to get valid JNI interface pointer for Event Server thread
    62     // it is needed to get valid JNI interface pointer for Event Server thread
    63  	aJniEnv = aEventServer->getValidJniEnv();
    63     aJniEnv = aEventServer->getValidJniEnv();
    64 
    64 
    65     *aOutputStream = NewL(aJniEnv, aEventPoster, aJavaOutputStreamWriter);
    65     *aOutputStream = NewL(aJniEnv, aEventPoster, aJavaOutputStreamWriter);
    66 }
    66 }
    67 
    67 
    68 
    68 
    69 CMMAOutputStream::~CMMAOutputStream()
    69 CMMAOutputStream::~CMMAOutputStream()
    70 {
    70 {
    71     LOG( EJavaMMAPI, EInfo, "MMA::CMMAOutputStream::~");
    71     LOG(EJavaMMAPI, EInfo, "MMA::CMMAOutputStream::~");
    72 
    72 
    73     // If write event is in the event server, it cannot be deleted.
    73     // If write event is in the event server, it cannot be deleted.
    74     // Thus set the event to be deleted when event dispatch is called.
    74     // Thus set the event to be deleted when event dispatch is called.
    75     if (iWriteEvent &&
    75     if (iWriteEvent &&
    76             iWriteEvent->State() == CMMAOutputStreamEvent::EMMAEventActive)
    76             iWriteEvent->State() == CMMAOutputStreamEvent::EMMAEventActive)
    81     {
    81     {
    82         delete iWriteEvent;
    82         delete iWriteEvent;
    83     }
    83     }
    84 
    84 
    85     delete iData;
    85     delete iData;
    86     LOG( EJavaMMAPI, EInfo, "MMA::CMMAOutputStream::~ OK");
    86     LOG(EJavaMMAPI, EInfo, "MMA::CMMAOutputStream::~ OK");
    87 }
    87 }
    88 
    88 
    89 
    89 
    90 // Default constructor
    90 // Default constructor
    91 CMMAOutputStream::CMMAOutputStream(MMMAEventPoster* aEventPoster)
    91 CMMAOutputStream::CMMAOutputStream(MMMAEventPoster* aEventPoster)
    92         : iEventSource(aEventPoster),
    92         : iEventSource(aEventPoster),
    93         iPtr(NULL, 0)
    93         iPtr(NULL, 0)
    94 {
    94 {
    95     LOG( EJavaMMAPI, EInfo, "MMA::CMMAOutputStream constructed");
    95     LOG(EJavaMMAPI, EInfo, "MMA::CMMAOutputStream constructed");
    96 }
    96 }
    97 
    97 
    98 
    98 
    99 void CMMAOutputStream::ConstructL(JNIEnv* aJNIEnv,
    99 void CMMAOutputStream::ConstructL(JNIEnv* aJNIEnv,
   100                                   jobject aJavaOutputStreamWriter)
   100                                   jobject aJavaOutputStreamWriter)
   101 {
   101 {
   102     LOG( EJavaMMAPI, EInfo, "CMMAOutputStream::ConstructL()");
   102     LOG(EJavaMMAPI, EInfo, "CMMAOutputStream::ConstructL()");
   103 
   103 
   104     // void write( int aLength, int aStatus ) method in OutputStreamWriter
   104     // void write( int aLength, int aStatus ) method in OutputStreamWriter
   105     jmethodID classMethodID =
   105     jmethodID classMethodID =
   106         aJNIEnv->GetMethodID(
   106         aJNIEnv->GetMethodID(
   107             aJNIEnv->GetObjectClass(aJavaOutputStreamWriter),
   107             aJNIEnv->GetObjectClass(aJavaOutputStreamWriter),
   108             "write",
   108             "write",
   109             "(II)V");
   109             "(II)V");
   110     if (!classMethodID)
   110     if (!classMethodID)
   111     {
   111     {
   112         LOG( EJavaMMAPI, EInfo, "CMMAOutputStream::ConstructL: Cannot find java method");
   112         LOG(EJavaMMAPI, EInfo, "CMMAOutputStream::ConstructL: Cannot find java method");
   113         User::Panic(_L("Java method write(II)V not found"), KErrGeneral);
   113         User::Panic(_L("Java method write(II)V not found"), KErrGeneral);
   114     }
   114     }
   115 
   115 
   116     iWriteEvent = new(ELeave) CMMAOutputStreamEvent(classMethodID,
   116     iWriteEvent = new(ELeave) CMMAOutputStreamEvent(classMethodID,
   117             aJavaOutputStreamWriter);
   117             aJavaOutputStreamWriter);
   128 
   128 
   129     // Status code to be returned
   129     // Status code to be returned
   130     TInt status = KErrNone;
   130     TInt status = KErrNone;
   131 
   131 
   132     TPtr8 buffer(aOutputData, *aBufferSize);
   132     TPtr8 buffer(aOutputData, *aBufferSize);
   133     LOG1( EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL SIZE BEFORE READL %d", buffer.Length());
   133     LOG1(EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL SIZE BEFORE READL %d", buffer.Length());
   134     LOG1( EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL BYTES AVAILABLE %d", iPtr.Length());
   134     LOG1(EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL BYTES AVAILABLE %d", iPtr.Length());
   135 
   135 
   136     // Bytes available in native buffer
   136     // Bytes available in native buffer
   137     TInt bytesAvailable = iPtr.Length();
   137     TInt bytesAvailable = iPtr.Length();
   138 
   138 
   139     // Maximum size that can be written to Java buffer
   139     // Maximum size that can be written to Java buffer
   164     *aBufferSize = buffer.Length();
   164     *aBufferSize = buffer.Length();
   165 
   165 
   166     // Move pointer to next read position.
   166     // Move pointer to next read position.
   167     iPtr = iPtr.Mid(writeLength);
   167     iPtr = iPtr.Mid(writeLength);
   168 
   168 
   169     LOG1( EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL SIZE AFTER READL %d", buffer.Length());
   169     LOG1(EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL SIZE AFTER READL %d", buffer.Length());
   170     LOG1( EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL RETURN %d", status);
   170     LOG1(EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL RETURN %d", status);
   171     LOG1( EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL dataLeft %d", iPtr.Length());
   171     LOG1(EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL dataLeft %d", iPtr.Length());
   172 
   172 
   173     if (iPtr.Length() == 0)
   173     if (iPtr.Length() == 0)
   174     {
   174     {
   175         // All data is read and buffer is not needed anymore.
   175         // All data is read and buffer is not needed anymore.
   176         delete iData;
   176         delete iData;
   179     *aReadStatus = status;
   179     *aReadStatus = status;
   180 }
   180 }
   181 
   181 
   182 void CMMAOutputStream::WriteL(const TDesC8& aData)
   182 void CMMAOutputStream::WriteL(const TDesC8& aData)
   183 {
   183 {
   184     LOG1( EJavaMMAPI, EInfo, "CMMAOutputStream::WriteL data size = %d", aData.Size());
   184     LOG1(EJavaMMAPI, EInfo, "CMMAOutputStream::WriteL data size = %d", aData.Size());
   185     if (iData)
   185     if (iData)
   186     {
   186     {
   187         // Previous data was not readed from the stream.
   187         // Previous data was not readed from the stream.
   188         User::Leave(KErrNotReady);
   188         User::Leave(KErrNotReady);
   189     }
   189     }
   195         delete iData;
   195         delete iData;
   196         iData = data;
   196         iData = data;
   197         iPtr.Set(iData->Des());
   197         iPtr.Set(iData->Des());
   198 
   198 
   199         // Set java event
   199         // Set java event
   200         LOG1( EJavaMMAPI, EInfo, "CMMAOutputStream::WriteL: available data: %d", iData->Length());
   200         LOG1(EJavaMMAPI, EInfo, "CMMAOutputStream::WriteL: available data: %d", iData->Length());
   201         iWriteEvent->SetLength(iData->Length());
   201         iWriteEvent->SetLength(iData->Length());
   202         iWriteEvent->SetStatus(EMoreData);
   202         iWriteEvent->SetStatus(EMoreData);
   203     }
   203     }
   204     else
   204     else
   205     {
   205     {
   206         LOG( EJavaMMAPI, EInfo, "CMMAOutputStream::WriteL Zero length data");
   206         LOG(EJavaMMAPI, EInfo, "CMMAOutputStream::WriteL Zero length data");
   207         iWriteEvent->SetLength(0);
   207         iWriteEvent->SetLength(0);
   208         iWriteEvent->SetStatus(ECompleted);
   208         iWriteEvent->SetStatus(ECompleted);
   209     }
   209     }
   210     iEventSource->PostEvent(iWriteEvent, CMMAEvent::ENotifyPriority);
   210     iEventSource->PostEvent(iWriteEvent, CMMAEvent::ENotifyPriority);
   211 }
   211 }