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