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