1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 import com
.sun
.star
.uno
.UnoRuntime
;
33 import com
.sun
.star
.lang
.XMultiServiceFactory
;
34 import com
.sun
.star
.ucb
.XSimpleFileAccess
;
37 * It simulates an input and output stream and
38 * implements the interfaces XInputStream, XOutputStream.
39 * So it can be used for testing loading/saving of documents
40 * using streams instead of URLs.
43 public class StreamSimulator
implements com
.sun
.star
.io
.XInputStream
,
44 com
.sun
.star
.io
.XOutputStream
,
45 com
.sun
.star
.io
.XSeekable
47 //_________________________________
49 * @member m_sFileName name of the corrsponding file on disk
50 * @member m_xInStream the internal input stream for reading
51 * @member m_xOutStream the internal input stream for writing
52 * @member m_xSeek points at runtime to m_xInStream or m_xOutStream and make it seekable
54 * @member //m_aProtocol the external set protocol object for logging messages
55 * @member m_bInWasUsed indicates, that the input stream interface was used
56 * @member m_bOutWasUsed indicates, that the output stream interface was used
59 private String m_sFileName
;
60 private com
.sun
.star
.io
.XInputStream m_xInStream
;
61 private com
.sun
.star
.io
.XOutputStream m_xOutStream
;
62 private com
.sun
.star
.io
.XSeekable m_xSeek
;
64 //public ComplexTestEnvironment //m_aProtocol ;
65 public boolean m_bInWasUsed
;
66 public boolean m_bOutWasUsed
;
68 //_________________________________
70 * construct a new instance of this class
71 * It set the name of the correspojnding file on disk, which
72 * should be source or target for the following operations on
73 * this object. And it regulate if it should function as
74 * input or output stream.
77 * name of the file on disk
78 * Will be used as source (if param bInput==true)
79 * or as target (if param bInput==false).
82 * it specify, which interface should work at this object.
83 * <TRUE/> => we simulate an input stream
84 * <FALSE/> => we simulate an output stream
86 * @throw com.sun.star.io.NotConnectedException
87 * in case the internal streams to the file on disk couldn't established.
88 * They are neccessary. Otherwhise this simulator can't realy work.
90 public StreamSimulator( String sFileName
, boolean bInput
,
91 lib
.TestParameters param
) throws com
.sun
.star
.io
.NotConnectedException
93 ////m_aProtocol = new ComplexTestEnvironment();
94 m_sFileName
= sFileName
;
95 m_bInWasUsed
= false ;
96 m_bOutWasUsed
= false ;
100 XSimpleFileAccess xHelper
= (XSimpleFileAccess
)
101 UnoRuntime
.queryInterface(XSimpleFileAccess
.class,
102 ((XMultiServiceFactory
)param
.getMSF()).createInstance("com.sun.star.ucb.SimpleFileAccess"));
103 /* com.sun.star.ucb.XSimpleFileAccess xHelper = (com.sun.star.ucb.XSimpleFileAccess)OfficeConnect.createRemoteInstance(
104 com.sun.star.ucb.XSimpleFileAccess.class,
105 "com.sun.star.ucb.SimpleFileAccess");*/
108 throw new com
.sun
.star
.io
.NotConnectedException("ucb helper not available. Can't create streams.");
112 m_xInStream
= xHelper
.openFileRead(m_sFileName
);
113 m_xSeek
= (com
.sun
.star
.io
.XSeekable
)UnoRuntime
.queryInterface(
114 com
.sun
.star
.io
.XSeekable
.class,
119 m_xOutStream
= xHelper
.openFileWrite(m_sFileName
);
120 m_xSeek
= (com
.sun
.star
.io
.XSeekable
)UnoRuntime
.queryInterface(
121 com
.sun
.star
.io
.XSeekable
.class,
125 catch(com
.sun
.star
.uno
.Exception exUno
)
127 ////m_aProtocol.log("\tstream not open. throw NotConnectedException\n\n\tfailed\n}\n");
128 throw new com
.sun
.star
.io
.NotConnectedException("Could not open the file.");
132 /* public void finalize()
134 ////m_aProtocol.log("finalize was called. Please check if it was right or not.\n");
137 //_________________________________
139 * following methods simulates the XInputStream.
140 * The notice all actions inside the internal protocol
141 * and try to map all neccessary functions to the internal
144 public int readBytes( /*OUT*/ byte[][] lData
,
145 /*IN*/ int nBytesToRead
) throws com
.sun
.star
.io
.NotConnectedException
,
146 com
.sun
.star
.io
.BufferSizeExceededException
,
147 com
.sun
.star
.io
.IOException
149 //m_aProtocol.log("readBytes(lData["+lData.length+"]["+lData[0]+"],"+nBytesToRead+")\n{\n");
152 if (m_xInStream
== null)
154 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\n\tfailed\n}\n");
155 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
161 nRead
= m_xInStream
.readBytes(lData
,nBytesToRead
);
163 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
165 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
167 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
169 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
171 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
174 //m_aProtocol.log("\treads "+nRead+" bytes\n\tOK\n}\n");
176 //if (nRead != nBytesToRead)
177 //m_aProtocol.log("there are some missing bytes for reading!\n");
182 //_________________________________
184 public int readSomeBytes( /*OUT*/ byte[][] lData
,
185 /*IN*/ int nMaxBytesToRead
) throws com
.sun
.star
.io
.NotConnectedException
,
186 com
.sun
.star
.io
.BufferSizeExceededException
,
187 com
.sun
.star
.io
.IOException
189 //m_aProtocol.log("readSomeBytes(lData["+lData.length+"]["+lData[0]+"],"+nMaxBytesToRead+")\n{\n");
192 if (m_xInStream
== null)
194 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
195 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
201 nRead
= m_xInStream
.readSomeBytes(lData
,nMaxBytesToRead
);
203 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
205 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
207 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
209 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
211 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
214 //m_aProtocol.log("\treads "+nRead+" bytes\n\tOK\n}\n");
216 //if (nRead != nMaxBytesToRead)
217 //m_aProtocol.log("there are some missing bytes for reading!");
222 //_________________________________
224 public void skipBytes( /*IN*/ int nBytesToSkip
) throws com
.sun
.star
.io
.NotConnectedException
,
225 com
.sun
.star
.io
.BufferSizeExceededException
,
226 com
.sun
.star
.io
.IOException
228 //m_aProtocol.log("skipBytes("+nBytesToSkip+")\n{\n");
231 if (m_xInStream
== null)
233 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
234 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
239 m_xInStream
.skipBytes(nBytesToSkip
);
241 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
243 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
245 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
247 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
249 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
252 //m_aProtocol.log("\tOK\n}\n");
255 //_________________________________
257 public int available() throws com
.sun
.star
.io
.NotConnectedException
,
258 com
.sun
.star
.io
.IOException
260 //m_aProtocol.log("available()\n{\n");
263 if (m_xInStream
== null)
265 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
266 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
272 nAvailable
= m_xInStream
.available();
274 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n"); throw exConnect;
276 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
278 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
280 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
283 //m_aProtocol.log("\treturns "+nAvailable+" bytes\n\tOK\n}\n");
287 //_________________________________
289 public void closeInput() throws com
.sun
.star
.io
.NotConnectedException
,
290 com
.sun
.star
.io
.IOException
292 //m_aProtocol.log("closeInput()\n{\n");
295 if (m_xInStream
== null)
297 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
298 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
303 m_xInStream
.closeInput();
305 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n"); throw exConnect;
307 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
309 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
311 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
314 //m_aProtocol.log("\tOK\n}\n");
317 //_________________________________
319 * following methods simulates the XOutputStream.
320 * The notice all actions inside the internal protocol
321 * and try to map all neccessary functions to the internal
324 public void writeBytes( /*IN*/byte[] lData
) throws com
.sun
.star
.io
.NotConnectedException
,
325 com
.sun
.star
.io
.BufferSizeExceededException
,
326 com
.sun
.star
.io
.IOException
328 //m_aProtocol.log("writeBytes(lData["+lData.length+"])\n{\n");
329 m_bOutWasUsed
= true;
331 if (m_xOutStream
== null)
333 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
334 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
339 m_xOutStream
.writeBytes(lData
);
341 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
343 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
345 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
347 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
349 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
352 //m_aProtocol.log("\tOK\n}\n");
355 //_________________________________
357 public void flush() throws com
.sun
.star
.io
.NotConnectedException
,
358 com
.sun
.star
.io
.BufferSizeExceededException
,
359 com
.sun
.star
.io
.IOException
361 //m_aProtocol.log("flush()\n{\n");
362 m_bOutWasUsed
= true;
364 if (m_xOutStream
== null)
366 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
367 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
372 m_xOutStream
.flush();
374 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
376 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
378 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
380 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
382 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
384 //m_aProtocol.log("\tOK\n}\n");
387 //_________________________________
389 public void closeOutput() throws com
.sun
.star
.io
.NotConnectedException
,
390 com
.sun
.star
.io
.BufferSizeExceededException
,
391 com
.sun
.star
.io
.IOException
393 //m_aProtocol.log("closeOutput()\n{\n");
394 m_bOutWasUsed
= true;
396 if (m_xOutStream
== null)
398 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
399 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
404 m_xOutStream
.closeOutput();
406 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
408 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
410 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
412 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
414 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
417 //m_aProtocol.log("\tOK\n}\n");
420 //_________________________________
422 * following methods simulates the XSeekable.
423 * The notice all actions inside the internal protocol
424 * and try to map all neccessary functions to the internal
427 public void seek( /*IN*/long nLocation
) throws com
.sun
.star
.lang
.IllegalArgumentException
,
428 com
.sun
.star
.io
.IOException
430 //m_aProtocol.log("seek("+nLocation+")\n{\n");
432 if (m_xInStream
!= null)
435 if (m_xOutStream
!= null)
436 m_bOutWasUsed
= true;
438 //m_aProtocol.log("\tno stream open!\n");
442 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n");
443 throw new com
.sun
.star
.io
.IOException("stream not seekable");
448 m_xSeek
.seek(nLocation
);
450 catch (com
.sun
.star
.lang
.IllegalArgumentException exArg
) { //m_aProtocol.log("\tgot IllegalArgumentException\n\tfailed\n}\n" ); throw exArg;
452 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
454 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
456 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
459 //m_aProtocol.log("\tOK\n}\n");
462 //_________________________________
464 public long getPosition() throws com
.sun
.star
.io
.IOException
466 //m_aProtocol.log("getPosition()\n{\n");
468 if (m_xInStream
!= null)
471 if (m_xOutStream
!= null)
472 m_bOutWasUsed
= true;
474 //m_aProtocol.log("\tno stream open!\n");
478 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n");
479 throw new com
.sun
.star
.io
.IOException("stream not seekable");
485 nPos
= m_xSeek
.getPosition();
487 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
489 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n"); throw exRuntime;
491 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
494 //m_aProtocol.log("\treturns pos="+nPos+"\n\tOK\n}\n");
498 //_________________________________
500 public long getLength() throws com
.sun
.star
.io
.IOException
502 //m_aProtocol.log("getLength()\n{\n");
504 if (m_xInStream
!= null)
507 if (m_xOutStream
!= null)
508 m_bOutWasUsed
= true;
510 //m_aProtocol.log("\tno stream open!\n");
514 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n");
515 throw new com
.sun
.star
.io
.IOException("stream not seekable");
521 nLen
= m_xSeek
.getLength();
523 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
525 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n"); throw exRuntime;
527 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
530 //m_aProtocol.log("\treturns len="+nLen+"\n\tOK\n}\n");