1*34dd1e25SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*34dd1e25SAndrew Rist * distributed with this work for additional information 6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an 15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 17*34dd1e25SAndrew Rist * specific language governing permissions and limitations 18*34dd1e25SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // imports 25cdf0e10cSrcweir import com.sun.star.io.BufferSizeExceededException; 26cdf0e10cSrcweir import com.sun.star.io.NotConnectedException; 27cdf0e10cSrcweir import com.sun.star.io.XInputStream; 28cdf0e10cSrcweir import com.sun.star.io.XSeekable; 29cdf0e10cSrcweir 30cdf0e10cSrcweir /** 31cdf0e10cSrcweir * XInputStream interface implementation. 32cdf0e10cSrcweir */ 33cdf0e10cSrcweir public class MyInputStream implements XSeekable, XInputStream { 34cdf0e10cSrcweir 35cdf0e10cSrcweir /** 36cdf0e10cSrcweir * Member properties 37cdf0e10cSrcweir */ 38cdf0e10cSrcweir private int offset = 0; 39cdf0e10cSrcweir private int read = offset; 40cdf0e10cSrcweir private byte[] bigbuffer; 41cdf0e10cSrcweir 42cdf0e10cSrcweir /** 43cdf0e10cSrcweir * Constructor 44cdf0e10cSrcweir */ MyInputStream()45cdf0e10cSrcweir public MyInputStream() { 46cdf0e10cSrcweir } 47cdf0e10cSrcweir 48cdf0e10cSrcweir // XSeekable. Makes it possible to seek to a certain position within a stream. 49cdf0e10cSrcweir 50cdf0e10cSrcweir /** 51cdf0e10cSrcweir * Returns the length of the stream. 52cdf0e10cSrcweir * 53cdf0e10cSrcweir *@return long The length of the storage medium on which the stream works. 54cdf0e10cSrcweir */ getLength()55cdf0e10cSrcweir public synchronized long getLength() 56cdf0e10cSrcweir throws com.sun.star.io.IOException,com.sun.star.uno.RuntimeException { 57cdf0e10cSrcweir if ( bigbuffer != null ) { 58cdf0e10cSrcweir return bigbuffer.length - offset; 59cdf0e10cSrcweir } else { 60cdf0e10cSrcweir return 0; 61cdf0e10cSrcweir } 62cdf0e10cSrcweir } 63cdf0e10cSrcweir 64cdf0e10cSrcweir /** 65cdf0e10cSrcweir * Returns the current offset of the stream. 66cdf0e10cSrcweir * 67cdf0e10cSrcweir *@return long The current offset in this stream. 68cdf0e10cSrcweir */ getPosition()69cdf0e10cSrcweir public synchronized long getPosition() 70cdf0e10cSrcweir throws com.sun.star.io.IOException,com.sun.star.uno.RuntimeException { 71cdf0e10cSrcweir return read - offset ; 72cdf0e10cSrcweir } 73cdf0e10cSrcweir 74cdf0e10cSrcweir /** 75cdf0e10cSrcweir * Changes the seek pointer to a new location relative to the beginning of the stream. 76cdf0e10cSrcweir * 77cdf0e10cSrcweir *@param long 78cdf0e10cSrcweir */ seek(long p0)79cdf0e10cSrcweir public synchronized void seek(long p0) 80cdf0e10cSrcweir throws IllegalArgumentException, com.sun.star.io.IOException, 81cdf0e10cSrcweir com.sun.star.uno.RuntimeException { 82cdf0e10cSrcweir if( bigbuffer != null ) { 83cdf0e10cSrcweir p0 +=offset; 84cdf0e10cSrcweir read = ( int ) p0; 85cdf0e10cSrcweir if( read < offset || read > bigbuffer.length ) 86cdf0e10cSrcweir throw new IllegalArgumentException(); 87cdf0e10cSrcweir } 88cdf0e10cSrcweir } 89cdf0e10cSrcweir 90cdf0e10cSrcweir // XInputStream. This is the basic interface to read data from a stream. 91cdf0e10cSrcweir 92cdf0e10cSrcweir /** 93cdf0e10cSrcweir * States how many bytes can be read or skipped without blocking. 94cdf0e10cSrcweir * 95cdf0e10cSrcweir *@return int If not available, then returned 0 96cdf0e10cSrcweir */ available()97cdf0e10cSrcweir public synchronized int available() 98cdf0e10cSrcweir throws NotConnectedException, com.sun.star.io.IOException, 99cdf0e10cSrcweir com.sun.star.uno.RuntimeException { 100cdf0e10cSrcweir if( bigbuffer != null ) 101cdf0e10cSrcweir return ( bigbuffer.length - read ); 102cdf0e10cSrcweir else 103cdf0e10cSrcweir return 0; 104cdf0e10cSrcweir } 105cdf0e10cSrcweir 106cdf0e10cSrcweir /** 107cdf0e10cSrcweir * Closes the stream. . 108cdf0e10cSrcweir */ closeInput()109cdf0e10cSrcweir public void closeInput() 110cdf0e10cSrcweir throws NotConnectedException,com.sun.star.io.IOException, 111cdf0e10cSrcweir com.sun.star.uno.RuntimeException { 112cdf0e10cSrcweir read = -1; 113cdf0e10cSrcweir } 114cdf0e10cSrcweir 115cdf0e10cSrcweir /** 116cdf0e10cSrcweir * Reads the specified number of bytes in the given sequence. 117cdf0e10cSrcweir * 118cdf0e10cSrcweir *@param byte[][] 119cdf0e10cSrcweir *@param int 120cdf0e10cSrcweir *@return int 121cdf0e10cSrcweir */ readBytes(byte[][] p0, int p1)122cdf0e10cSrcweir public synchronized int readBytes(byte[][] p0, int p1) 123cdf0e10cSrcweir throws NotConnectedException, BufferSizeExceededException, 124cdf0e10cSrcweir com.sun.star.io.IOException, com.sun.star.uno.RuntimeException { 125cdf0e10cSrcweir if( bigbuffer != null ) { 126cdf0e10cSrcweir if( read == -1 ) 127cdf0e10cSrcweir return 0; 128cdf0e10cSrcweir int i = 0; 129cdf0e10cSrcweir int available; 130cdf0e10cSrcweir if ( p1 > bigbuffer.length - read ) 131cdf0e10cSrcweir available = bigbuffer.length - read; 132cdf0e10cSrcweir else 133cdf0e10cSrcweir available = p1; 134cdf0e10cSrcweir 135cdf0e10cSrcweir p0[0] = new byte[p1]; 136cdf0e10cSrcweir while( available != 0 ) { 137cdf0e10cSrcweir p0[0][i++] = bigbuffer[read++]; 138cdf0e10cSrcweir --available; 139cdf0e10cSrcweir } 140cdf0e10cSrcweir return i; 141cdf0e10cSrcweir } else { 142cdf0e10cSrcweir p0[0] = new byte[0]; 143cdf0e10cSrcweir return 0; 144cdf0e10cSrcweir } 145cdf0e10cSrcweir } 146cdf0e10cSrcweir 147cdf0e10cSrcweir /** 148cdf0e10cSrcweir * Reads the available number of bytes at maximum nMaxBytesToRead . 149cdf0e10cSrcweir * This method blocks the thread until at least one byte is available. 150cdf0e10cSrcweir * 151cdf0e10cSrcweir *@param byte[][] 152cdf0e10cSrcweir *@param int 153cdf0e10cSrcweir *@return int 154cdf0e10cSrcweir */ readSomeBytes(byte[][] p0, int p1)155cdf0e10cSrcweir public synchronized int readSomeBytes(byte[][] p0, int p1) 156cdf0e10cSrcweir throws NotConnectedException, 157cdf0e10cSrcweir BufferSizeExceededException, 158cdf0e10cSrcweir com.sun.star.io.IOException, 159cdf0e10cSrcweir com.sun.star.uno.RuntimeException { 160cdf0e10cSrcweir return readBytes( p0,p1 ); 161cdf0e10cSrcweir } 162cdf0e10cSrcweir 163cdf0e10cSrcweir /** 164cdf0e10cSrcweir * Skips the next nBytesToSkip bytes (must be positive). 165cdf0e10cSrcweir * It is up to the implementation whether this method is blocking the thread or not. 166cdf0e10cSrcweir * 167cdf0e10cSrcweir *@param int 168cdf0e10cSrcweir */ skipBytes(int p0)169cdf0e10cSrcweir public synchronized void skipBytes(int p0) 170cdf0e10cSrcweir throws NotConnectedException, BufferSizeExceededException, 171cdf0e10cSrcweir com.sun.star.io.IOException, com.sun.star.uno.RuntimeException { 172cdf0e10cSrcweir read += p0; 173cdf0e10cSrcweir if( read > bigbuffer.length ) 174cdf0e10cSrcweir read = bigbuffer.length; 175cdf0e10cSrcweir 176cdf0e10cSrcweir if( read < offset ) 177cdf0e10cSrcweir read = offset; 178cdf0e10cSrcweir } 179cdf0e10cSrcweir } 180