update dev300-m58
[ooovba.git] / configmgr / source / misc / bufferedfile.cxx
blobe81120667ba2631a790edd419021afe6b907402f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: bufferedfile.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include <string.h>
35 #include "bufferedfile.hxx"
37 #include "filehelper.hxx"
39 #include <algorithm>
42 namespace configmgr
45 BufferedOutputFile::BufferedOutputFile( rtl::OUString const& aFileURL, sal_uInt32 nBufferSizeHint )
46 : m_pFile(new osl::File(aFileURL))
47 , m_buffer()
49 m_buffer.reserve( nBufferSizeHint ? nBufferSizeHint : 512 );
52 BufferedOutputFile::~BufferedOutputFile ()
54 delete m_pFile;
58 BufferedOutputFile::RC BufferedOutputFile::open( sal_uInt32 uFlags )
60 if (!m_pFile) return E_BADF;
62 return m_pFile->open(uFlags);
65 BufferedOutputFile::RC BufferedOutputFile::close()
67 if (!m_pFile) return E_BADF;
69 RC rc = this->sync();
70 RC rc2 = m_pFile->close();
72 delete m_pFile, m_pFile = 0;
73 if (rc == E_None)
74 rc = rc2;
75 return rc;
79 //BufferedOutputFile::RC BufferedOutputFile::getPos( sal_uInt64& uPos )
81 BufferedOutputFile::RC BufferedOutputFile::write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
83 if (!m_pFile) return E_BADF;
85 if (uBytesToWrite > m_buffer.max_size()-m_buffer.size())
87 // write big chunks natively
88 RC rc = this->sync();
89 if (rc == E_None)
91 OSL_ASSERT(m_buffer.empty());
92 rc = m_pFile->write(pBuffer, uBytesToWrite, rBytesWritten);
94 return rc;
97 // FIXME: handle out-out-memory here
98 const sal_uInt8 * data = static_cast<const sal_uInt8 *>(pBuffer);
99 m_buffer.insert(m_buffer.end(), data, data + uBytesToWrite);
100 rBytesWritten = uBytesToWrite;
102 return E_None;
105 BufferedOutputFile::RC BufferedOutputFile::sync()
107 if (!m_pFile) return E_BADF;
109 sal_uInt64 size = m_buffer.size();
110 sal_uInt64 written = 0;
112 RC rc = m_pFile->write(&m_buffer.front(),size,written);
114 if (rc != E_None)
115 return rc;
117 // we don't support special files where multiple write passes are needed
118 if (written < size)
120 // but we try our best to stay consistent
121 m_buffer.erase(m_buffer.begin(),
122 m_buffer.end() + sal::static_int_cast<sal_uInt32>( written ));
124 return E_IO;
127 m_buffer.clear();
129 return E_None;
132 } // namespace configmgr