Update ooo320-m1
[ooovba.git] / package / qa / storages / BorderedStream.java
blob41d5bd9de40fa2d5c07389b9e9ff8b597e6cd611
1 package complex.storages;
3 import com.sun.star.uno.XInterface;
4 import com.sun.star.lang.XMultiServiceFactory;
5 import com.sun.star.lang.XSingleServiceFactory;
7 import com.sun.star.bridge.XUnoUrlResolver;
8 import com.sun.star.uno.UnoRuntime;
9 import com.sun.star.uno.XInterface;
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;
18 public class BorderedStream
19 implements XStream, XInputStream, XOutputStream, XTruncate, XSeekable
21 int m_nMaxSize;
22 int m_nCurSize;
23 int m_nCurPos;
24 byte m_pBytes[];
26 public BorderedStream( int nMaxSize )
28 m_nMaxSize = nMaxSize;
29 m_nCurSize = 0;
30 m_nCurPos = 0;
31 m_pBytes = new byte[m_nMaxSize];
34 //==============
35 // XStream
36 //==============
38 // ----------------------------------------------------------
39 public synchronized XInputStream getInputStream()
40 throws com.sun.star.uno.RuntimeException
42 return (XInputStream)UnoRuntime.queryInterface( XInputStream.class, this );
45 // ----------------------------------------------------------
46 public synchronized XOutputStream getOutputStream()
47 throws com.sun.star.uno.RuntimeException
49 return (XOutputStream)UnoRuntime.queryInterface( XOutputStream.class, this );
52 //==============
53 // XInputStream
54 //==============
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
60 int nRead = 0;
61 if ( m_pBytes != null && nBytesToRead > 0 )
63 int nAvailable = m_nCurSize - m_nCurPos;
64 if ( nBytesToRead > nAvailable )
65 nBytesToRead = nAvailable;
67 aData[0] = new byte[nBytesToRead];
68 for ( int nInd = 0; nInd < nBytesToRead; nInd++ )
69 aData[0][nInd] = m_pBytes[m_nCurPos+nInd];
71 nRead = nBytesToRead;
72 m_nCurPos += nRead;
74 else
76 aData[0] = new byte[0];
79 return nRead;
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
86 return readBytes( aData, nMaxBytesToRead );
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
93 if ( nBytesToSkip < 0 )
94 throw new com.sun.star.io.IOException(); // illegal argument
96 if ( m_nCurSize - m_nCurPos > nBytesToSkip )
97 m_nCurPos += nBytesToSkip;
98 else
99 m_nCurPos = m_nCurSize;
102 // ----------------------------------------------------------
103 public synchronized int available()
104 throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
106 return 0;
109 // ----------------------------------------------------------
110 public synchronized void closeInput()
111 throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
113 // no need to do anything
117 //==============
118 // XOutputStream
119 //==============
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
125 if ( m_pBytes != null && aData.length > 0 )
127 if ( aData.length > m_nMaxSize - m_nCurPos )
128 throw new com.sun.star.io.IOException();
130 for ( int nInd = 0; nInd < aData.length; nInd++ )
131 m_pBytes[m_nCurPos+nInd] = aData[nInd];
133 m_nCurPos += aData.length;
134 if ( m_nCurPos > m_nCurSize )
135 m_nCurSize = m_nCurPos;
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
143 // nothing to do
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
150 // nothing to do
154 //==============
155 // XTruncate
156 //==============
158 // ----------------------------------------------------------
159 public synchronized void truncate()
160 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
162 m_nCurSize = 0;
163 m_nCurPos = 0;
167 //==============
168 // XSeekable
169 //==============
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
175 if ( location > (long)m_nCurSize )
176 throw new com.sun.star.lang.IllegalArgumentException();
178 m_nCurPos = (int)location;
181 // ----------------------------------------------------------
182 public synchronized long getPosition()
183 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
185 return (long)m_nCurPos;
188 // ----------------------------------------------------------
189 public synchronized long getLength()
190 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
192 return (long)m_nCurSize;