xref: /trunk/main/package/qa/storages/BorderedStream.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 package complex.storages;
2 
3 import com.sun.star.uno.XInterface;
4 import com.sun.star.lang.XMultiServiceFactory;
5 import com.sun.star.lang.XSingleServiceFactory;
6 
7 import com.sun.star.bridge.XUnoUrlResolver;
8 import com.sun.star.uno.UnoRuntime;
9 import com.sun.star.uno.XInterface;
10 
11 import com.sun.star.io.XStream;
12 import com.sun.star.io.XInputStream;
13 import com.sun.star.io.XOutputStream;
14 import com.sun.star.io.XTruncate;
15 import com.sun.star.io.XSeekable;
16 
17 
18 public class BorderedStream
19     implements XStream, XInputStream, XOutputStream, XTruncate, XSeekable
20 {
21     int m_nMaxSize;
22     int m_nCurSize;
23     int m_nCurPos;
24     byte m_pBytes[];
25 
26     public BorderedStream( int nMaxSize )
27     {
28         m_nMaxSize = nMaxSize;
29         m_nCurSize = 0;
30         m_nCurPos = 0;
31         m_pBytes = new byte[m_nMaxSize];
32     }
33 
34     //==============
35     // XStream
36     //==============
37 
38     // ----------------------------------------------------------
39     public synchronized XInputStream getInputStream()
40         throws com.sun.star.uno.RuntimeException
41     {
42         return (XInputStream)UnoRuntime.queryInterface( XInputStream.class, this );
43     }
44 
45     // ----------------------------------------------------------
46     public synchronized XOutputStream getOutputStream()
47         throws com.sun.star.uno.RuntimeException
48     {
49         return (XOutputStream)UnoRuntime.queryInterface( XOutputStream.class, this );
50     }
51 
52     //==============
53     // XInputStream
54     //==============
55 
56     // ----------------------------------------------------------
57     public synchronized int readBytes( byte[][] aData, int nBytesToRead )
58         throws  com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
59     {
60         int nRead = 0;
61         if ( m_pBytes != null && nBytesToRead > 0 )
62         {
63             int nAvailable = m_nCurSize - m_nCurPos;
64             if ( nBytesToRead > nAvailable )
65                 nBytesToRead = nAvailable;
66 
67             aData[0] = new byte[nBytesToRead];
68             for ( int nInd = 0; nInd < nBytesToRead; nInd++ )
69                 aData[0][nInd] = m_pBytes[m_nCurPos+nInd];
70 
71             nRead = nBytesToRead;
72             m_nCurPos += nRead;
73         }
74         else
75         {
76             aData[0] = new byte[0];
77         }
78 
79         return nRead;
80     }
81 
82     // ----------------------------------------------------------
83     public synchronized int readSomeBytes( byte[][] aData, int nMaxBytesToRead )
84         throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
85     {
86         return readBytes( aData, nMaxBytesToRead );
87     }
88 
89     // ----------------------------------------------------------
90     public synchronized void skipBytes( int nBytesToSkip  )
91         throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
92     {
93         if ( nBytesToSkip < 0 )
94             throw new com.sun.star.io.IOException(); // illegal argument
95 
96         if ( m_nCurSize - m_nCurPos > nBytesToSkip )
97             m_nCurPos += nBytesToSkip;
98         else
99             m_nCurPos = m_nCurSize;
100     }
101 
102     // ----------------------------------------------------------
103     public synchronized int available()
104         throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
105     {
106         return 0;
107     }
108 
109     // ----------------------------------------------------------
110     public synchronized void closeInput()
111         throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
112     {
113         // no need to do anything
114     }
115 
116 
117     //==============
118     // XOutputStream
119     //==============
120 
121     // ----------------------------------------------------------
122     public synchronized void writeBytes( byte[] aData  )
123         throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
124     {
125         if ( m_pBytes != null && aData.length > 0 )
126         {
127             if ( aData.length > m_nMaxSize - m_nCurPos )
128                 throw new com.sun.star.io.IOException();
129 
130             for ( int nInd = 0; nInd < aData.length; nInd++ )
131                 m_pBytes[m_nCurPos+nInd] = aData[nInd];
132 
133             m_nCurPos += aData.length;
134             if ( m_nCurPos > m_nCurSize )
135                 m_nCurSize = m_nCurPos;
136         }
137     }
138 
139     // ----------------------------------------------------------
140     public synchronized void flush()
141         throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
142     {
143         // nothing to do
144     }
145 
146     // ----------------------------------------------------------
147     public synchronized void closeOutput()
148         throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
149     {
150         // nothing to do
151     }
152 
153 
154     //==============
155     // XTruncate
156     //==============
157 
158     // ----------------------------------------------------------
159     public synchronized void truncate()
160         throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
161     {
162         m_nCurSize = 0;
163         m_nCurPos = 0;
164     }
165 
166 
167     //==============
168     // XSeekable
169     //==============
170 
171     // ----------------------------------------------------------
172     public synchronized void seek( long location )
173         throws com.sun.star.lang.IllegalArgumentException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
174     {
175         if ( location > (long)m_nCurSize )
176             throw new com.sun.star.lang.IllegalArgumentException();
177 
178         m_nCurPos = (int)location;
179     }
180 
181     // ----------------------------------------------------------
182     public synchronized long getPosition()
183         throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
184     {
185         return (long)m_nCurPos;
186     }
187 
188     // ----------------------------------------------------------
189     public synchronized long getLength()
190         throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
191     {
192         return (long)m_nCurSize;
193     }
194 };
195 
196