2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package complex
.loadAllDocuments
;
20 import com
.sun
.star
.uno
.UnoRuntime
;
21 import com
.sun
.star
.ucb
.XSimpleFileAccess
;
22 import com
.sun
.star
.lang
.XMultiServiceFactory
;
25 * Simulates an input and output stream and
26 * implements the interfaces XInputStream, XOutputStream.
27 * So it can be used for testing loading/saving of documents
28 * using streams instead of URLs.
30 public class StreamSimulator
implements com
.sun
.star
.io
.XInputStream
,
31 com
.sun
.star
.io
.XOutputStream
,
32 com
.sun
.star
.io
.XSeekable
34 //_________________________________
36 * @member m_sFileName name of the corrsponding file on disk
37 * @member m_xInStream the internal input stream for reading
38 * @member m_xOutStream the internal input stream for writing
39 * @member m_xSeek points at runtime to m_xInStream or m_xOutStream and make it seekable
41 * @member m_bInWasUsed indicates, that the input stream interface was used
42 * @member m_bOutWasUsed indicates, that the output stream interface was used
45 private String m_sFileName
;
46 private com
.sun
.star
.io
.XInputStream m_xInStream
;
47 private com
.sun
.star
.io
.XOutputStream m_xOutStream
;
48 private com
.sun
.star
.io
.XSeekable m_xSeek
;
50 public boolean m_bInWasUsed
;
51 public boolean m_bOutWasUsed
;
54 * construct a new instance of this class
55 * It set the name of the correspojnding file on disk, which
56 * should be source or target for the following operations on
57 * this object. And it regulate if it should function as
58 * input or output stream.
61 * name of the file on disk
62 * Will be used as source (if param bInput==true)
63 * or as target (if param bInput==false).
66 * it specify, which interface should work at this object.
67 * <TRUE/> => we simulate an input stream
68 * <FALSE/> => we simulate an output stream
70 * @throw com.sun.star.io.NotConnectedException
71 * in case the internal streams to the file on disk couldn't
73 * They are neccessary. Otherwhise this simulator can't
76 public StreamSimulator(XMultiServiceFactory xMSF
,
77 String sFileName
, boolean bInput
)
78 throws com
.sun
.star
.io
.NotConnectedException
80 m_sFileName
= sFileName
;
81 m_bInWasUsed
= false ;
82 m_bOutWasUsed
= false ;
86 XSimpleFileAccess xHelper
= UnoRuntime
.queryInterface(XSimpleFileAccess
.class,
87 xMSF
.createInstance("com.sun.star.ucb.SimpleFileAccess"));
90 throw new com
.sun
.star
.io
.NotConnectedException(
91 "ucb helper not available. Can't create streams.");
95 m_xInStream
= xHelper
.openFileRead(m_sFileName
);
96 m_xSeek
= UnoRuntime
.queryInterface(
97 com
.sun
.star
.io
.XSeekable
.class,
102 m_xOutStream
= xHelper
.openFileWrite(m_sFileName
);
103 m_xSeek
= UnoRuntime
.queryInterface(
104 com
.sun
.star
.io
.XSeekable
.class,
108 catch(com
.sun
.star
.uno
.Exception exUno
)
110 throw new com
.sun
.star
.io
.NotConnectedException(
111 "Could not open the file.");
116 * following methods simulates the XInputStream.
117 * The notice all actions inside the internal protocol
118 * and try to map all neccessary functions to the internal
121 public int readBytes(byte[][] lData
, int nBytesToRead
)
122 throws com
.sun
.star
.io
.NotConnectedException
,
123 com
.sun
.star
.io
.BufferSizeExceededException
,
124 com
.sun
.star
.io
.IOException
128 if (m_xInStream
== null)
130 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
136 nRead
= m_xInStream
.readBytes(lData
,nBytesToRead
);
138 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) {
140 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) {
142 catch (com
.sun
.star
.io
.IOException exIO
) {
144 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) {
151 public int readSomeBytes(byte[][] lData
, int nMaxBytesToRead
)
152 throws com
.sun
.star
.io
.NotConnectedException
,
153 com
.sun
.star
.io
.BufferSizeExceededException
,
154 com
.sun
.star
.io
.IOException
158 if (m_xInStream
== null)
160 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
166 nRead
= m_xInStream
.readSomeBytes(lData
,nMaxBytesToRead
);
168 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) {
170 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) {
172 catch (com
.sun
.star
.io
.IOException exIO
) {
174 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) {
180 //_________________________________
182 public void skipBytes(int nBytesToSkip
)
183 throws com
.sun
.star
.io
.NotConnectedException
,
184 com
.sun
.star
.io
.BufferSizeExceededException
,
185 com
.sun
.star
.io
.IOException
189 if (m_xInStream
== null)
191 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
196 m_xInStream
.skipBytes(nBytesToSkip
);
198 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) {
200 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) {
202 catch (com
.sun
.star
.io
.IOException exIO
) {
204 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) {
208 public int available() throws com
.sun
.star
.io
.NotConnectedException
,
209 com
.sun
.star
.io
.IOException
213 if (m_xInStream
== null)
215 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
221 nAvailable
= m_xInStream
.available();
223 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) {
225 catch (com
.sun
.star
.io
.IOException exIO
) {
227 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) {
233 //_________________________________
235 public void closeInput() throws com
.sun
.star
.io
.NotConnectedException
,
236 com
.sun
.star
.io
.IOException
240 if (m_xInStream
== null)
242 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
247 m_xInStream
.closeInput();
249 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) {
251 catch (com
.sun
.star
.io
.IOException exIO
) {
253 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) {
258 * following methods simulates the XOutputStream.
259 * The notice all actions inside the internal protocol
260 * and try to map all neccessary functions to the internal
263 public void writeBytes(byte[] lData
)
264 throws com
.sun
.star
.io
.NotConnectedException
,
265 com
.sun
.star
.io
.BufferSizeExceededException
,
266 com
.sun
.star
.io
.IOException
268 m_bOutWasUsed
= true;
270 if (m_xOutStream
== null)
272 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
277 m_xOutStream
.writeBytes(lData
);
279 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) {
281 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) {
283 catch (com
.sun
.star
.io
.IOException exIO
) {
285 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) {
290 //_________________________________
292 public void flush() throws com
.sun
.star
.io
.NotConnectedException
,
293 com
.sun
.star
.io
.BufferSizeExceededException
,
294 com
.sun
.star
.io
.IOException
296 m_bOutWasUsed
= true;
298 if (m_xOutStream
== null)
300 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
305 m_xOutStream
.flush();
307 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) {
309 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) {
311 catch (com
.sun
.star
.io
.IOException exIO
) {
313 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) {
317 //_________________________________
319 public void closeOutput() throws com
.sun
.star
.io
.NotConnectedException
,
320 com
.sun
.star
.io
.BufferSizeExceededException
,
321 com
.sun
.star
.io
.IOException
323 m_bOutWasUsed
= true;
325 if (m_xOutStream
== null)
327 throw new com
.sun
.star
.io
.NotConnectedException("stream not open");
332 m_xOutStream
.closeOutput();
334 catch (com
.sun
.star
.io
.NotConnectedException exConnect
) {
336 catch (com
.sun
.star
.io
.BufferSizeExceededException exBuffer
) {
338 catch (com
.sun
.star
.io
.IOException exIO
) {
340 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) {
345 * following methods simulates the XSeekable.
346 * The notice all actions inside the internal protocol
347 * and try to map all neccessary functions to the internal
350 public void seek(long nLocation
)
351 throws com
.sun
.star
.lang
.IllegalArgumentException
,
352 com
.sun
.star
.io
.IOException
354 if (m_xInStream
!= null)
357 if (m_xOutStream
!= null)
358 m_bOutWasUsed
= true;
362 throw new com
.sun
.star
.io
.IOException("stream not seekable");
367 m_xSeek
.seek(nLocation
);
369 catch (com
.sun
.star
.lang
.IllegalArgumentException exArg
) {
371 catch (com
.sun
.star
.io
.IOException exIO
) {
373 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) {
377 public long getPosition() throws com
.sun
.star
.io
.IOException
380 if (m_xInStream
!= null)
383 if (m_xOutStream
!= null)
384 m_bOutWasUsed
= true;
388 throw new com
.sun
.star
.io
.IOException("stream not seekable");
394 nPos
= m_xSeek
.getPosition();
396 catch (com
.sun
.star
.io
.IOException exIO
) {
398 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) {
404 //_________________________________
406 public long getLength() throws com
.sun
.star
.io
.IOException
409 if (m_xInStream
!= null)
412 if (m_xOutStream
!= null)
413 m_bOutWasUsed
= true;
417 throw new com
.sun
.star
.io
.IOException("stream not seekable");
423 nLen
= m_xSeek
.getLength();
425 catch (com
.sun
.star
.io
.IOException exIO
) {
427 catch (com
.sun
.star
.uno
.RuntimeException exRuntime
) {