Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / qadevOOo / runner / helper / StreamSimulator.java
blob8e17e910b5bbd1fe0df9fa471a69ae714aa681e8
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package helper;
21 import com.sun.star.uno.UnoRuntime;
24 import com.sun.star.ucb.XSimpleFileAccess;
26 /**
27 * It simulates an input and output stream and
28 * implements the interfaces XInputStream, XOutputStream.
29 * So it can be used for testing loading/saving of documents
30 * using streams instead of URLs.
33 public class StreamSimulator implements com.sun.star.io.XInputStream ,
34 com.sun.star.io.XOutputStream ,
35 com.sun.star.io.XSeekable
38 /**
39 * @member m_sFileName name of the corresponding file on disk
40 * @member m_xInStream the internal input stream for reading
41 * @member m_xOutStream the internal input stream for writing
42 * @member m_xSeek points at runtime to m_xInStream or m_xOutStream and make it seekable
44 * @member //m_aProtocol the external set protocol object for logging messages
45 * @member m_bInWasUsed indicates, that the input stream interface was used
46 * @member m_bOutWasUsed indicates, that the output stream interface was used
49 private com.sun.star.io.XInputStream m_xInStream ;
50 private com.sun.star.io.XOutputStream m_xOutStream ;
51 private com.sun.star.io.XSeekable m_xSeek ;
57 /**
58 * construct a new instance of this class
59 * It set the name of the corresponding file on disk, which
60 * should be source or target for the following operations on
61 * this object. And it regulate if it should function as
62 * input or output stream.
64 * @param sFileName
65 * name of the file on disk
66 * Will be used as source (if param bInput==true)
67 * or as target (if param bInput==false).
69 * @param bInput
70 * it specify, which interface should work at this object.
71 * <TRUE/> => we simulate an input stream
72 * <FALSE/> => we simulate an output stream
74 * @throw com.sun.star.io.NotConnectedException
75 * in case the internal streams to the file on disk couldn't established.
76 * They are necessary. Otherwise this simulator can't really work.
78 public StreamSimulator( String sFileName , boolean bInput ,
79 lib.TestParameters param ) throws com.sun.star.io.NotConnectedException
81 try
83 XSimpleFileAccess xHelper = UnoRuntime.queryInterface(XSimpleFileAccess.class,
84 param.getMSF().createInstance("com.sun.star.ucb.SimpleFileAccess"));
86 if (xHelper == null)
87 throw new com.sun.star.io.NotConnectedException("ucb helper not available. Can't create streams.");
89 if (bInput)
91 m_xInStream = xHelper.openFileRead(sFileName);
92 m_xSeek = UnoRuntime.queryInterface(
93 com.sun.star.io.XSeekable.class,
94 m_xInStream);
96 else
98 m_xOutStream = xHelper.openFileWrite(sFileName);
99 m_xSeek = UnoRuntime.queryInterface(
100 com.sun.star.io.XSeekable.class,
101 m_xOutStream);
104 catch(com.sun.star.uno.Exception exUno)
106 throw new com.sun.star.io.NotConnectedException(exUno);
111 * following methods simulates the XInputStream.
112 * The notice all actions inside the internal protocol
113 * and try to map all necessary functions to the internal
114 * open in-stream.
116 public int readBytes( /*OUT*/ byte[][] lData ,
117 /*IN*/ int nBytesToRead ) throws com.sun.star.io.NotConnectedException ,
118 com.sun.star.io.BufferSizeExceededException,
119 com.sun.star.io.IOException
121 if (m_xInStream == null)
123 throw new com.sun.star.io.NotConnectedException("stream not open");
126 int nRead = 0;
129 nRead = m_xInStream.readBytes(lData,nBytesToRead);
131 catch (com.sun.star.io.NotConnectedException exConnect) {
133 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) {
135 catch (com.sun.star.io.IOException exIO ) {
137 catch (com.sun.star.uno.RuntimeException exRuntime) {
140 return nRead;
145 public int readSomeBytes( /*OUT*/ byte[][] lData ,
146 /*IN*/ int nMaxBytesToRead ) throws com.sun.star.io.NotConnectedException ,
147 com.sun.star.io.BufferSizeExceededException ,
148 com.sun.star.io.IOException
150 if (m_xInStream == null)
152 throw new com.sun.star.io.NotConnectedException("stream not open");
155 int nRead = 0;
158 nRead = m_xInStream.readSomeBytes(lData,nMaxBytesToRead);
160 catch (com.sun.star.io.NotConnectedException exConnect) {
162 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) {
164 catch (com.sun.star.io.IOException exIO ) {
166 catch (com.sun.star.uno.RuntimeException exRuntime) {
169 return nRead;
174 public void skipBytes( /*IN*/ int nBytesToSkip ) throws com.sun.star.io.NotConnectedException ,
175 com.sun.star.io.BufferSizeExceededException ,
176 com.sun.star.io.IOException
178 if (m_xInStream == null)
180 throw new com.sun.star.io.NotConnectedException("stream not open");
185 m_xInStream.skipBytes(nBytesToSkip);
187 catch (com.sun.star.io.NotConnectedException exConnect) {
189 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) {
191 catch (com.sun.star.io.IOException exIO ) {
193 catch (com.sun.star.uno.RuntimeException exRuntime) {
199 public int available() throws com.sun.star.io.NotConnectedException,
200 com.sun.star.io.IOException
202 if (m_xInStream == null)
204 throw new com.sun.star.io.NotConnectedException("stream not open");
207 int nAvailable = 0;
210 nAvailable = m_xInStream.available();
212 catch (com.sun.star.io.NotConnectedException exConnect) {
214 catch (com.sun.star.io.IOException exIO ) {
216 catch (com.sun.star.uno.RuntimeException exRuntime) {
219 return nAvailable;
224 public void closeInput() throws com.sun.star.io.NotConnectedException,
225 com.sun.star.io.IOException
227 if (m_xInStream == null)
229 throw new com.sun.star.io.NotConnectedException("stream not open");
234 m_xInStream.closeInput();
236 catch (com.sun.star.io.NotConnectedException exConnect) {
238 catch (com.sun.star.io.IOException exIO ) {
240 catch (com.sun.star.uno.RuntimeException exRuntime) {
246 * following methods simulates the XOutputStream.
247 * The notice all actions inside the internal protocol
248 * and try to map all necessary functions to the internal
249 * open out-stream.
251 public void writeBytes( /*IN*/byte[] lData ) throws com.sun.star.io.NotConnectedException ,
252 com.sun.star.io.BufferSizeExceededException ,
253 com.sun.star.io.IOException
255 if (m_xOutStream == null)
257 throw new com.sun.star.io.NotConnectedException("stream not open");
262 m_xOutStream.writeBytes(lData);
264 catch (com.sun.star.io.NotConnectedException exConnect) {
266 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) {
268 catch (com.sun.star.io.IOException exIO ) {
270 catch (com.sun.star.uno.RuntimeException exRuntime) {
276 public void flush() throws com.sun.star.io.NotConnectedException ,
277 com.sun.star.io.BufferSizeExceededException ,
278 com.sun.star.io.IOException
280 if (m_xOutStream == null)
282 throw new com.sun.star.io.NotConnectedException("stream not open");
287 m_xOutStream.flush();
289 catch (com.sun.star.io.NotConnectedException exConnect) {
291 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) {
293 catch (com.sun.star.io.IOException exIO ) {
295 catch (com.sun.star.uno.RuntimeException exRuntime) {
301 public void closeOutput() throws com.sun.star.io.NotConnectedException ,
302 com.sun.star.io.BufferSizeExceededException,
303 com.sun.star.io.IOException
305 if (m_xOutStream == null)
307 throw new com.sun.star.io.NotConnectedException("stream not open");
312 m_xOutStream.closeOutput();
314 catch (com.sun.star.io.NotConnectedException exConnect) {
316 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) {
318 catch (com.sun.star.io.IOException exIO ) {
320 catch (com.sun.star.uno.RuntimeException exRuntime) {
326 * following methods simulates the XSeekable.
327 * The notice all actions inside the internal protocol
328 * and try to map all necessary functions to the internal
329 * open stream.
331 public void seek( /*IN*/long nLocation ) throws com.sun.star.lang.IllegalArgumentException,
332 com.sun.star.io.IOException
334 if (m_xSeek == null)
336 throw new com.sun.star.io.IOException("stream not seekable");
341 m_xSeek.seek(nLocation);
343 catch (com.sun.star.lang.IllegalArgumentException exArg ) {
345 catch (com.sun.star.io.IOException exIO ) {
347 catch (com.sun.star.uno.RuntimeException exRuntime) {
353 public long getPosition() throws com.sun.star.io.IOException
355 if (m_xSeek == null)
357 throw new com.sun.star.io.IOException("stream not seekable");
360 long nPos = 0;
363 nPos = m_xSeek.getPosition();
365 catch (com.sun.star.io.IOException exIO ) {
367 catch (com.sun.star.uno.RuntimeException exRuntime) {
370 return nPos;
375 public long getLength() throws com.sun.star.io.IOException
377 if (m_xSeek == null)
379 throw new com.sun.star.io.IOException("stream not seekable");
382 long nLen = 0;
385 nLen = m_xSeek.getLength();
387 catch (com.sun.star.io.IOException exIO ) {
389 catch (com.sun.star.uno.RuntimeException exRuntime) {
392 return nLen;