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