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