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 .
21 import com
.sun
.star
.uno
.UnoRuntime
;
24 import com
.sun
.star
.lang
.XMultiServiceFactory
;
25 import com
.sun
.star
.ucb
.XSimpleFileAccess
;
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 //_________________________________
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 //_________________________________
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.
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).
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 ;
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");*/
98 throw new com
.sun
.star
.io
.NotConnectedException("ucb helper not available. Can't create streams.");
102 m_xInStream
= xHelper
.openFileRead(m_sFileName
);
103 m_xSeek
= UnoRuntime
.queryInterface(
104 com
.sun
.star
.io
.XSeekable
.class,
109 m_xOutStream
= xHelper
.openFileWrite(m_sFileName
);
110 m_xSeek
= UnoRuntime
.queryInterface(
111 com
.sun
.star
.io
.XSeekable
.class,
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");
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
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");
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");
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");
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");
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");
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!");
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");
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");
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");
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");
269 //_________________________________
271 public void closeInput() throws com
.sun
.star
.io
.NotConnectedException
,
272 com
.sun
.star
.io
.IOException
274 //m_aProtocol.log("closeInput()\n{\n");
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
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
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)
409 if (m_xOutStream
!= null)
410 m_bOutWasUsed
= true;
412 //m_aProtocol.log("\tno stream open!\n");
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)
443 if (m_xOutStream
!= null)
444 m_bOutWasUsed
= true;
446 //m_aProtocol.log("\tno stream open!\n");
450 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n");
451 throw new com
.sun
.star
.io
.IOException("stream not seekable");
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");
468 //_________________________________
470 public long getLength() throws com
.sun
.star
.io
.IOException
472 //m_aProtocol.log("getLength()\n{\n");
474 if (m_xInStream
!= null)
477 if (m_xOutStream
!= null)
478 m_bOutWasUsed
= true;
480 //m_aProtocol.log("\tno stream open!\n");
484 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n");
485 throw new com
.sun
.star
.io
.IOException("stream not seekable");
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");