summaryrefslogtreecommitdiff
path: root/comphelper/source/streaming
diff options
context:
space:
mode:
authorJens-Heiner Rechtien <hr@openoffice.org>2004-05-10 16:34:12 +0000
committerJens-Heiner Rechtien <hr@openoffice.org>2004-05-10 16:34:12 +0000
commite828eff6a2c206eca4e83a85ef7d6778b1a05a74 (patch)
tree085d255c9052c584fba5a88eee28fd9ea6e5d80c /comphelper/source/streaming
parent5948a5f58a015f0e0c7e19abe4a9952c6db1e1c2 (diff)
INTEGRATION: CWS fwkbugfix02 (1.1.2); FILE ADDED
2004/03/24 07:50:05 mav 1.1.2.2: #115974# add static checking method 2004/03/23 15:40:18 mav 1.1.2.1: #115974# seekable wrapper
Diffstat (limited to 'comphelper/source/streaming')
-rw-r--r--comphelper/source/streaming/seekableinput.cxx300
1 files changed, 300 insertions, 0 deletions
diff --git a/comphelper/source/streaming/seekableinput.cxx b/comphelper/source/streaming/seekableinput.cxx
new file mode 100644
index 000000000000..dd929c767015
--- /dev/null
+++ b/comphelper/source/streaming/seekableinput.cxx
@@ -0,0 +1,300 @@
+/*************************************************************************
+ *
+ * $RCSfile: seekableinput.cxx,v $
+ *
+ * $Revision: 1.2 $
+ *
+ * last change: $Author: hr $ $Date: 2004-05-10 17:34:12 $
+ *
+ * The Contents of this file are made available subject to the terms of
+ * either of the following licenses
+ *
+ * - GNU Lesser General Public License Version 2.1
+ * - Sun Industry Standards Source License Version 1.1
+ *
+ * Sun Microsystems Inc., October, 2000
+ *
+ * GNU Lesser General Public License Version 2.1
+ * =============================================
+ * Copyright 2000 by Sun Microsystems, Inc.
+ * 901 San Antonio Road, Palo Alto, CA 94303, USA
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ *
+ * Sun Industry Standards Source License Version 1.1
+ * =================================================
+ * The contents of this file are subject to the Sun Industry Standards
+ * Source License Version 1.1 ( the "License" ); You may not use this file
+ * except in compliance with the License. You may obtain a copy of the
+ * License at http://www.openoffice.org/license.html.
+ *
+ * Software provided under this License is provided on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
+ * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
+ * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
+ * See the License for the specific provisions governing your rights and
+ * obligations concerning the Software.
+ *
+ * The Initial Developer of the Original Code is: Sun Microsystems, Inc.
+ *
+ * Copyright: 2000 by Sun Microsystems, Inc.
+ *
+ * All Rights Reserved.
+ *
+ * Contributor( s ): _______________________________________
+ *
+ *
+ ************************************************************************/
+
+#ifndef _COM_SUN_STAR_IO_XOUTPUTSTREAM_HPP_
+#include <com/sun/star/io/XOutputStream.hpp>
+#endif
+
+
+#include <comphelper/seekableinput.hxx>
+
+using namespace ::com::sun::star;
+
+namespace comphelper
+{
+
+const sal_Int32 nConstBufferSize = 32000;
+
+//---------------------------------------------------------------------------
+void copyInputToOutput_Impl( const uno::Reference< io::XInputStream >& xIn,
+ const uno::Reference< io::XOutputStream >& xOut )
+{
+ sal_Int32 nRead;
+ uno::Sequence< sal_Int8 > aSequence( nConstBufferSize );
+
+ do
+ {
+ nRead = xIn->readBytes( aSequence, nConstBufferSize );
+ if ( nRead < nConstBufferSize )
+ {
+ uno::Sequence< sal_Int8 > aTempBuf( aSequence.getConstArray(), nRead );
+ xOut->writeBytes( aTempBuf );
+ }
+ else
+ xOut->writeBytes( aSequence );
+ }
+ while ( nRead == nConstBufferSize );
+}
+
+//---------------------------------------------------------------------------
+OSeekableInputWrapper::OSeekableInputWrapper(
+ const uno::Reference< io::XInputStream >& xInStream,
+ const uno::Reference< lang::XMultiServiceFactory >& xFactory )
+: m_xFactory( xFactory )
+, m_xOriginalStream( xInStream )
+{
+ if ( !m_xFactory.is() )
+ throw uno::RuntimeException();
+}
+
+//---------------------------------------------------------------------------
+OSeekableInputWrapper::~OSeekableInputWrapper()
+{
+}
+
+//---------------------------------------------------------------------------
+uno::Reference< io::XInputStream > OSeekableInputWrapper::CheckSeekableCanWrap(
+ const uno::Reference< io::XInputStream >& xInStream,
+ const uno::Reference< lang::XMultiServiceFactory >& xFactory )
+{
+ // check that the stream is seekable and just wrap it if it is not
+ uno::Reference< io::XSeekable > xSeek( xInStream, uno::UNO_QUERY );
+ if ( xSeek.is() )
+ return xInStream;
+
+ uno::Reference< io::XInputStream > xNewStream(
+ static_cast< io::XInputStream* >(
+ new OSeekableInputWrapper( xInStream, xFactory ) ) );
+}
+
+//---------------------------------------------------------------------------
+void OSeekableInputWrapper::PrepareCopy_Impl()
+{
+ if ( !m_xCopyInput.is() )
+ {
+ if ( !m_xFactory.is() )
+ throw uno::RuntimeException();
+
+ uno::Reference< io::XOutputStream > xTempOut(
+ m_xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
+ uno::UNO_QUERY );
+
+ if ( xTempOut.is() )
+ {
+ copyInputToOutput_Impl( m_xOriginalStream, xTempOut );
+ xTempOut->closeOutput();
+
+ uno::Reference< io::XSeekable > xTempSeek( xTempOut, uno::UNO_QUERY );
+ if ( xTempSeek.is() )
+ {
+ xTempSeek->seek( 0 );
+ m_xCopyInput = uno::Reference< io::XInputStream >( xTempOut, uno::UNO_QUERY );
+ if ( m_xCopyInput.is() )
+ m_xCopySeek = xTempSeek;
+ }
+ }
+ }
+
+ if ( !m_xCopyInput.is() )
+ throw io::IOException();
+}
+
+// XInputStream
+//---------------------------------------------------------------------------
+sal_Int32 SAL_CALL OSeekableInputWrapper::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
+ throw ( io::NotConnectedException,
+ io::BufferSizeExceededException,
+ io::IOException,
+ uno::RuntimeException )
+{
+ ::osl::MutexGuard aGuard( m_aMutex );
+
+ if ( !m_xOriginalStream.is() )
+ throw io::NotConnectedException();
+
+ PrepareCopy_Impl();
+
+ return m_xCopyInput->readBytes( aData, nBytesToRead );
+}
+
+//---------------------------------------------------------------------------
+sal_Int32 SAL_CALL OSeekableInputWrapper::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
+ throw ( io::NotConnectedException,
+ io::BufferSizeExceededException,
+ io::IOException,
+ uno::RuntimeException )
+{
+ ::osl::MutexGuard aGuard( m_aMutex );
+
+ if ( !m_xOriginalStream.is() )
+ throw io::NotConnectedException();
+
+ PrepareCopy_Impl();
+
+ return m_xCopyInput->readSomeBytes( aData, nMaxBytesToRead );
+}
+
+//---------------------------------------------------------------------------
+void SAL_CALL OSeekableInputWrapper::skipBytes( sal_Int32 nBytesToSkip )
+ throw ( io::NotConnectedException,
+ io::BufferSizeExceededException,
+ io::IOException,
+ uno::RuntimeException )
+{
+ ::osl::MutexGuard aGuard( m_aMutex );
+
+ if ( !m_xOriginalStream.is() )
+ throw io::NotConnectedException();
+
+ PrepareCopy_Impl();
+
+ m_xCopyInput->skipBytes( nBytesToSkip );
+}
+
+//---------------------------------------------------------------------------
+sal_Int32 SAL_CALL OSeekableInputWrapper::available()
+ throw ( io::NotConnectedException,
+ io::IOException,
+ uno::RuntimeException )
+{
+ ::osl::MutexGuard aGuard( m_aMutex );
+
+ if ( !m_xOriginalStream.is() )
+ throw io::NotConnectedException();
+
+ PrepareCopy_Impl();
+
+ return m_xCopyInput->available();
+}
+
+//---------------------------------------------------------------------------
+void SAL_CALL OSeekableInputWrapper::closeInput()
+ throw ( io::NotConnectedException,
+ io::IOException,
+ uno::RuntimeException )
+{
+ ::osl::MutexGuard aGuard( m_aMutex );
+
+ if ( !m_xOriginalStream.is() )
+ throw io::NotConnectedException();
+
+ m_xOriginalStream->closeInput();
+ m_xOriginalStream = uno::Reference< io::XInputStream >();
+
+ if ( m_xCopyInput.is() )
+ {
+ m_xCopyInput->closeInput();
+ m_xCopyInput = uno::Reference< io::XInputStream >();
+ }
+
+ m_xCopySeek = uno::Reference< io::XSeekable >();
+}
+
+
+// XSeekable
+//---------------------------------------------------------------------------
+void SAL_CALL OSeekableInputWrapper::seek( sal_Int64 location )
+ throw ( lang::IllegalArgumentException,
+ io::IOException,
+ uno::RuntimeException )
+{
+ ::osl::MutexGuard aGuard( m_aMutex );
+
+ if ( !m_xOriginalStream.is() )
+ throw io::NotConnectedException();
+
+ PrepareCopy_Impl();
+
+ m_xCopySeek->seek( location );
+}
+
+//---------------------------------------------------------------------------
+sal_Int64 SAL_CALL OSeekableInputWrapper::getPosition()
+ throw ( io::IOException,
+ uno::RuntimeException )
+{
+ ::osl::MutexGuard aGuard( m_aMutex );
+
+ if ( !m_xOriginalStream.is() )
+ throw io::NotConnectedException();
+
+ PrepareCopy_Impl();
+
+ return m_xCopySeek->getPosition();
+}
+
+//---------------------------------------------------------------------------
+sal_Int64 SAL_CALL OSeekableInputWrapper::getLength()
+ throw ( io::IOException,
+ uno::RuntimeException )
+{
+ ::osl::MutexGuard aGuard( m_aMutex );
+
+ if ( !m_xOriginalStream.is() )
+ throw io::NotConnectedException();
+
+ PrepareCopy_Impl();
+
+ return m_xCopySeek->getLength();
+}
+
+} // namespace comphelper
+