1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_tools.hxx" 30 31 #include <tools/debug.hxx> 32 #include <tools/stream.hxx> 33 #include <tools/cachestr.hxx> 34 #include <tools/tempfile.hxx> 35 36 /************************************************************************* 37 |* 38 |* SvCacheStream::SvCacheStream() 39 |* 40 |* Beschreibung STREAM.SDW 41 |* Ersterstellung OV 27.09.94 42 |* Letzte Aenderung OV 27.09.94 43 |* 44 *************************************************************************/ 45 46 SvCacheStream::SvCacheStream( sal_uIntPtr nMaxMemSize ) 47 { 48 if( !nMaxMemSize ) 49 nMaxMemSize = 20480; 50 SvStream::bIsWritable = sal_True; 51 nMaxSize = nMaxMemSize; 52 bPersistent = sal_False; 53 pSwapStream = 0; 54 pCurrentStream = new SvMemoryStream( nMaxMemSize ); 55 pTempFile = 0; 56 } 57 58 /************************************************************************* 59 |* 60 |* SvCacheStream::SvCacheStream() 61 |* 62 |* Beschreibung STREAM.SDW 63 |* Ersterstellung OV 27.09.94 64 |* Letzte Aenderung OV 27.09.94 65 |* 66 *************************************************************************/ 67 68 SvCacheStream::SvCacheStream( const String &rFileName, 69 sal_uIntPtr nExpectedSize, 70 sal_uIntPtr nMaxMemSize ) 71 { 72 if( !nMaxMemSize ) 73 nMaxMemSize = 20480; 74 75 if( nExpectedSize > nMaxMemSize ) 76 nExpectedSize = nMaxMemSize; // oder gleich in File schreiben 77 else if( !nExpectedSize ) 78 nExpectedSize = 4096; 79 80 SvStream::bIsWritable = sal_True; 81 nMaxSize = nMaxMemSize; 82 bPersistent = sal_True; 83 aFileName = rFileName; 84 pSwapStream = 0; 85 pCurrentStream = new SvMemoryStream( nExpectedSize ); 86 pTempFile = 0; 87 } 88 89 /************************************************************************* 90 |* 91 |* SvCacheStream::~SvCacheStream() 92 |* 93 |* Beschreibung STREAM.SDW 94 |* Ersterstellung OV 27.09.94 95 |* Letzte Aenderung OV 27.09.94 96 |* 97 *************************************************************************/ 98 99 SvCacheStream::~SvCacheStream() 100 { 101 if( pCurrentStream != pSwapStream ) 102 delete pSwapStream; 103 delete pCurrentStream; 104 105 if( pSwapStream && !bPersistent && pTempFile ) 106 { 107 // temporaeres File loeschen 108 pTempFile->EnableKillingFile( sal_True ); 109 } 110 111 delete pTempFile; 112 } 113 114 /************************************************************************* 115 |* 116 |* SvCacheStream::SwapOut() 117 |* 118 |* Beschreibung STREAM.SDW 119 |* Ersterstellung OV 27.09.94 120 |* Letzte Aenderung OV 27.09.94 121 |* 122 *************************************************************************/ 123 124 void SvCacheStream::SwapOut() 125 { 126 if( pCurrentStream != pSwapStream ) 127 { 128 if( !pSwapStream && !aFileName.Len() ) 129 { 130 if (aFilenameLinkHdl.IsSet()) 131 { 132 // pSwapStream wird zum Schutz gegen Reentranz genutzt 133 pSwapStream = pCurrentStream; 134 Link aLink( aFilenameLinkHdl ); 135 aFilenameLinkHdl = Link(); 136 aLink.Call(this); 137 // pSwapStream nur zuruecksetzen, wenn nicht ueber 138 // SetSwapStream geaendert 139 if( pSwapStream == pCurrentStream ) pSwapStream = 0; 140 } 141 else 142 { 143 pTempFile = new TempFile; 144 aFileName = pTempFile->GetName(); 145 } 146 } 147 148 sal_uIntPtr nPos = pCurrentStream->Tell(); 149 pCurrentStream->Seek( 0 ); 150 if( !pSwapStream ) 151 pSwapStream = new SvFileStream( aFileName, STREAM_READWRITE | STREAM_TRUNC ); 152 *pSwapStream << *pCurrentStream; 153 pSwapStream->Flush(); 154 delete pCurrentStream; 155 pCurrentStream = pSwapStream; 156 pCurrentStream->Seek( nPos ); 157 } 158 } 159 160 /************************************************************************* 161 |* 162 |* SvCacheStream::GetData() 163 |* 164 |* Beschreibung STREAM.SDW 165 |* Ersterstellung OV 27.09.94 166 |* Letzte Aenderung OV 27.09.94 167 |* 168 *************************************************************************/ 169 170 sal_uIntPtr SvCacheStream::GetData( void* pData, sal_uIntPtr nSize ) 171 { 172 return pCurrentStream->Read( pData, nSize ); 173 } 174 175 /************************************************************************* 176 |* 177 |* SvCacheStream::PutData() 178 |* 179 |* Beschreibung STREAM.SDW 180 |* Ersterstellung OV 27.09.94 181 |* Letzte Aenderung OV 27.09.94 182 |* 183 *************************************************************************/ 184 185 sal_uIntPtr SvCacheStream::PutData( const void* pData, sal_uIntPtr nSize ) 186 { 187 // lieber unnoetig auslagern als unnoetig umkopieren 188 if( pCurrentStream != pSwapStream 189 && pCurrentStream->Tell() + nSize > nMaxSize ) 190 SwapOut(); 191 return pCurrentStream->Write( pData, nSize ); 192 } 193 194 /************************************************************************* 195 |* 196 |* SvCacheStream::SeekPos() 197 |* 198 |* Beschreibung STREAM.SDW 199 |* Ersterstellung OV 27.09.94 200 |* Letzte Aenderung OV 27.09.94 201 |* 202 *************************************************************************/ 203 204 sal_uIntPtr SvCacheStream::SeekPos( sal_uIntPtr nPos ) 205 { 206 return pCurrentStream->Seek( nPos ); 207 } 208 209 /************************************************************************* 210 |* 211 |* SvCacheStream::FlushData() 212 |* 213 |* Beschreibung STREAM.SDW 214 |* Ersterstellung OV 27.09.94 215 |* Letzte Aenderung OV 27.09.94 216 |* 217 *************************************************************************/ 218 219 void SvCacheStream::FlushData() 220 { 221 pCurrentStream->Flush(); 222 if( pCurrentStream != pSwapStream 223 && ((SvMemoryStream*)pCurrentStream)->GetSize() > nMaxSize ) 224 SwapOut(); 225 } 226 227 /************************************************************************* 228 |* 229 |* SvCacheStream::GetStr() 230 |* 231 |* Beschreibung STREAM.SDW 232 |* Ersterstellung OV 27.09.94 233 |* Letzte Aenderung OV 27.09.94 234 |* 235 *************************************************************************/ 236 237 const void* SvCacheStream::GetBuffer() 238 { 239 Flush(); 240 if( pCurrentStream != pSwapStream ) 241 return ((SvMemoryStream*)pCurrentStream)->GetData(); 242 else 243 return 0; 244 } 245 246 /************************************************************************* 247 |* 248 |* SvCacheStream::SetSize() 249 |* 250 |* Beschreibung STREAM.SDW 251 |* Ersterstellung OV 27.09.94 252 |* Letzte Aenderung OV 27.09.94 253 |* 254 *************************************************************************/ 255 256 void SvCacheStream::SetSize( sal_uIntPtr nSize ) 257 { 258 pCurrentStream->SetStreamSize( nSize ); 259 } 260 261 /************************************************************************* 262 |* 263 |* SvCacheStream::GetSize() 264 |* 265 |* Beschreibung STREAM.SDW 266 |* Ersterstellung OV 27.09.94 267 |* Letzte Aenderung OV 27.09.94 268 |* 269 *************************************************************************/ 270 271 sal_uIntPtr SvCacheStream::GetSize() 272 { 273 // ACHTUNG: SvMemoryStream::GetSize() gibt Groesse 274 // des allozierten Buffers zurueck 275 Flush(); 276 sal_uIntPtr nTemp = Tell(); 277 sal_uIntPtr nLength = Seek( STREAM_SEEK_TO_END ); 278 Seek( nTemp ); 279 return nLength; 280 } 281 282 void SvCacheStream::SetFilenameHdl( const Link& rLink) 283 { 284 aFilenameLinkHdl = rLink; 285 } 286 287 const Link& SvCacheStream::GetFilenameHdl() const 288 { 289 return aFilenameLinkHdl; 290 } 291