1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cachestr.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_tools.hxx"
34 #include <tools/debug.hxx>
35 #include <tools/stream.hxx>
36 #include <tools/cachestr.hxx>
37 #include <tools/tempfile.hxx>
39 /*************************************************************************
41 |* SvCacheStream::SvCacheStream()
43 |* Beschreibung STREAM.SDW
44 |* Ersterstellung OV 27.09.94
45 |* Letzte Aenderung OV 27.09.94
47 *************************************************************************/
49 SvCacheStream::SvCacheStream( ULONG nMaxMemSize
)
53 SvStream::bIsWritable
= TRUE
;
54 nMaxSize
= nMaxMemSize
;
57 pCurrentStream
= new SvMemoryStream( nMaxMemSize
);
61 /*************************************************************************
63 |* SvCacheStream::SvCacheStream()
65 |* Beschreibung STREAM.SDW
66 |* Ersterstellung OV 27.09.94
67 |* Letzte Aenderung OV 27.09.94
69 *************************************************************************/
71 SvCacheStream::SvCacheStream( const String
&rFileName
,
78 if( nExpectedSize
> nMaxMemSize
)
79 nExpectedSize
= nMaxMemSize
; // oder gleich in File schreiben
80 else if( !nExpectedSize
)
83 SvStream::bIsWritable
= TRUE
;
84 nMaxSize
= nMaxMemSize
;
86 aFileName
= rFileName
;
88 pCurrentStream
= new SvMemoryStream( nExpectedSize
);
92 /*************************************************************************
94 |* SvCacheStream::~SvCacheStream()
96 |* Beschreibung STREAM.SDW
97 |* Ersterstellung OV 27.09.94
98 |* Letzte Aenderung OV 27.09.94
100 *************************************************************************/
102 SvCacheStream::~SvCacheStream()
104 if( pCurrentStream
!= pSwapStream
)
106 delete pCurrentStream
;
108 if( pSwapStream
&& !bPersistent
&& pTempFile
)
110 // temporaeres File loeschen
111 pTempFile
->EnableKillingFile( TRUE
);
117 /*************************************************************************
119 |* SvCacheStream::SwapOut()
121 |* Beschreibung STREAM.SDW
122 |* Ersterstellung OV 27.09.94
123 |* Letzte Aenderung OV 27.09.94
125 *************************************************************************/
127 void SvCacheStream::SwapOut()
129 if( pCurrentStream
!= pSwapStream
)
131 if( !pSwapStream
&& !aFileName
.Len() )
133 if (aFilenameLinkHdl
.IsSet())
135 // pSwapStream wird zum Schutz gegen Reentranz genutzt
136 pSwapStream
= pCurrentStream
;
137 Link
aLink( aFilenameLinkHdl
);
138 aFilenameLinkHdl
= Link();
140 // pSwapStream nur zuruecksetzen, wenn nicht ueber
141 // SetSwapStream geaendert
142 if( pSwapStream
== pCurrentStream
) pSwapStream
= 0;
146 pTempFile
= new TempFile
;
147 aFileName
= pTempFile
->GetName();
151 ULONG nPos
= pCurrentStream
->Tell();
152 pCurrentStream
->Seek( 0 );
154 pSwapStream
= new SvFileStream( aFileName
, STREAM_READWRITE
| STREAM_TRUNC
);
155 *pSwapStream
<< *pCurrentStream
;
156 pSwapStream
->Flush();
157 delete pCurrentStream
;
158 pCurrentStream
= pSwapStream
;
159 pCurrentStream
->Seek( nPos
);
163 /*************************************************************************
165 |* SvCacheStream::GetData()
167 |* Beschreibung STREAM.SDW
168 |* Ersterstellung OV 27.09.94
169 |* Letzte Aenderung OV 27.09.94
171 *************************************************************************/
173 ULONG
SvCacheStream::GetData( void* pData
, ULONG nSize
)
175 return pCurrentStream
->Read( pData
, nSize
);
178 /*************************************************************************
180 |* SvCacheStream::PutData()
182 |* Beschreibung STREAM.SDW
183 |* Ersterstellung OV 27.09.94
184 |* Letzte Aenderung OV 27.09.94
186 *************************************************************************/
188 ULONG
SvCacheStream::PutData( const void* pData
, ULONG nSize
)
190 // lieber unnoetig auslagern als unnoetig umkopieren
191 if( pCurrentStream
!= pSwapStream
192 && pCurrentStream
->Tell() + nSize
> nMaxSize
)
194 return pCurrentStream
->Write( pData
, nSize
);
197 /*************************************************************************
199 |* SvCacheStream::SeekPos()
201 |* Beschreibung STREAM.SDW
202 |* Ersterstellung OV 27.09.94
203 |* Letzte Aenderung OV 27.09.94
205 *************************************************************************/
207 ULONG
SvCacheStream::SeekPos( ULONG nPos
)
209 return pCurrentStream
->Seek( nPos
);
212 /*************************************************************************
214 |* SvCacheStream::FlushData()
216 |* Beschreibung STREAM.SDW
217 |* Ersterstellung OV 27.09.94
218 |* Letzte Aenderung OV 27.09.94
220 *************************************************************************/
222 void SvCacheStream::FlushData()
224 pCurrentStream
->Flush();
225 if( pCurrentStream
!= pSwapStream
226 && ((SvMemoryStream
*)pCurrentStream
)->GetSize() > nMaxSize
)
230 /*************************************************************************
232 |* SvCacheStream::GetStr()
234 |* Beschreibung STREAM.SDW
235 |* Ersterstellung OV 27.09.94
236 |* Letzte Aenderung OV 27.09.94
238 *************************************************************************/
240 const void* SvCacheStream::GetBuffer()
243 if( pCurrentStream
!= pSwapStream
)
244 return ((SvMemoryStream
*)pCurrentStream
)->GetData();
249 /*************************************************************************
251 |* SvCacheStream::SetSize()
253 |* Beschreibung STREAM.SDW
254 |* Ersterstellung OV 27.09.94
255 |* Letzte Aenderung OV 27.09.94
257 *************************************************************************/
259 void SvCacheStream::SetSize( ULONG nSize
)
261 pCurrentStream
->SetStreamSize( nSize
);
264 /*************************************************************************
266 |* SvCacheStream::GetSize()
268 |* Beschreibung STREAM.SDW
269 |* Ersterstellung OV 27.09.94
270 |* Letzte Aenderung OV 27.09.94
272 *************************************************************************/
274 ULONG
SvCacheStream::GetSize()
276 // ACHTUNG: SvMemoryStream::GetSize() gibt Groesse
277 // des allozierten Buffers zurueck
279 ULONG nTemp
= Tell();
280 ULONG nLength
= Seek( STREAM_SEEK_TO_END
);
285 void SvCacheStream::SetFilenameHdl( const Link
& rLink
)
287 aFilenameLinkHdl
= rLink
;
290 const Link
& SvCacheStream::GetFilenameHdl() const
292 return aFilenameLinkHdl
;