bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / runner / helper / StreamSimulator.java
blobda5f422e722ea336871d05975e508b5edaa096e3
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.lang.XMultiServiceFactory;
25 import com.sun.star.ucb.XSimpleFileAccess;
27 /**
28 * It simulates an input and output stream and
29 * implements the interfaces XInputStream, XOutputStream.
30 * So it can be used for testing loading/saving of documents
31 * using streams instead of URLs.
34 public class StreamSimulator implements com.sun.star.io.XInputStream ,
35 com.sun.star.io.XOutputStream ,
36 com.sun.star.io.XSeekable
38 //_________________________________
39 /**
40 * @member m_sFileName name of the corrsponding file on disk
41 * @member m_xInStream the internal input stream for reading
42 * @member m_xOutStream the internal input stream for writing
43 * @member m_xSeek points at runtime to m_xInStream or m_xOutStream and make it seekable
45 * @member //m_aProtocol the external set protocol object for logging messages
46 * @member m_bInWasUsed indicates, that the input stream interface was used
47 * @member m_bOutWasUsed indicates, that the output stream interface was used
50 private String m_sFileName ;
51 private com.sun.star.io.XInputStream m_xInStream ;
52 private com.sun.star.io.XOutputStream m_xOutStream ;
53 private com.sun.star.io.XSeekable m_xSeek ;
55 //public ComplexTestEnvironment //m_aProtocol ;
56 public boolean m_bInWasUsed ;
57 public boolean m_bOutWasUsed ;
59 //_________________________________
60 /**
61 * construct a new instance of this class
62 * It set the name of the correspojnding file on disk, which
63 * should be source or target for the following operations on
64 * this object. And it regulate if it should function as
65 * input or output stream.
67 * @param sFileName
68 * name of the file on disk
69 * Will be used as source (if param bInput==true)
70 * or as target (if param bInput==false).
72 * @param bInput
73 * it specify, which interface should work at this object.
74 * <TRUE/> => we simulate an input stream
75 * <FALSE/> => we simulate an output stream
77 * @throw com.sun.star.io.NotConnectedException
78 * in case the internal streams to the file on disk couldn't established.
79 * They are neccessary. Otherwhise this simulator can't realy work.
81 public StreamSimulator( String sFileName , boolean bInput ,
82 lib.TestParameters param ) throws com.sun.star.io.NotConnectedException
84 ////m_aProtocol = new ComplexTestEnvironment();
85 m_sFileName = sFileName ;
86 m_bInWasUsed = false ;
87 m_bOutWasUsed = false ;
89 try
91 XSimpleFileAccess xHelper = UnoRuntime.queryInterface(XSimpleFileAccess.class,
92 ((XMultiServiceFactory)param.getMSF()).createInstance("com.sun.star.ucb.SimpleFileAccess"));
93 /* com.sun.star.ucb.XSimpleFileAccess xHelper = (com.sun.star.ucb.XSimpleFileAccess)OfficeConnect.createRemoteInstance(
94 com.sun.star.ucb.XSimpleFileAccess.class,
95 "com.sun.star.ucb.SimpleFileAccess");*/
97 if (xHelper == null)
98 throw new com.sun.star.io.NotConnectedException("ucb helper not available. Can't create streams.");
100 if (bInput)
102 m_xInStream = xHelper.openFileRead(m_sFileName);
103 m_xSeek = UnoRuntime.queryInterface(
104 com.sun.star.io.XSeekable.class,
105 m_xInStream);
107 else
109 m_xOutStream = xHelper.openFileWrite(m_sFileName);
110 m_xSeek = UnoRuntime.queryInterface(
111 com.sun.star.io.XSeekable.class,
112 m_xOutStream);
115 catch(com.sun.star.uno.Exception exUno)
117 ////m_aProtocol.log("\tstream not open. throw NotConnectedException\n\n\tfailed\n}\n");
118 throw new com.sun.star.io.NotConnectedException("Could not open the file.");
122 /* public void finalize()
124 ////m_aProtocol.log("finalize was called. Please check if it was right or not.\n");
125 } */
127 //_________________________________
129 * following methods simulates the XInputStream.
130 * The notice all actions inside the internal protocol
131 * and try to map all neccessary functions to the internal
132 * open in-stream.
134 public int readBytes( /*OUT*/ byte[][] lData ,
135 /*IN*/ int nBytesToRead ) throws com.sun.star.io.NotConnectedException ,
136 com.sun.star.io.BufferSizeExceededException,
137 com.sun.star.io.IOException
139 //m_aProtocol.log("readBytes(lData["+lData.length+"]["+lData[0]+"],"+nBytesToRead+")\n{\n");
140 m_bInWasUsed = true;
142 if (m_xInStream == null)
144 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\n\tfailed\n}\n");
145 throw new com.sun.star.io.NotConnectedException("stream not open");
148 int nRead = 0;
151 nRead = m_xInStream.readBytes(lData,nBytesToRead);
153 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
155 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
157 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
159 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
162 //m_aProtocol.log("\treads "+nRead+" bytes\n\tOK\n}\n");
164 //if (nRead != nBytesToRead)
165 //m_aProtocol.log("there are some missing bytes for reading!\n");
167 return nRead;
170 //_________________________________
172 public int readSomeBytes( /*OUT*/ byte[][] lData ,
173 /*IN*/ int nMaxBytesToRead ) throws com.sun.star.io.NotConnectedException ,
174 com.sun.star.io.BufferSizeExceededException ,
175 com.sun.star.io.IOException
177 //m_aProtocol.log("readSomeBytes(lData["+lData.length+"]["+lData[0]+"],"+nMaxBytesToRead+")\n{\n");
178 m_bInWasUsed = true;
180 if (m_xInStream == null)
182 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
183 throw new com.sun.star.io.NotConnectedException("stream not open");
186 int nRead = 0;
189 nRead = m_xInStream.readSomeBytes(lData,nMaxBytesToRead);
191 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
193 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
195 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
197 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
200 //m_aProtocol.log("\treads "+nRead+" bytes\n\tOK\n}\n");
202 //if (nRead != nMaxBytesToRead)
203 //m_aProtocol.log("there are some missing bytes for reading!");
205 return nRead;
208 //_________________________________
210 public void skipBytes( /*IN*/ int nBytesToSkip ) throws com.sun.star.io.NotConnectedException ,
211 com.sun.star.io.BufferSizeExceededException ,
212 com.sun.star.io.IOException
214 //m_aProtocol.log("skipBytes("+nBytesToSkip+")\n{\n");
215 m_bInWasUsed = true;
217 if (m_xInStream == null)
219 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
220 throw new com.sun.star.io.NotConnectedException("stream not open");
225 m_xInStream.skipBytes(nBytesToSkip);
227 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
229 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
231 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
233 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
236 //m_aProtocol.log("\tOK\n}\n");
239 //_________________________________
241 public int available() throws com.sun.star.io.NotConnectedException,
242 com.sun.star.io.IOException
244 //m_aProtocol.log("available()\n{\n");
245 m_bInWasUsed = true;
247 if (m_xInStream == null)
249 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
250 throw new com.sun.star.io.NotConnectedException("stream not open");
253 int nAvailable = 0;
256 nAvailable = m_xInStream.available();
258 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n"); throw exConnect;
260 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
262 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
265 //m_aProtocol.log("\treturns "+nAvailable+" bytes\n\tOK\n}\n");
266 return nAvailable;
269 //_________________________________
271 public void closeInput() throws com.sun.star.io.NotConnectedException,
272 com.sun.star.io.IOException
274 //m_aProtocol.log("closeInput()\n{\n");
275 m_bInWasUsed = true;
277 if (m_xInStream == null)
279 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
280 throw new com.sun.star.io.NotConnectedException("stream not open");
285 m_xInStream.closeInput();
287 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n"); throw exConnect;
289 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
291 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
294 //m_aProtocol.log("\tOK\n}\n");
297 //_________________________________
299 * following methods simulates the XOutputStream.
300 * The notice all actions inside the internal protocol
301 * and try to map all neccessary functions to the internal
302 * open out-stream.
304 public void writeBytes( /*IN*/byte[] lData ) throws com.sun.star.io.NotConnectedException ,
305 com.sun.star.io.BufferSizeExceededException ,
306 com.sun.star.io.IOException
308 //m_aProtocol.log("writeBytes(lData["+lData.length+"])\n{\n");
309 m_bOutWasUsed = true;
311 if (m_xOutStream == null)
313 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
314 throw new com.sun.star.io.NotConnectedException("stream not open");
319 m_xOutStream.writeBytes(lData);
321 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
323 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
325 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
327 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
330 //m_aProtocol.log("\tOK\n}\n");
333 //_________________________________
335 public void flush() throws com.sun.star.io.NotConnectedException ,
336 com.sun.star.io.BufferSizeExceededException ,
337 com.sun.star.io.IOException
339 //m_aProtocol.log("flush()\n{\n");
340 m_bOutWasUsed = true;
342 if (m_xOutStream == null)
344 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
345 throw new com.sun.star.io.NotConnectedException("stream not open");
350 m_xOutStream.flush();
352 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
354 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
356 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
358 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
360 //m_aProtocol.log("\tOK\n}\n");
363 //_________________________________
365 public void closeOutput() throws com.sun.star.io.NotConnectedException ,
366 com.sun.star.io.BufferSizeExceededException,
367 com.sun.star.io.IOException
369 //m_aProtocol.log("closeOutput()\n{\n");
370 m_bOutWasUsed = true;
372 if (m_xOutStream == null)
374 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
375 throw new com.sun.star.io.NotConnectedException("stream not open");
380 m_xOutStream.closeOutput();
382 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
384 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
386 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
388 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
391 //m_aProtocol.log("\tOK\n}\n");
394 //_________________________________
396 * following methods simulates the XSeekable.
397 * The notice all actions inside the internal protocol
398 * and try to map all neccessary functions to the internal
399 * open stream.
401 public void seek( /*IN*/long nLocation ) throws com.sun.star.lang.IllegalArgumentException,
402 com.sun.star.io.IOException
404 //m_aProtocol.log("seek("+nLocation+")\n{\n");
406 if (m_xInStream != null)
407 m_bInWasUsed = true;
408 else
409 if (m_xOutStream != null)
410 m_bOutWasUsed = true;
411 else
412 //m_aProtocol.log("\tno stream open!\n");
414 if (m_xSeek == null)
416 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n");
417 throw new com.sun.star.io.IOException("stream not seekable");
422 m_xSeek.seek(nLocation);
424 catch (com.sun.star.lang.IllegalArgumentException exArg ) { //m_aProtocol.log("\tgot IllegalArgumentException\n\tfailed\n}\n" ); throw exArg;
426 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
428 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
431 //m_aProtocol.log("\tOK\n}\n");
434 //_________________________________
436 public long getPosition() throws com.sun.star.io.IOException
438 //m_aProtocol.log("getPosition()\n{\n");
440 if (m_xInStream != null)
441 m_bInWasUsed = true;
442 else
443 if (m_xOutStream != null)
444 m_bOutWasUsed = true;
445 else
446 //m_aProtocol.log("\tno stream open!\n");
448 if (m_xSeek == null)
450 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n");
451 throw new com.sun.star.io.IOException("stream not seekable");
454 long nPos = 0;
457 nPos = m_xSeek.getPosition();
459 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
461 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n"); throw exRuntime;
464 //m_aProtocol.log("\treturns pos="+nPos+"\n\tOK\n}\n");
465 return nPos;
468 //_________________________________
470 public long getLength() throws com.sun.star.io.IOException
472 //m_aProtocol.log("getLength()\n{\n");
474 if (m_xInStream != null)
475 m_bInWasUsed = true;
476 else
477 if (m_xOutStream != null)
478 m_bOutWasUsed = true;
479 else
480 //m_aProtocol.log("\tno stream open!\n");
482 if (m_xSeek == null)
484 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n");
485 throw new com.sun.star.io.IOException("stream not seekable");
488 long nLen = 0;
491 nLen = m_xSeek.getLength();
493 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
495 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n"); throw exRuntime;
498 //m_aProtocol.log("\treturns len="+nLen+"\n\tOK\n}\n");
499 return nLen;