2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package complex
.storages
;
21 import com
.sun
.star
.uno
.XInterface
;
22 import com
.sun
.star
.lang
.XMultiServiceFactory
;
23 import com
.sun
.star
.lang
.XSingleServiceFactory
;
25 import com
.sun
.star
.bridge
.XUnoUrlResolver
;
26 import com
.sun
.star
.uno
.UnoRuntime
;
27 import com
.sun
.star
.uno
.XInterface
;
29 import com
.sun
.star
.io
.XStream
;
30 import com
.sun
.star
.io
.XInputStream
;
31 import com
.sun
.star
.io
.XOutputStream
;
32 import com
.sun
.star
.io
.XTruncate
;
33 import com
.sun
.star
.io
.XSeekable
;
36 public class BorderedStream
37 implements XStream
, XInputStream
, XOutputStream
, XTruncate
, XSeekable
44 public BorderedStream( int nMaxSize
)
46 m_nMaxSize
= nMaxSize
;
49 m_pBytes
= new byte[m_nMaxSize
];
57 public synchronized XInputStream
getInputStream()
58 throws com
.sun
.star
.uno
.RuntimeException
60 return (XInputStream
)UnoRuntime
.queryInterface( XInputStream
.class, this );
64 public synchronized XOutputStream
getOutputStream()
65 throws com
.sun
.star
.uno
.RuntimeException
67 return (XOutputStream
)UnoRuntime
.queryInterface( XOutputStream
.class, this );
75 public synchronized int readBytes( byte[][] aData
, int nBytesToRead
)
76 throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.BufferSizeExceededException
, com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
79 if ( m_pBytes
!= null && nBytesToRead
> 0 )
81 int nAvailable
= m_nCurSize
- m_nCurPos
;
82 if ( nBytesToRead
> nAvailable
)
83 nBytesToRead
= nAvailable
;
85 aData
[0] = new byte[nBytesToRead
];
86 for ( int nInd
= 0; nInd
< nBytesToRead
; nInd
++ )
87 aData
[0][nInd
] = m_pBytes
[m_nCurPos
+nInd
];
94 aData
[0] = new byte[0];
101 public synchronized int readSomeBytes( byte[][] aData
, int nMaxBytesToRead
)
102 throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.BufferSizeExceededException
, com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
104 return readBytes( aData
, nMaxBytesToRead
);
108 public synchronized void skipBytes( int nBytesToSkip
)
109 throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.BufferSizeExceededException
, com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
111 if ( nBytesToSkip
< 0 )
112 throw new com
.sun
.star
.io
.IOException(); // illegal argument
114 if ( m_nCurSize
- m_nCurPos
> nBytesToSkip
)
115 m_nCurPos
+= nBytesToSkip
;
117 m_nCurPos
= m_nCurSize
;
121 public synchronized int available()
122 throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
128 public synchronized void closeInput()
129 throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
131 // no need to do anything
140 public synchronized void writeBytes( byte[] aData
)
141 throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.BufferSizeExceededException
, com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
143 if ( m_pBytes
!= null && aData
.length
> 0 )
145 if ( aData
.length
> m_nMaxSize
- m_nCurPos
)
146 throw new com
.sun
.star
.io
.IOException();
148 for ( int nInd
= 0; nInd
< aData
.length
; nInd
++ )
149 m_pBytes
[m_nCurPos
+nInd
] = aData
[nInd
];
151 m_nCurPos
+= aData
.length
;
152 if ( m_nCurPos
> m_nCurSize
)
153 m_nCurSize
= m_nCurPos
;
158 public synchronized void flush()
159 throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.BufferSizeExceededException
, com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
165 public synchronized void closeOutput()
166 throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.BufferSizeExceededException
, com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
177 public synchronized void truncate()
178 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
190 public synchronized void seek( long location
)
191 throws com
.sun
.star
.lang
.IllegalArgumentException
, com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
193 if ( location
> (long)m_nCurSize
)
194 throw new com
.sun
.star
.lang
.IllegalArgumentException();
196 m_nCurPos
= (int)location
;
200 public synchronized long getPosition()
201 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
203 return (long)m_nCurPos
;
207 public synchronized long getLength()
208 throws com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
210 return (long)m_nCurSize
;