1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package helper; 25 26 import com.sun.star.uno.UnoRuntime; 27 28 29 import com.sun.star.lang.XMultiServiceFactory; 30 import com.sun.star.ucb.XSimpleFileAccess; 31 32 /** 33 * It simulates an input and output stream and 34 * implements the interfaces XInputStream, XOutputStream. 35 * So it can be used for testing loading/saving of documents 36 * using streams instead of URLs. 37 * 38 */ 39 public class StreamSimulator implements com.sun.star.io.XInputStream , 40 com.sun.star.io.XOutputStream , 41 com.sun.star.io.XSeekable 42 { 43 //_________________________________ 44 /** 45 * @member m_sFileName name of the corrsponding file on disk 46 * @member m_xInStream the internal input stream for reading 47 * @member m_xOutStream the internal input stream for writing 48 * @member m_xSeek points at runtime to m_xInStream or m_xOutStream and make it seekable 49 * 50 * @member //m_aProtocol the external set protocol object for logging messages 51 * @member m_bInWasUsed indicates, that the input stream interface was used 52 * @member m_bOutWasUsed indicates, that the output stream interface was used 53 */ 54 55 private String m_sFileName ; 56 private com.sun.star.io.XInputStream m_xInStream ; 57 private com.sun.star.io.XOutputStream m_xOutStream ; 58 private com.sun.star.io.XSeekable m_xSeek ; 59 60 //public ComplexTestEnvironment //m_aProtocol ; 61 public boolean m_bInWasUsed ; 62 public boolean m_bOutWasUsed ; 63 64 //_________________________________ 65 /** 66 * construct a new instance of this class 67 * It set the name of the correspojnding file on disk, which 68 * should be source or target for the following operations on 69 * this object. And it regulate if it should function as 70 * input or output stream. 71 * 72 * @param sFileName 73 * name of the file on disk 74 * Will be used as source (if param bInput==true) 75 * or as target (if param bInput==false). 76 * 77 * @param bInput 78 * it specify, which interface should work at this object. 79 * <TRUE/> => we simulate an input stream 80 * <FALSE/> => we simulate an output stream 81 * 82 * @throw com.sun.star.io.NotConnectedException 83 * in case the internal streams to the file on disk couldn't established. 84 * They are neccessary. Otherwhise this simulator can't realy work. 85 */ StreamSimulator( String sFileName , boolean bInput , lib.TestParameters param )86 public StreamSimulator( String sFileName , boolean bInput , 87 lib.TestParameters param ) throws com.sun.star.io.NotConnectedException 88 { 89 ////m_aProtocol = new ComplexTestEnvironment(); 90 m_sFileName = sFileName ; 91 m_bInWasUsed = false ; 92 m_bOutWasUsed = false ; 93 94 try 95 { 96 XSimpleFileAccess xHelper = (XSimpleFileAccess) 97 UnoRuntime.queryInterface(XSimpleFileAccess.class, 98 ((XMultiServiceFactory)param.getMSF()).createInstance("com.sun.star.ucb.SimpleFileAccess")); 99 /* com.sun.star.ucb.XSimpleFileAccess xHelper = (com.sun.star.ucb.XSimpleFileAccess)OfficeConnect.createRemoteInstance( 100 com.sun.star.ucb.XSimpleFileAccess.class, 101 "com.sun.star.ucb.SimpleFileAccess");*/ 102 103 if (xHelper == null) 104 throw new com.sun.star.io.NotConnectedException("ucb helper not available. Can't create streams."); 105 106 if (bInput) 107 { 108 m_xInStream = xHelper.openFileRead(m_sFileName); 109 m_xSeek = (com.sun.star.io.XSeekable)UnoRuntime.queryInterface( 110 com.sun.star.io.XSeekable.class, 111 m_xInStream); 112 } 113 else 114 { 115 m_xOutStream = xHelper.openFileWrite(m_sFileName); 116 m_xSeek = (com.sun.star.io.XSeekable)UnoRuntime.queryInterface( 117 com.sun.star.io.XSeekable.class, 118 m_xOutStream); 119 } 120 } 121 catch(com.sun.star.uno.Exception exUno) 122 { 123 ////m_aProtocol.log("\tstream not open. throw NotConnectedException\n\n\tfailed\n}\n"); 124 throw new com.sun.star.io.NotConnectedException("Could not open the file."); 125 } 126 } 127 128 /* public void finalize() 129 { 130 ////m_aProtocol.log("finalize was called. Please check if it was right or not.\n"); 131 } */ 132 133 //_________________________________ 134 /** 135 * following methods simulates the XInputStream. 136 * The notice all actions inside the internal protocol 137 * and try to map all neccessary functions to the internal 138 * open in-stream. 139 */ readBytes( byte[][] lData , int nBytesToRead )140 public int readBytes( /*OUT*/ byte[][] lData , 141 /*IN*/ int nBytesToRead ) throws com.sun.star.io.NotConnectedException , 142 com.sun.star.io.BufferSizeExceededException, 143 com.sun.star.io.IOException 144 { 145 //m_aProtocol.log("readBytes(lData["+lData.length+"]["+lData[0]+"],"+nBytesToRead+")\n{\n"); 146 m_bInWasUsed = true; 147 148 if (m_xInStream == null) 149 { 150 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\n\tfailed\n}\n"); 151 throw new com.sun.star.io.NotConnectedException("stream not open"); 152 } 153 154 int nRead = 0; 155 try 156 { 157 nRead = m_xInStream.readBytes(lData,nBytesToRead); 158 } 159 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 160 } 161 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 162 } 163 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 164 } 165 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 166 } 167 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 168 } 169 170 //m_aProtocol.log("\treads "+nRead+" bytes\n\tOK\n}\n"); 171 172 //if (nRead != nBytesToRead) 173 //m_aProtocol.log("there are some missing bytes for reading!\n"); 174 175 return nRead; 176 } 177 178 //_________________________________ 179 readSomeBytes( byte[][] lData , int nMaxBytesToRead )180 public int readSomeBytes( /*OUT*/ byte[][] lData , 181 /*IN*/ int nMaxBytesToRead ) throws com.sun.star.io.NotConnectedException , 182 com.sun.star.io.BufferSizeExceededException , 183 com.sun.star.io.IOException 184 { 185 //m_aProtocol.log("readSomeBytes(lData["+lData.length+"]["+lData[0]+"],"+nMaxBytesToRead+")\n{\n"); 186 m_bInWasUsed = true; 187 188 if (m_xInStream == null) 189 { 190 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 191 throw new com.sun.star.io.NotConnectedException("stream not open"); 192 } 193 194 int nRead = 0; 195 try 196 { 197 nRead = m_xInStream.readSomeBytes(lData,nMaxBytesToRead); 198 } 199 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 200 } 201 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 202 } 203 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 204 } 205 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 206 } 207 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 208 } 209 210 //m_aProtocol.log("\treads "+nRead+" bytes\n\tOK\n}\n"); 211 212 //if (nRead != nMaxBytesToRead) 213 //m_aProtocol.log("there are some missing bytes for reading!"); 214 215 return nRead; 216 } 217 218 //_________________________________ 219 skipBytes( int nBytesToSkip )220 public void skipBytes( /*IN*/ int nBytesToSkip ) throws com.sun.star.io.NotConnectedException , 221 com.sun.star.io.BufferSizeExceededException , 222 com.sun.star.io.IOException 223 { 224 //m_aProtocol.log("skipBytes("+nBytesToSkip+")\n{\n"); 225 m_bInWasUsed = true; 226 227 if (m_xInStream == null) 228 { 229 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 230 throw new com.sun.star.io.NotConnectedException("stream not open"); 231 } 232 233 try 234 { 235 m_xInStream.skipBytes(nBytesToSkip); 236 } 237 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 238 } 239 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 240 } 241 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 242 } 243 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 244 } 245 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 246 } 247 248 //m_aProtocol.log("\tOK\n}\n"); 249 } 250 251 //_________________________________ 252 available()253 public int available() throws com.sun.star.io.NotConnectedException, 254 com.sun.star.io.IOException 255 { 256 //m_aProtocol.log("available()\n{\n"); 257 m_bInWasUsed = true; 258 259 if (m_xInStream == null) 260 { 261 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 262 throw new com.sun.star.io.NotConnectedException("stream not open"); 263 } 264 265 int nAvailable = 0; 266 try 267 { 268 nAvailable = m_xInStream.available(); 269 } 270 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n"); throw exConnect; 271 } 272 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 273 } 274 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 275 } 276 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 277 } 278 279 //m_aProtocol.log("\treturns "+nAvailable+" bytes\n\tOK\n}\n"); 280 return nAvailable; 281 } 282 283 //_________________________________ 284 closeInput()285 public void closeInput() throws com.sun.star.io.NotConnectedException, 286 com.sun.star.io.IOException 287 { 288 //m_aProtocol.log("closeInput()\n{\n"); 289 m_bInWasUsed = true; 290 291 if (m_xInStream == null) 292 { 293 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 294 throw new com.sun.star.io.NotConnectedException("stream not open"); 295 } 296 297 try 298 { 299 m_xInStream.closeInput(); 300 } 301 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n"); throw exConnect; 302 } 303 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 304 } 305 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 306 } 307 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 308 } 309 310 //m_aProtocol.log("\tOK\n}\n"); 311 } 312 313 //_________________________________ 314 /** 315 * following methods simulates the XOutputStream. 316 * The notice all actions inside the internal protocol 317 * and try to map all neccessary functions to the internal 318 * open out-stream. 319 */ writeBytes( byte[] lData )320 public void writeBytes( /*IN*/byte[] lData ) throws com.sun.star.io.NotConnectedException , 321 com.sun.star.io.BufferSizeExceededException , 322 com.sun.star.io.IOException 323 { 324 //m_aProtocol.log("writeBytes(lData["+lData.length+"])\n{\n"); 325 m_bOutWasUsed = true; 326 327 if (m_xOutStream == null) 328 { 329 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 330 throw new com.sun.star.io.NotConnectedException("stream not open"); 331 } 332 333 try 334 { 335 m_xOutStream.writeBytes(lData); 336 } 337 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 338 } 339 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 340 } 341 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 342 } 343 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 344 } 345 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 346 } 347 348 //m_aProtocol.log("\tOK\n}\n"); 349 } 350 351 //_________________________________ 352 flush()353 public void flush() throws com.sun.star.io.NotConnectedException , 354 com.sun.star.io.BufferSizeExceededException , 355 com.sun.star.io.IOException 356 { 357 //m_aProtocol.log("flush()\n{\n"); 358 m_bOutWasUsed = true; 359 360 if (m_xOutStream == null) 361 { 362 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 363 throw new com.sun.star.io.NotConnectedException("stream not open"); 364 } 365 366 try 367 { 368 m_xOutStream.flush(); 369 } 370 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 371 } 372 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 373 } 374 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 375 } 376 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 377 } 378 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 379 } 380 //m_aProtocol.log("\tOK\n}\n"); 381 } 382 383 //_________________________________ 384 closeOutput()385 public void closeOutput() throws com.sun.star.io.NotConnectedException , 386 com.sun.star.io.BufferSizeExceededException, 387 com.sun.star.io.IOException 388 { 389 //m_aProtocol.log("closeOutput()\n{\n"); 390 m_bOutWasUsed = true; 391 392 if (m_xOutStream == null) 393 { 394 //m_aProtocol.log("\tstream not open. throw NotConnectedException\n\tfailed\n}\n"); 395 throw new com.sun.star.io.NotConnectedException("stream not open"); 396 } 397 398 try 399 { 400 m_xOutStream.closeOutput(); 401 } 402 catch (com.sun.star.io.NotConnectedException exConnect) { //m_aProtocol.log("\tgot NotConnectedException\n\tfailed\n}\n" ); throw exConnect; 403 } 404 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { //m_aProtocol.log("\tgot BufferSizeExceededException\n\tfailed\n}\n"); throw exBuffer; 405 } 406 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 407 } 408 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 409 } 410 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 411 } 412 413 //m_aProtocol.log("\tOK\n}\n"); 414 } 415 416 //_________________________________ 417 /** 418 * following methods simulates the XSeekable. 419 * The notice all actions inside the internal protocol 420 * and try to map all neccessary functions to the internal 421 * open stream. 422 */ seek( long nLocation )423 public void seek( /*IN*/long nLocation ) throws com.sun.star.lang.IllegalArgumentException, 424 com.sun.star.io.IOException 425 { 426 //m_aProtocol.log("seek("+nLocation+")\n{\n"); 427 428 if (m_xInStream != null) 429 m_bInWasUsed = true; 430 else 431 if (m_xOutStream != null) 432 m_bOutWasUsed = true; 433 else 434 //m_aProtocol.log("\tno stream open!\n"); 435 436 if (m_xSeek == null) 437 { 438 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n"); 439 throw new com.sun.star.io.IOException("stream not seekable"); 440 } 441 442 try 443 { 444 m_xSeek.seek(nLocation); 445 } 446 catch (com.sun.star.lang.IllegalArgumentException exArg ) { //m_aProtocol.log("\tgot IllegalArgumentException\n\tfailed\n}\n" ); throw exArg; 447 } 448 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 449 } 450 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n" ); throw exRuntime; 451 } 452 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 453 } 454 455 //m_aProtocol.log("\tOK\n}\n"); 456 } 457 458 //_________________________________ 459 getPosition()460 public long getPosition() throws com.sun.star.io.IOException 461 { 462 //m_aProtocol.log("getPosition()\n{\n"); 463 464 if (m_xInStream != null) 465 m_bInWasUsed = true; 466 else 467 if (m_xOutStream != null) 468 m_bOutWasUsed = true; 469 else 470 //m_aProtocol.log("\tno stream open!\n"); 471 472 if (m_xSeek == null) 473 { 474 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n"); 475 throw new com.sun.star.io.IOException("stream not seekable"); 476 } 477 478 long nPos = 0; 479 try 480 { 481 nPos = m_xSeek.getPosition(); 482 } 483 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 484 } 485 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n"); throw exRuntime; 486 } 487 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 488 } 489 490 //m_aProtocol.log("\treturns pos="+nPos+"\n\tOK\n}\n"); 491 return nPos; 492 } 493 494 //_________________________________ 495 getLength()496 public long getLength() throws com.sun.star.io.IOException 497 { 498 //m_aProtocol.log("getLength()\n{\n"); 499 500 if (m_xInStream != null) 501 m_bInWasUsed = true; 502 else 503 if (m_xOutStream != null) 504 m_bOutWasUsed = true; 505 else 506 //m_aProtocol.log("\tno stream open!\n"); 507 508 if (m_xSeek == null) 509 { 510 //m_aProtocol.log("\tstream not seekable. throw IOException\n\tfailed\n}\n"); 511 throw new com.sun.star.io.IOException("stream not seekable"); 512 } 513 514 long nLen = 0; 515 try 516 { 517 nLen = m_xSeek.getLength(); 518 } 519 catch (com.sun.star.io.IOException exIO ) { //m_aProtocol.log("\tgot IOException\n\tfailed\n}\n" ); throw exIO; 520 } 521 catch (com.sun.star.uno.RuntimeException exRuntime) { //m_aProtocol.log("\tgot RuntimeException\n\tfailed\n}\n"); throw exRuntime; 522 } 523 catch (com.sun.star.uno.Exception exUno ) { //m_aProtocol.log("\tgot Exception\n\tfailed\n}\n" ); 524 } 525 526 //m_aProtocol.log("\treturns len="+nLen+"\n\tOK\n}\n"); 527 return nLen; 528 } 529 } 530