1*a740f2aaSAndrew Rist /**************************************************************
2*a740f2aaSAndrew Rist  *
3*a740f2aaSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a740f2aaSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a740f2aaSAndrew Rist  * distributed with this work for additional information
6*a740f2aaSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a740f2aaSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a740f2aaSAndrew Rist  * "License"); you may not use this file except in compliance
9*a740f2aaSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a740f2aaSAndrew Rist  *
11*a740f2aaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a740f2aaSAndrew Rist  *
13*a740f2aaSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a740f2aaSAndrew Rist  * software distributed under the License is distributed on an
15*a740f2aaSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a740f2aaSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a740f2aaSAndrew Rist  * specific language governing permissions and limitations
18*a740f2aaSAndrew Rist  * under the License.
19*a740f2aaSAndrew Rist  *
20*a740f2aaSAndrew Rist  *************************************************************/
21*a740f2aaSAndrew Rist 
22cdf0e10cSrcweir package complex.storages;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.XInterface;
25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30cdf0e10cSrcweir import com.sun.star.uno.XInterface;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.io.XStream;
33cdf0e10cSrcweir import com.sun.star.io.XInputStream;
34cdf0e10cSrcweir import com.sun.star.io.XOutputStream;
35cdf0e10cSrcweir import com.sun.star.io.XTruncate;
36cdf0e10cSrcweir import com.sun.star.io.XSeekable;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 
39cdf0e10cSrcweir public class BorderedStream
40cdf0e10cSrcweir 	implements XStream, XInputStream, XOutputStream, XTruncate, XSeekable
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 	int m_nMaxSize;
43cdf0e10cSrcweir 	int m_nCurSize;
44cdf0e10cSrcweir 	int m_nCurPos;
45cdf0e10cSrcweir 	byte m_pBytes[];
46cdf0e10cSrcweir 
BorderedStream( int nMaxSize )47cdf0e10cSrcweir 	public BorderedStream( int nMaxSize )
48cdf0e10cSrcweir 	{
49cdf0e10cSrcweir 		m_nMaxSize = nMaxSize;
50cdf0e10cSrcweir 		m_nCurSize = 0;
51cdf0e10cSrcweir 		m_nCurPos = 0;
52cdf0e10cSrcweir 		m_pBytes = new byte[m_nMaxSize];
53cdf0e10cSrcweir 	}
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 	//==============
56cdf0e10cSrcweir 	// XStream
57cdf0e10cSrcweir 	//==============
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	// ----------------------------------------------------------
getInputStream()60cdf0e10cSrcweir 	public synchronized XInputStream getInputStream()
61cdf0e10cSrcweir 		throws com.sun.star.uno.RuntimeException
62cdf0e10cSrcweir 	{
63cdf0e10cSrcweir 		return (XInputStream)UnoRuntime.queryInterface( XInputStream.class, this );
64cdf0e10cSrcweir 	}
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	// ----------------------------------------------------------
getOutputStream()67cdf0e10cSrcweir 	public synchronized XOutputStream getOutputStream()
68cdf0e10cSrcweir 		throws com.sun.star.uno.RuntimeException
69cdf0e10cSrcweir 	{
70cdf0e10cSrcweir 		return (XOutputStream)UnoRuntime.queryInterface( XOutputStream.class, this );
71cdf0e10cSrcweir 	}
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 	//==============
74cdf0e10cSrcweir 	// XInputStream
75cdf0e10cSrcweir 	//==============
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 	// ----------------------------------------------------------
readBytes( byte[][] aData, int nBytesToRead )78cdf0e10cSrcweir 	public synchronized int readBytes( byte[][] aData, int nBytesToRead )
79cdf0e10cSrcweir     	throws  com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
80cdf0e10cSrcweir 	{
81cdf0e10cSrcweir 		int nRead = 0;
82cdf0e10cSrcweir 		if ( m_pBytes != null && nBytesToRead > 0 )
83cdf0e10cSrcweir 		{
84cdf0e10cSrcweir 			int nAvailable = m_nCurSize - m_nCurPos;
85cdf0e10cSrcweir 			if ( nBytesToRead > nAvailable )
86cdf0e10cSrcweir 				nBytesToRead = nAvailable;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 			aData[0] = new byte[nBytesToRead];
89cdf0e10cSrcweir 			for ( int nInd = 0; nInd < nBytesToRead; nInd++ )
90cdf0e10cSrcweir 				aData[0][nInd] = m_pBytes[m_nCurPos+nInd];
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 			nRead = nBytesToRead;
93cdf0e10cSrcweir 			m_nCurPos += nRead;
94cdf0e10cSrcweir 		}
95cdf0e10cSrcweir 		else
96cdf0e10cSrcweir 		{
97cdf0e10cSrcweir 			aData[0] = new byte[0];
98cdf0e10cSrcweir 		}
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 		return nRead;
101cdf0e10cSrcweir 	}
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     // ----------------------------------------------------------
readSomeBytes( byte[][] aData, int nMaxBytesToRead )104cdf0e10cSrcweir 	public synchronized int readSomeBytes( byte[][] aData, int nMaxBytesToRead )
105cdf0e10cSrcweir 		throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
106cdf0e10cSrcweir 	{
107cdf0e10cSrcweir 		return readBytes( aData, nMaxBytesToRead );
108cdf0e10cSrcweir 	}
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     // ----------------------------------------------------------
skipBytes( int nBytesToSkip )111cdf0e10cSrcweir 	public synchronized void skipBytes( int nBytesToSkip  )
112cdf0e10cSrcweir 		throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
113cdf0e10cSrcweir 	{
114cdf0e10cSrcweir 		if ( nBytesToSkip < 0 )
115cdf0e10cSrcweir 			throw new com.sun.star.io.IOException(); // illegal argument
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 		if ( m_nCurSize - m_nCurPos > nBytesToSkip )
118cdf0e10cSrcweir 			m_nCurPos += nBytesToSkip;
119cdf0e10cSrcweir 		else
120cdf0e10cSrcweir 			m_nCurPos = m_nCurSize;
121cdf0e10cSrcweir 	}
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     // ----------------------------------------------------------
available()124cdf0e10cSrcweir 	public synchronized int available()
125cdf0e10cSrcweir 		throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
126cdf0e10cSrcweir 	{
127cdf0e10cSrcweir 		return 0;
128cdf0e10cSrcweir 	}
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     // ----------------------------------------------------------
closeInput()131cdf0e10cSrcweir 	public synchronized void closeInput()
132cdf0e10cSrcweir 		throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
133cdf0e10cSrcweir 	{
134cdf0e10cSrcweir 		// no need to do anything
135cdf0e10cSrcweir 	}
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	//==============
139cdf0e10cSrcweir 	// XOutputStream
140cdf0e10cSrcweir 	//==============
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     // ----------------------------------------------------------
writeBytes( byte[] aData )143cdf0e10cSrcweir 	public synchronized void writeBytes( byte[] aData  )
144cdf0e10cSrcweir 		throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
145cdf0e10cSrcweir 	{
146cdf0e10cSrcweir 		if ( m_pBytes != null && aData.length > 0 )
147cdf0e10cSrcweir 		{
148cdf0e10cSrcweir 			if ( aData.length > m_nMaxSize - m_nCurPos )
149cdf0e10cSrcweir 				throw new com.sun.star.io.IOException();
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 			for ( int nInd = 0; nInd < aData.length; nInd++ )
152cdf0e10cSrcweir 				m_pBytes[m_nCurPos+nInd] = aData[nInd];
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 			m_nCurPos += aData.length;
155cdf0e10cSrcweir 			if ( m_nCurPos > m_nCurSize )
156cdf0e10cSrcweir 				m_nCurSize = m_nCurPos;
157cdf0e10cSrcweir 		}
158cdf0e10cSrcweir 	}
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     // ----------------------------------------------------------
flush()161cdf0e10cSrcweir 	public synchronized void flush()
162cdf0e10cSrcweir 		throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
163cdf0e10cSrcweir 	{
164cdf0e10cSrcweir 		// nothing to do
165cdf0e10cSrcweir 	}
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     // ----------------------------------------------------------
closeOutput()168cdf0e10cSrcweir 	public synchronized void closeOutput()
169cdf0e10cSrcweir 		throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
170cdf0e10cSrcweir 	{
171cdf0e10cSrcweir 		// nothing to do
172cdf0e10cSrcweir 	}
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	//==============
176cdf0e10cSrcweir 	// XTruncate
177cdf0e10cSrcweir 	//==============
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     // ----------------------------------------------------------
truncate()180cdf0e10cSrcweir 	public synchronized void truncate()
181cdf0e10cSrcweir 		throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		m_nCurSize = 0;
184cdf0e10cSrcweir 		m_nCurPos = 0;
185cdf0e10cSrcweir 	}
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	//==============
189cdf0e10cSrcweir 	// XSeekable
190cdf0e10cSrcweir 	//==============
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     // ----------------------------------------------------------
seek( long location )193cdf0e10cSrcweir 	public synchronized void seek( long location )
194cdf0e10cSrcweir 		throws com.sun.star.lang.IllegalArgumentException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
195cdf0e10cSrcweir 	{
196cdf0e10cSrcweir 		if ( location > (long)m_nCurSize )
197cdf0e10cSrcweir 			throw new com.sun.star.lang.IllegalArgumentException();
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 		m_nCurPos = (int)location;
200cdf0e10cSrcweir 	}
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     // ----------------------------------------------------------
getPosition()203cdf0e10cSrcweir 	public synchronized long getPosition()
204cdf0e10cSrcweir 		throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
205cdf0e10cSrcweir 	{
206cdf0e10cSrcweir 		return (long)m_nCurPos;
207cdf0e10cSrcweir 	}
208cdf0e10cSrcweir 
209cdf0e10cSrcweir     // ----------------------------------------------------------
getLength()210cdf0e10cSrcweir 	public synchronized long getLength()
211cdf0e10cSrcweir 		throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
212cdf0e10cSrcweir 	{
213cdf0e10cSrcweir 		return (long)m_nCurSize;
214cdf0e10cSrcweir 	}
215cdf0e10cSrcweir };
216cdf0e10cSrcweir 
217