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 <rtl/ustring.hxx>
21 : #include <tools/stream.hxx>
22 : #include <tools/cachestr.hxx>
23 : #include <tools/tempfile.hxx>
24 :
25 5666 : SvCacheStream::SvCacheStream( sal_uIntPtr nMaxMemSize )
26 : {
27 5666 : if( !nMaxMemSize )
28 0 : nMaxMemSize = 20480;
29 5666 : SvStream::bIsWritable = true;
30 5666 : nMaxSize = nMaxMemSize;
31 5666 : bPersistent = false;
32 5666 : pSwapStream = 0;
33 5666 : pCurrentStream = new SvMemoryStream( nMaxMemSize );
34 5666 : pTempFile = 0;
35 5666 : }
36 :
37 11332 : SvCacheStream::~SvCacheStream()
38 : {
39 5666 : if( pCurrentStream != pSwapStream )
40 5666 : delete pSwapStream;
41 5666 : delete pCurrentStream;
42 :
43 5666 : if( pSwapStream && !bPersistent && pTempFile )
44 : {
45 : // temporaeres File loeschen
46 0 : pTempFile->EnableKillingFile( true );
47 : }
48 :
49 5666 : delete pTempFile;
50 5666 : }
51 :
52 0 : void SvCacheStream::SwapOut()
53 : {
54 0 : if( pCurrentStream != pSwapStream )
55 : {
56 0 : if( !pSwapStream && aFileName.isEmpty() )
57 : {
58 0 : pTempFile = new TempFile;
59 0 : aFileName = pTempFile->GetName();
60 : }
61 :
62 0 : sal_uIntPtr nPos = pCurrentStream->Tell();
63 0 : pCurrentStream->Seek( 0 );
64 0 : if( !pSwapStream )
65 0 : pSwapStream = new SvFileStream( aFileName, STREAM_READWRITE | STREAM_TRUNC );
66 0 : *pSwapStream << *pCurrentStream;
67 0 : pSwapStream->Flush();
68 0 : delete pCurrentStream;
69 0 : pCurrentStream = pSwapStream;
70 0 : pCurrentStream->Seek( nPos );
71 : }
72 0 : }
73 :
74 5665 : sal_uIntPtr SvCacheStream::GetData( void* pData, sal_uIntPtr nSize )
75 : {
76 5665 : return pCurrentStream->Read( pData, nSize );
77 : }
78 :
79 11445 : sal_uIntPtr SvCacheStream::PutData( const void* pData, sal_uIntPtr nSize )
80 : {
81 : // prefer swapping data instead copying it again
82 22890 : if( pCurrentStream != pSwapStream
83 11445 : && pCurrentStream->Tell() + nSize > nMaxSize )
84 0 : SwapOut();
85 11445 : return pCurrentStream->Write( pData, nSize );
86 : }
87 :
88 16997 : sal_uIntPtr SvCacheStream::SeekPos( sal_uIntPtr nPos )
89 : {
90 16997 : return pCurrentStream->Seek( nPos );
91 : }
92 :
93 5667 : void SvCacheStream::FlushData()
94 : {
95 5667 : pCurrentStream->Flush();
96 11334 : if( pCurrentStream != pSwapStream
97 5667 : && ((SvMemoryStream*)pCurrentStream)->GetSize() > nMaxSize )
98 0 : SwapOut();
99 5667 : }
100 :
101 1 : const void* SvCacheStream::GetBuffer()
102 : {
103 1 : Flush();
104 1 : if( pCurrentStream != pSwapStream )
105 1 : return ((SvMemoryStream*)pCurrentStream)->GetData();
106 : else
107 0 : return 0;
108 : }
109 :
110 0 : void SvCacheStream::SetSize( sal_uIntPtr nSize )
111 : {
112 0 : pCurrentStream->SetStreamSize( nSize );
113 0 : }
114 :
115 5666 : sal_uIntPtr SvCacheStream::GetSize()
116 : {
117 : // CAUTION: SvMemoryStream::GetSize() returns size of the allocated buffer
118 5666 : Flush();
119 5666 : sal_uIntPtr nTemp = Tell();
120 5666 : sal_uIntPtr nLength = Seek( STREAM_SEEK_TO_END );
121 5666 : Seek( nTemp );
122 5666 : return nLength;
123 : }
124 :
125 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|