1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/stream.hxx>
21 #include <tools/cachestr.hxx>
22 #include <tools/tempfile.hxx>
24 SvCacheStream::SvCacheStream( sal_uIntPtr nMaxMemSize
)
28 SvStream::bIsWritable
= sal_True
;
29 nMaxSize
= nMaxMemSize
;
30 bPersistent
= sal_False
;
32 pCurrentStream
= new SvMemoryStream( nMaxMemSize
);
36 SvCacheStream::~SvCacheStream()
38 if( pCurrentStream
!= pSwapStream
)
40 delete pCurrentStream
;
42 if( pSwapStream
&& !bPersistent
&& pTempFile
)
44 // temporaeres File loeschen
45 pTempFile
->EnableKillingFile( sal_True
);
51 void SvCacheStream::SwapOut()
53 if( pCurrentStream
!= pSwapStream
)
55 if( !pSwapStream
&& !aFileName
.Len() )
57 pTempFile
= new TempFile
;
58 aFileName
= pTempFile
->GetName();
61 sal_uIntPtr nPos
= pCurrentStream
->Tell();
62 pCurrentStream
->Seek( 0 );
64 pSwapStream
= new SvFileStream( aFileName
, STREAM_READWRITE
| STREAM_TRUNC
);
65 *pSwapStream
<< *pCurrentStream
;
67 delete pCurrentStream
;
68 pCurrentStream
= pSwapStream
;
69 pCurrentStream
->Seek( nPos
);
73 sal_uIntPtr
SvCacheStream::GetData( void* pData
, sal_uIntPtr nSize
)
75 return pCurrentStream
->Read( pData
, nSize
);
78 sal_uIntPtr
SvCacheStream::PutData( const void* pData
, sal_uIntPtr nSize
)
80 // prefer swapping data instead copying it again
81 if( pCurrentStream
!= pSwapStream
82 && pCurrentStream
->Tell() + nSize
> nMaxSize
)
84 return pCurrentStream
->Write( pData
, nSize
);
87 sal_uIntPtr
SvCacheStream::SeekPos( sal_uIntPtr nPos
)
89 return pCurrentStream
->Seek( nPos
);
92 void SvCacheStream::FlushData()
94 pCurrentStream
->Flush();
95 if( pCurrentStream
!= pSwapStream
96 && ((SvMemoryStream
*)pCurrentStream
)->GetSize() > nMaxSize
)
100 const void* SvCacheStream::GetBuffer()
103 if( pCurrentStream
!= pSwapStream
)
104 return ((SvMemoryStream
*)pCurrentStream
)->GetData();
109 void SvCacheStream::SetSize( sal_uIntPtr nSize
)
111 pCurrentStream
->SetStreamSize( nSize
);
114 sal_uIntPtr
SvCacheStream::GetSize()
116 // CAUTION: SvMemoryStream::GetSize() returns size of the allocated buffer
118 sal_uIntPtr nTemp
= Tell();
119 sal_uIntPtr nLength
= Seek( STREAM_SEEK_TO_END
);
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */