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: Class to handle OutputStream operations. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <logger.h> |
|
19 |
|
20 #include <JniEnvWrapper.h> |
|
21 #include "cmmaoutputstream.h" |
|
22 #include "mmmaeventposter.h" |
|
23 #include "cmmaoutputstreamevent.h" |
|
24 |
|
25 // CONSTANTS |
|
26 const TInt KMMACommit = -10000; // indicates native started commit |
|
27 |
|
28 CMMAOutputStream* CMMAOutputStream::NewL(JNIEnv* aJNIEnv, |
|
29 MMMAEventPoster* aEventPoster, |
|
30 jobject aJavaOutputStreamWriter |
|
31 ) |
|
32 { |
|
33 CMMAOutputStream* self = CMMAOutputStream::NewLC(aJNIEnv, |
|
34 aEventPoster, |
|
35 aJavaOutputStreamWriter |
|
36 ); |
|
37 CleanupStack::Pop(); |
|
38 return self; |
|
39 } |
|
40 |
|
41 |
|
42 CMMAOutputStream* CMMAOutputStream::NewLC(JNIEnv* aJNIEnv, |
|
43 MMMAEventPoster* aEventPoster, |
|
44 jobject aJavaOutputStreamWriter |
|
45 ) |
|
46 { |
|
47 CMMAOutputStream* self = new(ELeave) CMMAOutputStream(aEventPoster); |
|
48 CleanupStack::PushL(self); |
|
49 self->ConstructL(aJNIEnv, aJavaOutputStreamWriter); |
|
50 return self; |
|
51 } |
|
52 |
|
53 |
|
54 void CMMAOutputStream::CreateL(CMMAOutputStream** aOutputStream, |
|
55 MMAFunctionServer* aEventServer, |
|
56 JNIEnv* aJniEnv, |
|
57 MMMAEventPoster* aEventPoster, |
|
58 jobject aJavaOutputStreamWriter) |
|
59 { |
|
60 |
|
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 |
|
63 aJniEnv = aEventServer->getValidJniEnv(); |
|
64 |
|
65 *aOutputStream = NewL(aJniEnv, aEventPoster, aJavaOutputStreamWriter); |
|
66 } |
|
67 |
|
68 |
|
69 CMMAOutputStream::~CMMAOutputStream() |
|
70 { |
|
71 LOG( EJavaMMAPI, EInfo, "MMA::CMMAOutputStream::~"); |
|
72 |
|
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. |
|
75 if (iWriteEvent && |
|
76 iWriteEvent->State() == CMMAOutputStreamEvent::EMMAEventActive) |
|
77 { |
|
78 iWriteEvent->SetState(CMMAOutputStreamEvent::EMMADeleteEvent); |
|
79 } |
|
80 else |
|
81 { |
|
82 delete iWriteEvent; |
|
83 } |
|
84 |
|
85 delete iData; |
|
86 LOG( EJavaMMAPI, EInfo, "MMA::CMMAOutputStream::~ OK"); |
|
87 } |
|
88 |
|
89 |
|
90 // Default constructor |
|
91 CMMAOutputStream::CMMAOutputStream(MMMAEventPoster* aEventPoster) |
|
92 : iEventSource(aEventPoster), |
|
93 iPtr(NULL, 0) |
|
94 { |
|
95 LOG( EJavaMMAPI, EInfo, "MMA::CMMAOutputStream constructed"); |
|
96 } |
|
97 |
|
98 |
|
99 void CMMAOutputStream::ConstructL(JNIEnv* aJNIEnv, |
|
100 jobject aJavaOutputStreamWriter) |
|
101 { |
|
102 LOG( EJavaMMAPI, EInfo, "CMMAOutputStream::ConstructL()"); |
|
103 |
|
104 // void write( int aLength, int aStatus ) method in OutputStreamWriter |
|
105 jmethodID classMethodID = |
|
106 aJNIEnv->GetMethodID( |
|
107 aJNIEnv->GetObjectClass(aJavaOutputStreamWriter), |
|
108 "write", |
|
109 "(II)V"); |
|
110 if (!classMethodID) |
|
111 { |
|
112 LOG( EJavaMMAPI, EInfo, "CMMAOutputStream::ConstructL: Cannot find java method"); |
|
113 User::Panic(_L("Java method write(II)V not found"), KErrGeneral); |
|
114 } |
|
115 |
|
116 iWriteEvent = new(ELeave) CMMAOutputStreamEvent(classMethodID, |
|
117 aJavaOutputStreamWriter); |
|
118 } |
|
119 |
|
120 void CMMAOutputStream::ReadDataL(TUint8* aOutputData, |
|
121 TInt* aBufferSize, |
|
122 TInt* aReadStatus) |
|
123 { |
|
124 if (!iData) |
|
125 { |
|
126 User::Leave(KErrNotReady); |
|
127 } |
|
128 |
|
129 // Status code to be returned |
|
130 TInt status = KErrNone; |
|
131 |
|
132 TPtr8 buffer(aOutputData, *aBufferSize); |
|
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()); |
|
135 |
|
136 // Bytes available in native buffer |
|
137 TInt bytesAvailable = iPtr.Length(); |
|
138 |
|
139 // Maximum size that can be written to Java buffer |
|
140 TInt outputMaxLength = buffer.MaxLength(); |
|
141 |
|
142 // Length that can be written |
|
143 TInt writeLength = 0; |
|
144 |
|
145 if (outputMaxLength < bytesAvailable) |
|
146 { |
|
147 // Not all bytes can be written Java buffer |
|
148 writeLength = outputMaxLength; |
|
149 |
|
150 // Java need to read more data |
|
151 status = EMoreData; |
|
152 } |
|
153 else |
|
154 { |
|
155 // All bytes can be written to Java buffer |
|
156 writeLength = bytesAvailable; |
|
157 |
|
158 // All data is copied |
|
159 status = ECompleted; |
|
160 } |
|
161 |
|
162 // Copy maximum number of bytes to Java buffer |
|
163 buffer.Copy(iPtr.Left(writeLength)); |
|
164 *aBufferSize = buffer.Length(); |
|
165 |
|
166 // Move pointer to next read position. |
|
167 iPtr = iPtr.Mid(writeLength); |
|
168 |
|
169 LOG1( EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL SIZE AFTER READL %d", buffer.Length()); |
|
170 LOG1( EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL RETURN %d", status); |
|
171 LOG1( EJavaMMAPI, EInfo, " MMA::CMMAOutputStream::ReadDataL dataLeft %d", iPtr.Length()); |
|
172 |
|
173 if (iPtr.Length() == 0) |
|
174 { |
|
175 // All data is read and buffer is not needed anymore. |
|
176 delete iData; |
|
177 iData = NULL; |
|
178 } |
|
179 *aReadStatus = status; |
|
180 } |
|
181 |
|
182 void CMMAOutputStream::WriteL(const TDesC8& aData) |
|
183 { |
|
184 LOG1( EJavaMMAPI, EInfo, "CMMAOutputStream::WriteL data size = %d", aData.Size()); |
|
185 if (iData) |
|
186 { |
|
187 // Previous data was not readed from the stream. |
|
188 User::Leave(KErrNotReady); |
|
189 } |
|
190 |
|
191 if (aData.Length() > 0) |
|
192 { |
|
193 // Take a copy of new data. |
|
194 HBufC8* data = aData.AllocL(); |
|
195 delete iData; |
|
196 iData = data; |
|
197 iPtr.Set(iData->Des()); |
|
198 |
|
199 // Set java event |
|
200 LOG1( EJavaMMAPI, EInfo, "CMMAOutputStream::WriteL: available data: %d", iData->Length()); |
|
201 iWriteEvent->SetLength(iData->Length()); |
|
202 iWriteEvent->SetStatus(EMoreData); |
|
203 } |
|
204 else |
|
205 { |
|
206 LOG( EJavaMMAPI, EInfo, "CMMAOutputStream::WriteL Zero length data"); |
|
207 iWriteEvent->SetLength(0); |
|
208 iWriteEvent->SetStatus(ECompleted); |
|
209 } |
|
210 iEventSource->PostEvent(iWriteEvent, CMMAEvent::ENotifyPriority); |
|
211 } |
|
212 |
|
213 void CMMAOutputStream::Commit() |
|
214 { |
|
215 iWriteEvent->SetLength(0); |
|
216 iWriteEvent->SetStatus(KMMACommit); |
|
217 iEventSource->PostEvent(iWriteEvent, CMMAEvent::ENotifyPriority); |
|
218 } |
|
219 |
|
220 // END OF FILE |
|
221 |
|