1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: StreamSimulator.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import com
.sun
.star
.uno
.UnoRuntime
;
36 import com
.sun
.star
.lang
.XMultiServiceFactory
;
37 import com
.sun
.star
.ucb
.XSimpleFileAccess
;
40 * It simulates an input and output stream and
41 * implements the interfaces XInputStream, XOutputStream.
42 * So it can be used for testing loading/saving of documents
43 * using streams instead of URLs.
46 public class StreamSimulator
implements com
.sun
.star
.io
.XInputStream
,
47 com
.sun
.star
.io
.XOutputStream
,
48 com
.sun
.star
.io
.XSeekable
50 //_________________________________
52 * @member m_sFileName name of the corrsponding file on disk
53 * @member m_xInStream the internal input stream for reading
54 * @member m_xOutStream the internal input stream for writing
55 * @member m_xSeek points at runtime to m_xInStream or m_xOutStream and make it seekable
57 * @member //m_aProtocol the external set protocol object for logging messages
58 * @member m_bInWasUsed indicates, that the input stream interface was used
59 * @member m_bOutWasUsed indicates, that the output stream interface was used
62 private String m_sFileName
;
63 private com
.sun
.star
.io
.XInputStream m_xInStream
;
64 private com
.sun
.star
.io
.XOutputStream m_xOutStream
;
65 private com
.sun
.star
.io
.XSeekable m_xSeek
;
67 //public ComplexTestEnvironment //m_aProtocol ;
68 public boolean m_bInWasUsed
;
69 public boolean m_bOutWasUsed
;
71 //_________________________________
73 * construct a new instance of this class
74 * It set the name of the correspojnding file on disk, which
75 * should be source or target for the following operations on
76 * this object. And it regulate if it should function as
77 * input or output stream.
80 * name of the file on disk
81 * Will be used as source (if param bInput==true)
82 * or as target (if param bInput==false).
85 * it specify, which interface should work at this object.
86 * <TRUE/> => we simulate an input stream
87 * <FALSE/> => we simulate an output stream
89 * @throw com.sun.star.io.NotConnectedException
90 * in case the internal streams to the file on disk couldn't established.
91 * They are neccessary. Otherwhise this simulator can't realy work.
93 public StreamSimulator( String sFileName
, boolean bInput
,
94 lib
.TestParameters param
) throws com
.sun
.star
.io
.NotConnectedException
96 ////m_aProtocol = new ComplexTestEnvironment();
97 m_sFileName
= sFileName
;
98 m_bInWasUsed
= false ;
99 m_bOutWasUsed
= false ;
103 XSimpleFileAccess xHelper
= (XSimpleFileAccess
)
104 UnoRuntime
.queryInterface(XSimpleFileAccess
.class,
105 ((XMultiServiceFactory
)param
.getMSF()).createInstance("com.sun.star.ucb.SimpleFileAccess"));
106 /* com.sun.star.ucb.XSimpleFileAccess xHelper = (com.sun.star.ucb.XSimpleFileAccess)OfficeConnect.createRemoteInstance(
107 com.sun.star.ucb.XSimpleFileAccess.class,
108 "com.sun.star.ucb.SimpleFileAccess");*/
111 throw new com
.sun
.star
.io
.NotConnectedException("ucb helper not available. Can't create streams.");
115 m_xInStream
= xHelper
.openFileRead(m_sFileName
);
116 m_xSeek
= (com
.sun
.star
.io
.XSeekable
)UnoRuntime
.queryInterface(
117 com
.sun
.star
.io
.XSeekable
.class,
122 m_xOutStream
= xHelper
.openFileWrite(m_sFileName
);
123 m_xSeek
= (com
.sun
.star
.io
.XSeekable
)UnoRuntime
.queryInterface(
124 com
.sun
.star
.io
.XSeekable
.class,
128 catch(com
.sun
.star
.uno
.Exception exUno
)
130 ////m_aProtocol.log("\tstream not open. throw NotConnectedException\n\n\tfailed\n}\n");
131 throw new com
.sun
.star
.io
.NotConnectedException("Could not open the file.");
135 /* public void finalize()
137 ////m_aProtocol.log("finalize was called. Please check if it was right or not.\n");
140 //_________________________________
142 * following methods simulates the XInputStream.
143 * The notice all actions inside the internal protocol
144 * and try to map all neccessary functions to the internal
147 public int readBytes( /*OUT*/ byte[][] lData
,
148 /*IN*/ int nBytesToRead
) throws com
.sun
.star
.io
.NotConnectedException
,
149 com
.sun
.star
.io
.BufferSizeExceededException
,
150 com
.sun
.star
.io
.IOException
152 //m_aProtocol.log("readBytes(lData["+lData.length+"]["+lData[0]+"],"+nBytesToRead+")\n{\n");
155 if (m_xInStream
== null)
157 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\n\tfailed\n}\n");
158 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
164 nRead
= m_xInStream
.readBytes(lData
,nBytesToRead
);
166 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
168 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
170 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
172 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
174 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
177 //m_aProtocol.log("\treads "+nRead+" bytes\n\tOK\n}\n");
179 //if (nRead != nBytesToRead)
180 //m_aProtocol.log("there are some missing bytes for reading!\n");
185 //_________________________________
187 public int readSomeBytes( /*OUT*/ byte[][] lData
,
188 /*IN*/ int nMaxBytesToRead
) throws com
.sun
.star
.io
.NotConnectedException
,
189 com
.sun
.star
.io
.BufferSizeExceededException
,
190 com
.sun
.star
.io
.IOException
192 //m_aProtocol.log("readSomeBytes(lData["+lData.length+"]["+lData[0]+"],"+nMaxBytesToRead+")\n{\n");
195 if (m_xInStream
== null)
197 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
198 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
204 nRead
= m_xInStream
.readSomeBytes(lData
,nMaxBytesToRead
);
206 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
208 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
210 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
212 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
214 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
217 //m_aProtocol.log("\treads "+nRead+" bytes\n\tOK\n}\n");
219 //if (nRead != nMaxBytesToRead)
220 //m_aProtocol.log("there are some missing bytes for reading!");
225 //_________________________________
227 public void skipBytes( /*IN*/ int nBytesToSkip
) throws com
.sun
.star
.io
.NotConnectedException
,
228 com
.sun
.star
.io
.BufferSizeExceededException
,
229 com
.sun
.star
.io
.IOException
231 //m_aProtocol.log("skipBytes("+nBytesToSkip+")\n{\n");
234 if (m_xInStream
== null)
236 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
237 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
242 m_xInStream
.skipBytes(nBytesToSkip
);
244 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
246 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
248 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
250 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
252 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
255 //m_aProtocol.log("\tOK\n}\n");
258 //_________________________________
260 public int available() throws com
.sun
.star
.io
.NotConnectedException
,
261 com
.sun
.star
.io
.IOException
263 //m_aProtocol.log("available()\n{\n");
266 if (m_xInStream
== null)
268 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
269 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
275 nAvailable
= m_xInStream
.available();
277 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n"); throw exConnect;
279 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
281 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
283 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
286 //m_aProtocol.log("\treturns "+nAvailable+" bytes\n\tOK\n}\n");
290 //_________________________________
292 public void closeInput() throws com
.sun
.star
.io
.NotConnectedException
,
293 com
.sun
.star
.io
.IOException
295 //m_aProtocol.log("closeInput()\n{\n");
298 if (m_xInStream
== null)
300 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
301 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
306 m_xInStream
.closeInput();
308 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n"); throw exConnect;
310 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
312 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
314 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
317 //m_aProtocol.log("\tOK\n}\n");
320 //_________________________________
322 * following methods simulates the XOutputStream.
323 * The notice all actions inside the internal protocol
324 * and try to map all neccessary functions to the internal
327 public void writeBytes( /*IN*/byte[] lData
) throws com
.sun
.star
.io
.NotConnectedException
,
328 com
.sun
.star
.io
.BufferSizeExceededException
,
329 com
.sun
.star
.io
.IOException
331 //m_aProtocol.log("writeBytes(lData["+lData.length+"])\n{\n");
332 m_bOutWasUsed
= true;
334 if (m_xOutStream
== null)
336 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
337 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
342 m_xOutStream
.writeBytes(lData
);
344 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
346 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
348 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
350 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
352 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
355 //m_aProtocol.log("\tOK\n}\n");
358 //_________________________________
360 public void flush() throws com
.sun
.star
.io
.NotConnectedException
,
361 com
.sun
.star
.io
.BufferSizeExceededException
,
362 com
.sun
.star
.io
.IOException
364 //m_aProtocol.log("flush()\n{\n");
365 m_bOutWasUsed
= true;
367 if (m_xOutStream
== null)
369 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
370 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
375 m_xOutStream
.flush();
377 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
379 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
381 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
383 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
385 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
387 //m_aProtocol.log("\tOK\n}\n");
390 //_________________________________
392 public void closeOutput() throws com
.sun
.star
.io
.NotConnectedException
,
393 com
.sun
.star
.io
.BufferSizeExceededException
,
394 com
.sun
.star
.io
.IOException
396 //m_aProtocol.log("closeOutput()\n{\n");
397 m_bOutWasUsed
= true;
399 if (m_xOutStream
== null)
401 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n");
402 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
407 m_xOutStream
.closeOutput();
409 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect;
411 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer;
413 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
415 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
417 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
420 //m_aProtocol.log("\tOK\n}\n");
423 //_________________________________
425 * following methods simulates the XSeekable.
426 * The notice all actions inside the internal protocol
427 * and try to map all neccessary functions to the internal
430 public void seek( /*IN*/long nLocation
) throws com
.sun
.star
.lang
.IllegalArgumentException
,
431 com
.sun
.star
.io
.IOException
433 //m_aProtocol.log("seek("+nLocation+")\n{\n");
435 if (m_xInStream
!= null)
438 if (m_xOutStream
!= null)
439 m_bOutWasUsed
= true;
441 //m_aProtocol.log("\tno stream open!\n");
445 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n");
446 throw new com
.sun
.star
.io
.IOException("stream not seekable");
451 m_xSeek
.seek(nLocation
);
453 catch (com
.sun
.star
.lang
.IllegalArgumentException exArg
) { //m_aProtocol.log("\tgot IllegalArgumentException\n\tfailed\n}\n" ); throw exArg;
455 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
457 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime;
459 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
462 //m_aProtocol.log("\tOK\n}\n");
465 //_________________________________
467 public long getPosition() throws com
.sun
.star
.io
.IOException
469 //m_aProtocol.log("getPosition()\n{\n");
471 if (m_xInStream
!= null)
474 if (m_xOutStream
!= null)
475 m_bOutWasUsed
= true;
477 //m_aProtocol.log("\tno stream open!\n");
481 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n");
482 throw new com
.sun
.star
.io
.IOException("stream not seekable");
488 nPos
= m_xSeek
.getPosition();
490 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
492 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n"); throw exRuntime;
494 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
497 //m_aProtocol.log("\treturns pos="+nPos+"\n\tOK\n}\n");
501 //_________________________________
503 public long getLength() throws com
.sun
.star
.io
.IOException
505 //m_aProtocol.log("getLength()\n{\n");
507 if (m_xInStream
!= null)
510 if (m_xOutStream
!= null)
511 m_bOutWasUsed
= true;
513 //m_aProtocol.log("\tno stream open!\n");
517 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n");
518 throw new com
.sun
.star
.io
.IOException("stream not seekable");
524 nLen
= m_xSeek
.getLength();
526 catch (com
.sun
.star
.io
.IOException exIO
) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO;
528 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n"); throw exRuntime;
530 catch (com
.sun
.star
.uno
.Exception exUno
) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" );
533 //m_aProtocol.log("\treturns len="+nLen+"\n\tOK\n}\n");