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 package complex.loadAllDocuments; 24 25 import com.sun.star.uno.UnoRuntime; 26 import com.sun.star.ucb.XSimpleFileAccess; 27 import com.sun.star.lang.XMultiServiceFactory; 28 29 /** 30 * Simulates an input and output stream and 31 * implements the interfaces XInputStream, XOutputStream. 32 * So it can be used for testing loading/saving of documents 33 * using streams instead of URLs. 34 */ 35 public class StreamSimulator implements com.sun.star.io.XInputStream , 36 com.sun.star.io.XOutputStream , 37 com.sun.star.io.XSeekable 38 { 39 //_________________________________ 40 /** 41 * @member m_sFileName name of the corrsponding file on disk 42 * @member m_xInStream the internal input stream for reading 43 * @member m_xOutStream the internal input stream for writing 44 * @member m_xSeek points at runtime to m_xInStream or m_xOutStream and make it seekable 45 * 46 * @member m_bInWasUsed indicates, that the input stream interface was used 47 * @member m_bOutWasUsed indicates, that the output stream interface was used 48 */ 49 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 ; 54 55 public boolean m_bInWasUsed ; 56 public boolean m_bOutWasUsed ; 57 58 /** 59 * construct a new instance of this class 60 * It set the name of the correspojnding file on disk, which 61 * should be source or target for the following operations on 62 * this object. And it regulate if it should function as 63 * input or output stream. 64 * 65 * @param sFileName 66 * name of the file on disk 67 * Will be used as source (if param bInput==true) 68 * or as target (if param bInput==false). 69 * 70 * @param bInput 71 * it specify, which interface should work at this object. 72 * <TRUE/> => we simulate an input stream 73 * <FALSE/> => we simulate an output stream 74 * 75 * @throw com.sun.star.io.NotConnectedException 76 * in case the internal streams to the file on disk couldn't 77 * be established. 78 * They are neccessary. Otherwhise this simulator can't 79 * really work. 80 */ StreamSimulator(XMultiServiceFactory xMSF, String sFileName, boolean bInput)81 public StreamSimulator(XMultiServiceFactory xMSF, 82 String sFileName, boolean bInput) 83 throws com.sun.star.io.NotConnectedException 84 { 85 m_sFileName = sFileName ; 86 m_bInWasUsed = false ; 87 m_bOutWasUsed = false ; 88 89 try 90 { 91 XSimpleFileAccess xHelper = (XSimpleFileAccess) 92 UnoRuntime.queryInterface(XSimpleFileAccess.class, 93 xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess")); 94 95 if (xHelper == null) 96 throw new com.sun.star.io.NotConnectedException( 97 "ucb helper not available. Can't create streams."); 98 99 if (bInput) 100 { 101 m_xInStream = xHelper.openFileRead(m_sFileName); 102 m_xSeek = (com.sun.star.io.XSeekable)UnoRuntime.queryInterface( 103 com.sun.star.io.XSeekable.class, 104 m_xInStream); 105 } 106 else 107 { 108 m_xOutStream = xHelper.openFileWrite(m_sFileName); 109 m_xSeek = (com.sun.star.io.XSeekable)UnoRuntime.queryInterface( 110 com.sun.star.io.XSeekable.class, 111 m_xOutStream); 112 } 113 } 114 catch(com.sun.star.uno.Exception exUno) 115 { 116 throw new com.sun.star.io.NotConnectedException( 117 "Could not open the file."); 118 } 119 } 120 121 /** 122 * following methods simulates the XInputStream. 123 * The notice all actions inside the internal protocol 124 * and try to map all neccessary functions to the internal 125 * open in-stream. 126 */ readBytes(byte[][] lData, int nBytesToRead )127 public int readBytes(byte[][] lData, int nBytesToRead ) 128 throws com.sun.star.io.NotConnectedException, 129 com.sun.star.io.BufferSizeExceededException, 130 com.sun.star.io.IOException 131 { 132 m_bInWasUsed = true; 133 134 if (m_xInStream == null) 135 { 136 throw new com.sun.star.io.NotConnectedException("stream not open"); 137 } 138 139 int nRead = 0; 140 try 141 { 142 nRead = m_xInStream.readBytes(lData,nBytesToRead); 143 } 144 catch (com.sun.star.io.NotConnectedException exConnect) { 145 } 146 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { 147 } 148 catch (com.sun.star.io.IOException exIO ) { 149 } 150 catch (com.sun.star.uno.RuntimeException exRuntime) { 151 } 152 catch (com.sun.star.uno.Exception exUno ) { 153 } 154 155 156 return nRead; 157 } 158 readSomeBytes(byte[][] lData, int nMaxBytesToRead)159 public int readSomeBytes(byte[][] lData, int nMaxBytesToRead) 160 throws com.sun.star.io.NotConnectedException, 161 com.sun.star.io.BufferSizeExceededException , 162 com.sun.star.io.IOException 163 { 164 m_bInWasUsed = true; 165 166 if (m_xInStream == null) 167 { 168 throw new com.sun.star.io.NotConnectedException("stream not open"); 169 } 170 171 int nRead = 0; 172 try 173 { 174 nRead = m_xInStream.readSomeBytes(lData,nMaxBytesToRead); 175 } 176 catch (com.sun.star.io.NotConnectedException exConnect) { 177 } 178 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { 179 } 180 catch (com.sun.star.io.IOException exIO ) { 181 } 182 catch (com.sun.star.uno.RuntimeException exRuntime) { 183 } 184 catch (com.sun.star.uno.Exception exUno ) { 185 } 186 187 return nRead; 188 } 189 190 //_________________________________ 191 skipBytes(int nBytesToSkip)192 public void skipBytes(int nBytesToSkip) 193 throws com.sun.star.io.NotConnectedException, 194 com.sun.star.io.BufferSizeExceededException , 195 com.sun.star.io.IOException 196 { 197 m_bInWasUsed = true; 198 199 if (m_xInStream == null) 200 { 201 throw new com.sun.star.io.NotConnectedException("stream not open"); 202 } 203 204 try 205 { 206 m_xInStream.skipBytes(nBytesToSkip); 207 } 208 catch (com.sun.star.io.NotConnectedException exConnect) { 209 } 210 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { 211 } 212 catch (com.sun.star.io.IOException exIO ) { 213 } 214 catch (com.sun.star.uno.RuntimeException exRuntime) { 215 } 216 catch (com.sun.star.uno.Exception exUno ) { 217 } 218 219 } 220 available()221 public int available() throws com.sun.star.io.NotConnectedException, 222 com.sun.star.io.IOException 223 { 224 m_bInWasUsed = true; 225 226 if (m_xInStream == null) 227 { 228 throw new com.sun.star.io.NotConnectedException("stream not open"); 229 } 230 231 int nAvailable = 0; 232 try 233 { 234 nAvailable = m_xInStream.available(); 235 } 236 catch (com.sun.star.io.NotConnectedException exConnect) { 237 } 238 catch (com.sun.star.io.IOException exIO ) { 239 } 240 catch (com.sun.star.uno.RuntimeException exRuntime) { 241 } 242 catch (com.sun.star.uno.Exception exUno ) { 243 } 244 245 return nAvailable; 246 } 247 248 //_________________________________ 249 closeInput()250 public void closeInput() throws com.sun.star.io.NotConnectedException, 251 com.sun.star.io.IOException 252 { 253 m_bInWasUsed = true; 254 255 if (m_xInStream == null) 256 { 257 throw new com.sun.star.io.NotConnectedException("stream not open"); 258 } 259 260 try 261 { 262 m_xInStream.closeInput(); 263 } 264 catch (com.sun.star.io.NotConnectedException exConnect) { 265 } 266 catch (com.sun.star.io.IOException exIO ) { 267 } 268 catch (com.sun.star.uno.RuntimeException exRuntime) { 269 } 270 catch (com.sun.star.uno.Exception exUno ) { 271 } 272 273 } 274 275 /** 276 * following methods simulates the XOutputStream. 277 * The notice all actions inside the internal protocol 278 * and try to map all neccessary functions to the internal 279 * open out-stream. 280 */ writeBytes(byte[] lData)281 public void writeBytes(byte[] lData) 282 throws com.sun.star.io.NotConnectedException, 283 com.sun.star.io.BufferSizeExceededException , 284 com.sun.star.io.IOException 285 { 286 m_bOutWasUsed = true; 287 288 if (m_xOutStream == null) 289 { 290 throw new com.sun.star.io.NotConnectedException("stream not open"); 291 } 292 293 try 294 { 295 m_xOutStream.writeBytes(lData); 296 } 297 catch (com.sun.star.io.NotConnectedException exConnect) { 298 } 299 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { 300 } 301 catch (com.sun.star.io.IOException exIO ) { 302 } 303 catch (com.sun.star.uno.RuntimeException exRuntime) { 304 } 305 catch (com.sun.star.uno.Exception exUno ) { 306 } 307 308 } 309 310 //_________________________________ 311 flush()312 public void flush() throws com.sun.star.io.NotConnectedException , 313 com.sun.star.io.BufferSizeExceededException , 314 com.sun.star.io.IOException 315 { 316 m_bOutWasUsed = true; 317 318 if (m_xOutStream == null) 319 { 320 throw new com.sun.star.io.NotConnectedException("stream not open"); 321 } 322 323 try 324 { 325 m_xOutStream.flush(); 326 } 327 catch (com.sun.star.io.NotConnectedException exConnect) { 328 } 329 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { 330 } 331 catch (com.sun.star.io.IOException exIO ) { 332 } 333 catch (com.sun.star.uno.RuntimeException exRuntime) { 334 } 335 catch (com.sun.star.uno.Exception exUno ) { 336 } 337 } 338 339 //_________________________________ 340 closeOutput()341 public void closeOutput() throws com.sun.star.io.NotConnectedException , 342 com.sun.star.io.BufferSizeExceededException, 343 com.sun.star.io.IOException 344 { 345 m_bOutWasUsed = true; 346 347 if (m_xOutStream == null) 348 { 349 throw new com.sun.star.io.NotConnectedException("stream not open"); 350 } 351 352 try 353 { 354 m_xOutStream.closeOutput(); 355 } 356 catch (com.sun.star.io.NotConnectedException exConnect) { 357 } 358 catch (com.sun.star.io.BufferSizeExceededException exBuffer ) { 359 } 360 catch (com.sun.star.io.IOException exIO ) { 361 } 362 catch (com.sun.star.uno.RuntimeException exRuntime) { 363 } 364 catch (com.sun.star.uno.Exception exUno ) { 365 } 366 367 } 368 369 /** 370 * following methods simulates the XSeekable. 371 * The notice all actions inside the internal protocol 372 * and try to map all neccessary functions to the internal 373 * open stream. 374 */ seek(long nLocation )375 public void seek(long nLocation ) 376 throws com.sun.star.lang.IllegalArgumentException, 377 com.sun.star.io.IOException 378 { 379 if (m_xInStream != null) 380 m_bInWasUsed = true; 381 else 382 if (m_xOutStream != null) 383 m_bOutWasUsed = true; 384 // else 385 //m_aProtocol.log("\tno stream open!\n"); 386 387 if (m_xSeek == null) 388 { 389 throw new com.sun.star.io.IOException("stream not seekable"); 390 } 391 392 try 393 { 394 m_xSeek.seek(nLocation); 395 } 396 catch (com.sun.star.lang.IllegalArgumentException exArg ) { 397 } 398 catch (com.sun.star.io.IOException exIO ) { 399 } 400 catch (com.sun.star.uno.RuntimeException exRuntime) { 401 } 402 catch (com.sun.star.uno.Exception exUno ) { 403 } 404 405 } 406 getPosition()407 public long getPosition() throws com.sun.star.io.IOException 408 { 409 410 if (m_xInStream != null) 411 m_bInWasUsed = true; 412 else 413 if (m_xOutStream != null) 414 m_bOutWasUsed = true; 415 // else 416 //m_aProtocol.log("\tno stream open!\n"); 417 418 if (m_xSeek == null) 419 { 420 throw new com.sun.star.io.IOException("stream not seekable"); 421 } 422 423 long nPos = 0; 424 try 425 { 426 nPos = m_xSeek.getPosition(); 427 } 428 catch (com.sun.star.io.IOException exIO ) { 429 } 430 catch (com.sun.star.uno.RuntimeException exRuntime) { 431 } 432 catch (com.sun.star.uno.Exception exUno ) { 433 } 434 435 return nPos; 436 } 437 438 //_________________________________ 439 getLength()440 public long getLength() throws com.sun.star.io.IOException 441 { 442 443 if (m_xInStream != null) 444 m_bInWasUsed = true; 445 else 446 if (m_xOutStream != null) 447 m_bOutWasUsed = true; 448 // else 449 //m_aProtocol.log("\tno stream open!\n"); 450 451 if (m_xSeek == null) 452 { 453 throw new com.sun.star.io.IOException("stream not seekable"); 454 } 455 456 long nLen = 0; 457 try 458 { 459 nLen = m_xSeek.getLength(); 460 } 461 catch (com.sun.star.io.IOException exIO ) { 462 } 463 catch (com.sun.star.uno.RuntimeException exRuntime) { 464 } 465 catch (com.sun.star.uno.Exception exUno ) { 466 } 467 468 return nLen; 469 } 470 } 471