xref: /trunk/main/tools/source/stream/cachestr.cxx (revision 89b56da7)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_tools.hxx"
26 
27 #include <tools/debug.hxx>
28 #include <tools/stream.hxx>
29 #include <tools/cachestr.hxx>
30 #include <tools/tempfile.hxx>
31 
32 /*************************************************************************
33 |*
34 |*    SvCacheStream::SvCacheStream()
35 |*
36 |*    Beschreibung      STREAM.SDW
37 |*    Ersterstellung    OV 27.09.94
38 |*    Letzte Aenderung  OV 27.09.94
39 |*
40 *************************************************************************/
41 
SvCacheStream(sal_uIntPtr nMaxMemSize)42 SvCacheStream::SvCacheStream( sal_uIntPtr nMaxMemSize )
43 {
44 	if( !nMaxMemSize )
45 		nMaxMemSize = 20480;
46     SvStream::bIsWritable = sal_True;
47     nMaxSize		= nMaxMemSize;
48 	bPersistent		= sal_False;
49     pSwapStream		= 0;
50     pCurrentStream	= new SvMemoryStream( nMaxMemSize );
51     pTempFile       = 0;
52 }
53 
54 /*************************************************************************
55 |*
56 |*    SvCacheStream::SvCacheStream()
57 |*
58 |*    Beschreibung      STREAM.SDW
59 |*    Ersterstellung    OV 27.09.94
60 |*    Letzte Aenderung  OV 27.09.94
61 |*
62 *************************************************************************/
63 
SvCacheStream(const String & rFileName,sal_uIntPtr nExpectedSize,sal_uIntPtr nMaxMemSize)64 SvCacheStream::SvCacheStream( const String &rFileName,
65 							  sal_uIntPtr nExpectedSize,
66 						      sal_uIntPtr nMaxMemSize )
67 {
68 	if( !nMaxMemSize )
69 		nMaxMemSize = 20480;
70 
71 	if( nExpectedSize > nMaxMemSize )
72 		nExpectedSize = nMaxMemSize; // oder gleich in File schreiben
73 	else if( !nExpectedSize )
74 		nExpectedSize = 4096;
75 
76     SvStream::bIsWritable = sal_True;
77     nMaxSize		= nMaxMemSize;
78 	bPersistent		= sal_True;
79 	aFileName		= rFileName;
80     pSwapStream		= 0;
81     pCurrentStream	= new SvMemoryStream( nExpectedSize );
82     pTempFile       = 0;
83 }
84 
85 /*************************************************************************
86 |*
87 |*    SvCacheStream::~SvCacheStream()
88 |*
89 |*    Beschreibung      STREAM.SDW
90 |*    Ersterstellung    OV 27.09.94
91 |*    Letzte Aenderung  OV 27.09.94
92 |*
93 *************************************************************************/
94 
~SvCacheStream()95 SvCacheStream::~SvCacheStream()
96 {
97    	if( pCurrentStream != pSwapStream )
98     	delete pSwapStream;
99     delete pCurrentStream;
100 
101     if( pSwapStream && !bPersistent && pTempFile )
102     {
103         // temporaeres File loeschen
104         pTempFile->EnableKillingFile( sal_True );
105     }
106 
107     delete pTempFile;
108 }
109 
110 /*************************************************************************
111 |*
112 |*    SvCacheStream::SwapOut()
113 |*
114 |*    Beschreibung      STREAM.SDW
115 |*    Ersterstellung    OV 27.09.94
116 |*    Letzte Aenderung  OV 27.09.94
117 |*
118 *************************************************************************/
119 
SwapOut()120 void SvCacheStream::SwapOut()
121 {
122 	if( pCurrentStream != pSwapStream )
123 	{
124 		if( !pSwapStream && !aFileName.Len() )
125 		{
126 			if (aFilenameLinkHdl.IsSet())
127 			{
128 				// pSwapStream wird zum Schutz gegen Reentranz genutzt
129 				pSwapStream = pCurrentStream;
130 				Link aLink( aFilenameLinkHdl );
131 				aFilenameLinkHdl = Link();
132 				aLink.Call(this);
133 				// pSwapStream nur zuruecksetzen, wenn nicht ueber
134 				// SetSwapStream geaendert
135 				if( pSwapStream == pCurrentStream ) pSwapStream = 0;
136 			}
137 			else
138 			{
139                 pTempFile = new TempFile;
140                 aFileName = pTempFile->GetName();
141 			}
142 		}
143 
144 		sal_uIntPtr nPos = pCurrentStream->Tell();
145     	pCurrentStream->Seek( 0 );
146 		if( !pSwapStream )
147     		pSwapStream = new SvFileStream( aFileName, STREAM_READWRITE | STREAM_TRUNC );
148     	*pSwapStream << *pCurrentStream;
149     	pSwapStream->Flush();
150     	delete pCurrentStream;
151     	pCurrentStream = pSwapStream;
152     	pCurrentStream->Seek( nPos );
153 	}
154 }
155 
156 /*************************************************************************
157 |*
158 |*    SvCacheStream::GetData()
159 |*
160 |*    Beschreibung      STREAM.SDW
161 |*    Ersterstellung    OV 27.09.94
162 |*    Letzte Aenderung  OV 27.09.94
163 |*
164 *************************************************************************/
165 
GetData(void * pData,sal_uIntPtr nSize)166 sal_uIntPtr SvCacheStream::GetData( void* pData, sal_uIntPtr nSize )
167 {
168     return pCurrentStream->Read( pData, nSize );
169 }
170 
171 /*************************************************************************
172 |*
173 |*    SvCacheStream::PutData()
174 |*
175 |*    Beschreibung      STREAM.SDW
176 |*    Ersterstellung    OV 27.09.94
177 |*    Letzte Aenderung  OV 27.09.94
178 |*
179 *************************************************************************/
180 
PutData(const void * pData,sal_uIntPtr nSize)181 sal_uIntPtr SvCacheStream::PutData( const void* pData, sal_uIntPtr nSize )
182 {
183 	// lieber unnoetig auslagern als unnoetig umkopieren
184     if( pCurrentStream != pSwapStream
185 	    && pCurrentStream->Tell() + nSize > nMaxSize )
186         SwapOut();
187     return pCurrentStream->Write( pData, nSize );
188 }
189 
190 /*************************************************************************
191 |*
192 |*    SvCacheStream::SeekPos()
193 |*
194 |*    Beschreibung      STREAM.SDW
195 |*    Ersterstellung    OV 27.09.94
196 |*    Letzte Aenderung  OV 27.09.94
197 |*
198 *************************************************************************/
199 
SeekPos(sal_uIntPtr nPos)200 sal_uIntPtr SvCacheStream::SeekPos( sal_uIntPtr nPos )
201 {
202     return pCurrentStream->Seek( nPos );
203 }
204 
205 /*************************************************************************
206 |*
207 |*    SvCacheStream::FlushData()
208 |*
209 |*    Beschreibung      STREAM.SDW
210 |*    Ersterstellung    OV 27.09.94
211 |*    Letzte Aenderung  OV 27.09.94
212 |*
213 *************************************************************************/
214 
FlushData()215 void SvCacheStream::FlushData()
216 {
217     pCurrentStream->Flush();
218     if( pCurrentStream != pSwapStream
219 	    && ((SvMemoryStream*)pCurrentStream)->GetSize() > nMaxSize )
220         SwapOut();
221 }
222 
223 /*************************************************************************
224 |*
225 |*    SvCacheStream::GetStr()
226 |*
227 |*    Beschreibung      STREAM.SDW
228 |*    Ersterstellung    OV 27.09.94
229 |*    Letzte Aenderung  OV 27.09.94
230 |*
231 *************************************************************************/
232 
GetBuffer()233 const void* SvCacheStream::GetBuffer()
234 {
235     Flush();
236     if( pCurrentStream != pSwapStream )
237         return ((SvMemoryStream*)pCurrentStream)->GetData();
238     else
239         return 0;
240 }
241 
242 /*************************************************************************
243 |*
244 |*    SvCacheStream::SetSize()
245 |*
246 |*    Beschreibung      STREAM.SDW
247 |*    Ersterstellung    OV 27.09.94
248 |*    Letzte Aenderung  OV 27.09.94
249 |*
250 *************************************************************************/
251 
SetSize(sal_uIntPtr nSize)252 void SvCacheStream::SetSize( sal_uIntPtr nSize )
253 {
254     pCurrentStream->SetStreamSize( nSize );
255 }
256 
257 /*************************************************************************
258 |*
259 |*    SvCacheStream::GetSize()
260 |*
261 |*    Beschreibung      STREAM.SDW
262 |*    Ersterstellung    OV 27.09.94
263 |*    Letzte Aenderung  OV 27.09.94
264 |*
265 *************************************************************************/
266 
GetSize()267 sal_uIntPtr SvCacheStream::GetSize()
268 {
269     // ACHTUNG: SvMemoryStream::GetSize() gibt Groesse
270     // des allozierten Buffers zurueck
271     Flush();
272     sal_uIntPtr nTemp = Tell();
273     sal_uIntPtr nLength = Seek( STREAM_SEEK_TO_END );
274     Seek( nTemp );
275     return nLength;
276 }
277 
SetFilenameHdl(const Link & rLink)278 void    SvCacheStream::SetFilenameHdl( const Link& rLink)
279 {
280 	aFilenameLinkHdl = rLink;
281 }
282 
GetFilenameHdl() const283 const Link& SvCacheStream::GetFilenameHdl() const
284 {
285 	return aFilenameLinkHdl;
286 }
287