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 20 : SvCacheStream::SvCacheStream( sal_uIntPtr nMaxMemSize )
25 : {
26 20 : if( !nMaxMemSize )
27 0 : nMaxMemSize = 20480;
28 20 : SvStream::bIsWritable = sal_True;
29 20 : nMaxSize = nMaxMemSize;
30 20 : bPersistent = sal_False;
31 20 : pSwapStream = 0;
32 20 : pCurrentStream = new SvMemoryStream( nMaxMemSize );
33 20 : pTempFile = 0;
34 20 : }
35 :
36 40 : SvCacheStream::~SvCacheStream()
37 : {
38 20 : if( pCurrentStream != pSwapStream )
39 20 : delete pSwapStream;
40 20 : delete pCurrentStream;
41 :
42 20 : if( pSwapStream && !bPersistent && pTempFile )
43 : {
44 : // temporaeres File loeschen
45 0 : pTempFile->EnableKillingFile( sal_True );
46 : }
47 :
48 20 : delete pTempFile;
49 20 : }
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 20 : sal_uIntPtr SvCacheStream::GetData( void* pData, sal_uIntPtr nSize )
74 : {
75 20 : return pCurrentStream->Read( pData, nSize );
76 : }
77 :
78 40 : sal_uIntPtr SvCacheStream::PutData( const void* pData, sal_uIntPtr nSize )
79 : {
80 : // prefer swapping data instead copying it again
81 80 : if( pCurrentStream != pSwapStream
82 40 : && pCurrentStream->Tell() + nSize > nMaxSize )
83 0 : SwapOut();
84 40 : return pCurrentStream->Write( pData, nSize );
85 : }
86 :
87 60 : sal_uIntPtr SvCacheStream::SeekPos( sal_uIntPtr nPos )
88 : {
89 60 : return pCurrentStream->Seek( nPos );
90 : }
91 :
92 20 : void SvCacheStream::FlushData()
93 : {
94 20 : pCurrentStream->Flush();
95 40 : if( pCurrentStream != pSwapStream
96 20 : && ((SvMemoryStream*)pCurrentStream)->GetSize() > nMaxSize )
97 0 : SwapOut();
98 20 : }
99 :
100 0 : const void* SvCacheStream::GetBuffer()
101 : {
102 0 : Flush();
103 0 : if( pCurrentStream != pSwapStream )
104 0 : return ((SvMemoryStream*)pCurrentStream)->GetData();
105 : else
106 0 : return 0;
107 : }
108 :
109 0 : void SvCacheStream::SetSize( sal_uIntPtr nSize )
110 : {
111 0 : pCurrentStream->SetStreamSize( nSize );
112 0 : }
113 :
114 20 : sal_uIntPtr SvCacheStream::GetSize()
115 : {
116 : // CAUTION: SvMemoryStream::GetSize() returns size of the allocated buffer
117 20 : Flush();
118 20 : sal_uIntPtr nTemp = Tell();
119 20 : sal_uIntPtr nLength = Seek( STREAM_SEEK_TO_END );
120 20 : Seek( nTemp );
121 20 : return nLength;
122 : }
123 :
124 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|