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