1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 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. 14 * 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). 20 * 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. 25 * 26 ************************************************************************/ 27 28 package helper; 29 30 import com.sun.star.uno.UnoRuntime; 31 32 33 import com.sun.star.lang.XMultiServiceFactory; 34 import com.sun.star.ucb.XSimpleFileAccess; 35 36 /** 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. 41 * 42 */ 43 public class StreamSimulator implements com.sun.star.io.XInputStream , 44 com.sun.star.io.XOutputStream , 45 com.sun.star.io.XSeekable 46 { 47 //_________________________________ 48 /** 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 53 * 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 57 */ 58 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 ; 63 64 //public ComplexTestEnvironment //m_aProtocol ; 65 public boolean m_bInWasUsed ; 66 public boolean m_bOutWasUsed ; 67 68 //_________________________________ 69 /** 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. 75 * 76 * @param sFileName 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). 80 * 81 * @param bInput 82 * it specify, which interface should work at this object. 83 * <TRUE/> => we simulate an input stream 84 * <FALSE/> => we simulate an output stream 85 * 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. 89 */ 90 public StreamSimulator( String sFileName , boolean bInput , 91 lib.TestParameters param ) throws com.sun.star.io.NotConnectedException 92 { 93 ////m_aProtocol = new ComplexTestEnvironment(); 94 m_sFileName = sFileName ; 95 m_bInWasUsed = false ; 96 m_bOutWasUsed = false ; 97 98 try 99 { 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");*/ 106 107 if (xHelper == null) 108 throw new com.sun.star.io.NotConnectedException("ucb helper not available. Can't create streams."); 109 110 if (bInput) 111 { 112 m_xInStream = xHelper.openFileRead(m_sFileName); 113 m_xSeek = (com.sun.star.io.XSeekable)UnoRuntime.queryInterface( 114 com.sun.star.io.XSeekable.class, 115 m_xInStream); 116 } 117 else 118 { 119 m_xOutStream = xHelper.openFileWrite(m_sFileName); 120 m_xSeek = (com.sun.star.io.XSeekable)UnoRuntime.queryInterface( 121 com.sun.star.io.XSeekable.class, 122 m_xOutStream); 123 } 124 } 125 catch(com.sun.star.uno.Exception exUno) 126 { 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."); 129 } 130 } 131 132 /* public void finalize() 133 { 134 ////m_aProtocol.log("finalize was called. Please check if it was right or not.\n"); 135 } */ 136 137 //_________________________________ 138 /** 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 142 * open in-stream. 143 */ 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 148 { 149 //m_aProtocol.log("readBytes(lData["+lData.length+"]["+lData[0]+"],"+nBytesToRead+")\n{\n"); 150 m_bInWasUsed = true; 151 152 if (m_xInStream == null) 153 { 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"); 156 } 157 158 int nRead = 0; 159 try 160 { 161 nRead = m_xInStream.readBytes(lData,nBytesToRead); 162 } 163 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 164 } 165 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 166 } 167 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 168 } 169 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 170 } 171 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 172 } 173 174 //m_aProtocol.log("\treads "+nRead+" bytes\n\tOK\n}\n"); 175 176 //if (nRead != nBytesToRead) 177 //m_aProtocol.log("there are some missing bytes for reading!\n"); 178 179 return nRead; 180 } 181 182 //_________________________________ 183 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 188 { 189 //m_aProtocol.log("readSomeBytes(lData["+lData.length+"]["+lData[0]+"],"+nMaxBytesToRead+")\n{\n"); 190 m_bInWasUsed = true; 191 192 if (m_xInStream == null) 193 { 194 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 195 throw new com.sun.star.io.NotConnectedException("stream not open"); 196 } 197 198 int nRead = 0; 199 try 200 { 201 nRead = m_xInStream.readSomeBytes(lData,nMaxBytesToRead); 202 } 203 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 204 } 205 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 206 } 207 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 208 } 209 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 210 } 211 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 212 } 213 214 //m_aProtocol.log("\treads "+nRead+" bytes\n\tOK\n}\n"); 215 216 //if (nRead != nMaxBytesToRead) 217 //m_aProtocol.log("there are some missing bytes for reading!"); 218 219 return nRead; 220 } 221 222 //_________________________________ 223 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 227 { 228 //m_aProtocol.log("skipBytes("+nBytesToSkip+")\n{\n"); 229 m_bInWasUsed = true; 230 231 if (m_xInStream == null) 232 { 233 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 234 throw new com.sun.star.io.NotConnectedException("stream not open"); 235 } 236 237 try 238 { 239 m_xInStream.skipBytes(nBytesToSkip); 240 } 241 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 242 } 243 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 244 } 245 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 246 } 247 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 248 } 249 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 250 } 251 252 //m_aProtocol.log("\tOK\n}\n"); 253 } 254 255 //_________________________________ 256 257 public int available() throws com.sun.star.io.NotConnectedException, 258 com.sun.star.io.IOException 259 { 260 //m_aProtocol.log("available()\n{\n"); 261 m_bInWasUsed = true; 262 263 if (m_xInStream == null) 264 { 265 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 266 throw new com.sun.star.io.NotConnectedException("stream not open"); 267 } 268 269 int nAvailable = 0; 270 try 271 { 272 nAvailable = m_xInStream.available(); 273 } 274 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n"); throw exConnect; 275 } 276 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 277 } 278 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 279 } 280 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 281 } 282 283 //m_aProtocol.log("\treturns "+nAvailable+" bytes\n\tOK\n}\n"); 284 return nAvailable; 285 } 286 287 //_________________________________ 288 289 public void closeInput() throws com.sun.star.io.NotConnectedException, 290 com.sun.star.io.IOException 291 { 292 //m_aProtocol.log("closeInput()\n{\n"); 293 m_bInWasUsed = true; 294 295 if (m_xInStream == null) 296 { 297 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 298 throw new com.sun.star.io.NotConnectedException("stream not open"); 299 } 300 301 try 302 { 303 m_xInStream.closeInput(); 304 } 305 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n"); throw exConnect; 306 } 307 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 308 } 309 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 310 } 311 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 312 } 313 314 //m_aProtocol.log("\tOK\n}\n"); 315 } 316 317 //_________________________________ 318 /** 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 322 * open out-stream. 323 */ 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 327 { 328 //m_aProtocol.log("writeBytes(lData["+lData.length+"])\n{\n"); 329 m_bOutWasUsed = true; 330 331 if (m_xOutStream == null) 332 { 333 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 334 throw new com.sun.star.io.NotConnectedException("stream not open"); 335 } 336 337 try 338 { 339 m_xOutStream.writeBytes(lData); 340 } 341 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 342 } 343 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 344 } 345 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 346 } 347 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 348 } 349 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 350 } 351 352 //m_aProtocol.log("\tOK\n}\n"); 353 } 354 355 //_________________________________ 356 357 public void flush() throws com.sun.star.io.NotConnectedException , 358 com.sun.star.io.BufferSizeExceededException , 359 com.sun.star.io.IOException 360 { 361 //m_aProtocol.log("flush()\n{\n"); 362 m_bOutWasUsed = true; 363 364 if (m_xOutStream == null) 365 { 366 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 367 throw new com.sun.star.io.NotConnectedException("stream not open"); 368 } 369 370 try 371 { 372 m_xOutStream.flush(); 373 } 374 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 375 } 376 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 377 } 378 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 379 } 380 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 381 } 382 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 383 } 384 //m_aProtocol.log("\tOK\n}\n"); 385 } 386 387 //_________________________________ 388 389 public void closeOutput() throws com.sun.star.io.NotConnectedException , 390 com.sun.star.io.BufferSizeExceededException, 391 com.sun.star.io.IOException 392 { 393 //m_aProtocol.log("closeOutput()\n{\n"); 394 m_bOutWasUsed = true; 395 396 if (m_xOutStream == null) 397 { 398 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 399 throw new com.sun.star.io.NotConnectedException("stream not open"); 400 } 401 402 try 403 { 404 m_xOutStream.closeOutput(); 405 } 406 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 407 } 408 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 409 } 410 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 411 } 412 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 413 } 414 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 415 } 416 417 //m_aProtocol.log("\tOK\n}\n"); 418 } 419 420 //_________________________________ 421 /** 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 425 * open stream. 426 */ 427 public void seek( /*IN*/long nLocation ) throws com.sun.star.lang.IllegalArgumentException, 428 com.sun.star.io.IOException 429 { 430 //m_aProtocol.log("seek("+nLocation+")\n{\n"); 431 432 if (m_xInStream != null) 433 m_bInWasUsed = true; 434 else 435 if (m_xOutStream != null) 436 m_bOutWasUsed = true; 437 else 438 //m_aProtocol.log("\tno stream open!\n"); 439 440 if (m_xSeek == null) 441 { 442 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n"); 443 throw new com.sun.star.io.IOException("stream not seekable"); 444 } 445 446 try 447 { 448 m_xSeek.seek(nLocation); 449 } 450 catch (com.sun.star.lang.IllegalArgumentException exArg ) { //m_aProtocol.log("\tgot IllegalArgumentException\n\tfailed\n}\n" ); throw exArg; 451 } 452 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 453 } 454 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 455 } 456 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 457 } 458 459 //m_aProtocol.log("\tOK\n}\n"); 460 } 461 462 //_________________________________ 463 464 public long getPosition() throws com.sun.star.io.IOException 465 { 466 //m_aProtocol.log("getPosition()\n{\n"); 467 468 if (m_xInStream != null) 469 m_bInWasUsed = true; 470 else 471 if (m_xOutStream != null) 472 m_bOutWasUsed = true; 473 else 474 //m_aProtocol.log("\tno stream open!\n"); 475 476 if (m_xSeek == null) 477 { 478 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n"); 479 throw new com.sun.star.io.IOException("stream not seekable"); 480 } 481 482 long nPos = 0; 483 try 484 { 485 nPos = m_xSeek.getPosition(); 486 } 487 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 488 } 489 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n"); throw exRuntime; 490 } 491 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 492 } 493 494 //m_aProtocol.log("\treturns pos="+nPos+"\n\tOK\n}\n"); 495 return nPos; 496 } 497 498 //_________________________________ 499 500 public long getLength() throws com.sun.star.io.IOException 501 { 502 //m_aProtocol.log("getLength()\n{\n"); 503 504 if (m_xInStream != null) 505 m_bInWasUsed = true; 506 else 507 if (m_xOutStream != null) 508 m_bOutWasUsed = true; 509 else 510 //m_aProtocol.log("\tno stream open!\n"); 511 512 if (m_xSeek == null) 513 { 514 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n"); 515 throw new com.sun.star.io.IOException("stream not seekable"); 516 } 517 518 long nLen = 0; 519 try 520 { 521 nLen = m_xSeek.getLength(); 522 } 523 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 524 } 525 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n"); throw exRuntime; 526 } 527 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 528 } 529 530 //m_aProtocol.log("\treturns len="+nLen+"\n\tOK\n}\n"); 531 return nLen; 532 } 533 } 534