|
1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // System includes |
|
15 // Local includes |
|
16 // |
|
17 // |
|
18 |
|
19 // Class signatures |
|
20 #include "cwspfiledatasupplier.h" |
|
21 |
|
22 |
|
23 // Constants used in this file |
|
24 _LIT(KFileSystemRoot,"C:\\"); |
|
25 const TInt KChunkSize = 64; |
|
26 |
|
27 /** Implementation of class 'CWspFileDataSupplier'. |
|
28 */ |
|
29 |
|
30 |
|
31 CWspFileDataSupplier* CWspFileDataSupplier::NewL() |
|
32 { |
|
33 CWspFileDataSupplier* me = new(ELeave)CWspFileDataSupplier(); |
|
34 CleanupStack::PushL(me); |
|
35 me->ConstructL(); |
|
36 CleanupStack::Pop(me); |
|
37 return me; |
|
38 } |
|
39 |
|
40 CWspFileDataSupplier* CWspFileDataSupplier::NewL(const TDesC& aFileName) |
|
41 { |
|
42 CWspFileDataSupplier* me = new(ELeave)CWspFileDataSupplier(); |
|
43 CleanupStack::PushL(me); |
|
44 me->ConstructL(aFileName); |
|
45 CleanupStack::Pop(me); |
|
46 return me; |
|
47 } |
|
48 |
|
49 CWspFileDataSupplier::~CWspFileDataSupplier() |
|
50 { |
|
51 delete iBuffer; |
|
52 iFileHnd.Close(); |
|
53 iFileSrvHnd.Close(); |
|
54 } |
|
55 |
|
56 void CWspFileDataSupplier::CleanUp(TAny* aDataSupplierItem) |
|
57 { |
|
58 delete STATIC_CAST(CWspFileDataSupplier*, aDataSupplierItem); |
|
59 } |
|
60 |
|
61 void CWspFileDataSupplier::SetFileL(const TDesC& aFileName) |
|
62 { |
|
63 // Close any already open file |
|
64 if (iFileOpen) |
|
65 { |
|
66 iFileHnd.Close(); |
|
67 iFileOpen = EFalse; |
|
68 } |
|
69 |
|
70 // Parse the name of the new file and open it |
|
71 iParsedFileName.Set(KFileSystemRoot, &aFileName, NULL); |
|
72 User::LeaveIfError(iFileHnd.Open(iFileSrvHnd, iParsedFileName.FullName(), EFileRead)); |
|
73 iFileOpen = ETrue; |
|
74 |
|
75 // Read from the request body file |
|
76 TPtr8 bufferPtr = iBuffer->Des(); |
|
77 |
|
78 // Read initially into the dynamic buffer. If we haven't hit the |
|
79 // end of file, then read more into a local buffer and append to the |
|
80 // dynamic one, expanding and reallocating where necessary |
|
81 TInt err = iFileHnd.Read(bufferPtr); |
|
82 TBuf8<KChunkSize> chunk; |
|
83 TBool done = EFalse; |
|
84 while (!done) |
|
85 { |
|
86 // If it's not at EOF, then the read should have been successful with no error |
|
87 User::LeaveIfError(err); |
|
88 |
|
89 // Read some more... detect EOF |
|
90 err = iFileHnd.Read(chunk); |
|
91 done = (chunk.Length() == 0); |
|
92 if ((err == KErrNone) && !done) |
|
93 { |
|
94 // Determine whether we need to grow the dynamic buffer |
|
95 if (chunk.Length() > (bufferPtr.MaxLength() - bufferPtr.Length())) |
|
96 { |
|
97 HBufC8* oldbuffer = iBuffer; |
|
98 iBuffer = oldbuffer->ReAllocL(oldbuffer->Length() + KChunkSize); |
|
99 bufferPtr = iBuffer->Des(); |
|
100 } |
|
101 |
|
102 // Append from the local buffer |
|
103 bufferPtr.Append(chunk); |
|
104 } |
|
105 } |
|
106 } |
|
107 |
|
108 TBool CWspFileDataSupplier::GetNextDataPart(TPtrC8& aDataPart) |
|
109 { |
|
110 __ASSERT_ALWAYS(iFileOpen, User::Invariant()); |
|
111 // Return the data in one chunk |
|
112 aDataPart.Set(*iBuffer); |
|
113 return ETrue; |
|
114 } |
|
115 |
|
116 void CWspFileDataSupplier::ReleaseData() |
|
117 { |
|
118 __ASSERT_ALWAYS(iFileOpen, User::Invariant()); |
|
119 // Clear out the submit buffer |
|
120 TPtr8 bufferPtr = iBuffer->Des(); |
|
121 bufferPtr.Zero(); |
|
122 } |
|
123 |
|
124 TInt CWspFileDataSupplier::OverallDataSize() |
|
125 { |
|
126 __ASSERT_ALWAYS(iFileOpen, User::Invariant()); |
|
127 return iBuffer->Length(); |
|
128 } |
|
129 |
|
130 TInt CWspFileDataSupplier::Reset() |
|
131 { |
|
132 __ASSERT_ALWAYS(iFileOpen, User::Invariant()); |
|
133 return KErrNotSupported; |
|
134 } |
|
135 |
|
136 CWspFileDataSupplier::CWspFileDataSupplier() |
|
137 { |
|
138 } |
|
139 |
|
140 void CWspFileDataSupplier::ConstructL() |
|
141 { |
|
142 // Connect to the fileserver |
|
143 User::LeaveIfError(iFileSrvHnd.Connect()); |
|
144 |
|
145 // Make a buffer to hold chunks taken from the file |
|
146 iBuffer = HBufC8::NewMaxL(KChunkSize); |
|
147 } |
|
148 |
|
149 void CWspFileDataSupplier::ConstructL(const TDesC& aFileName) |
|
150 { |
|
151 // General set-up first |
|
152 ConstructL(); |
|
153 |
|
154 // Open the specified file |
|
155 SetFileL(aFileName); |
|
156 } |