Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include <tools/stream.hxx>
21 : : #include <tools/cachestr.hxx>
22 : : #include <tools/tempfile.hxx>
23 : :
24 [ + - ]: 10495 : SvCacheStream::SvCacheStream( sal_uIntPtr nMaxMemSize )
25 : : {
26 [ - + ]: 10495 : if( !nMaxMemSize )
27 : 0 : nMaxMemSize = 20480;
28 : 10495 : SvStream::bIsWritable = sal_True;
29 : 10495 : nMaxSize = nMaxMemSize;
30 : 10495 : bPersistent = sal_False;
31 : 10495 : pSwapStream = 0;
32 [ + - ][ + - ]: 10495 : pCurrentStream = new SvMemoryStream( nMaxMemSize );
33 : 10495 : pTempFile = 0;
34 : 10495 : }
35 : :
36 [ + - ]: 10495 : SvCacheStream::~SvCacheStream()
37 : : {
38 [ + - ]: 10495 : if( pCurrentStream != pSwapStream )
39 [ - + ][ # # ]: 10495 : delete pSwapStream;
40 [ + - ][ + - ]: 10495 : delete pCurrentStream;
41 : :
42 [ - + ][ # # ]: 10495 : if( pSwapStream && !bPersistent && pTempFile )
[ # # ]
43 : : {
44 : : // temporaeres File loeschen
45 : 0 : pTempFile->EnableKillingFile( sal_True );
46 : : }
47 : :
48 [ - + ][ # # ]: 10495 : delete pTempFile;
49 [ - + ]: 10495 : }
50 : :
51 : 0 : void SvCacheStream::SwapOut()
52 : : {
53 [ # # ]: 0 : if( pCurrentStream != pSwapStream )
54 : : {
55 [ # # ][ # # ]: 0 : if( !pSwapStream && !aFileName.Len() )
[ # # ]
56 : : {
57 [ # # ]: 0 : pTempFile = new TempFile;
58 [ # # ]: 0 : aFileName = pTempFile->GetName();
59 : : }
60 : :
61 : 0 : sal_uIntPtr nPos = pCurrentStream->Tell();
62 : 0 : pCurrentStream->Seek( 0 );
63 [ # # ]: 0 : if( !pSwapStream )
64 [ # # ]: 0 : pSwapStream = new SvFileStream( aFileName, STREAM_READWRITE | STREAM_TRUNC );
65 : 0 : *pSwapStream << *pCurrentStream;
66 : 0 : pSwapStream->Flush();
67 [ # # ]: 0 : delete pCurrentStream;
68 : 0 : pCurrentStream = pSwapStream;
69 : 0 : pCurrentStream->Seek( nPos );
70 : : }
71 : 0 : }
72 : :
73 : 10493 : sal_uIntPtr SvCacheStream::GetData( void* pData, sal_uIntPtr nSize )
74 : : {
75 : 10493 : return pCurrentStream->Read( pData, nSize );
76 : : }
77 : :
78 : 21230 : sal_uIntPtr SvCacheStream::PutData( const void* pData, sal_uIntPtr nSize )
79 : : {
80 : : // prefer swapping data instead copying it again
81 [ + - - + ]: 42460 : if( pCurrentStream != pSwapStream
[ - + ]
82 : 21230 : && pCurrentStream->Tell() + nSize > nMaxSize )
83 : 0 : SwapOut();
84 : 21230 : return pCurrentStream->Write( pData, nSize );
85 : : }
86 : :
87 : 31483 : sal_uIntPtr SvCacheStream::SeekPos( sal_uIntPtr nPos )
88 : : {
89 : 31483 : return pCurrentStream->Seek( nPos );
90 : : }
91 : :
92 : 10497 : void SvCacheStream::FlushData()
93 : : {
94 : 10497 : pCurrentStream->Flush();
95 [ - + ]: 20994 : if( pCurrentStream != pSwapStream
[ + - - + ]
96 : 10497 : && ((SvMemoryStream*)pCurrentStream)->GetSize() > nMaxSize )
97 : 0 : SwapOut();
98 : 10497 : }
99 : :
100 : 2 : const void* SvCacheStream::GetBuffer()
101 : : {
102 : 2 : Flush();
103 [ + - ]: 2 : if( pCurrentStream != pSwapStream )
104 : 2 : return ((SvMemoryStream*)pCurrentStream)->GetData();
105 : : else
106 : 2 : return 0;
107 : : }
108 : :
109 : 0 : void SvCacheStream::SetSize( sal_uIntPtr nSize )
110 : : {
111 : 0 : pCurrentStream->SetStreamSize( nSize );
112 : 0 : }
113 : :
114 : 10495 : sal_uIntPtr SvCacheStream::GetSize()
115 : : {
116 : : // CAUTION: SvMemoryStream::GetSize() returns size of the allocated buffer
117 : 10495 : Flush();
118 : 10495 : sal_uIntPtr nTemp = Tell();
119 : 10495 : sal_uIntPtr nLength = Seek( STREAM_SEEK_TO_END );
120 : 10495 : Seek( nTemp );
121 : 10495 : return nLength;
122 : : }
123 : :
124 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|