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