1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 *************************************************************************/ 34 35 // imports 36 import com.sun.star.io.BufferSizeExceededException; 37 import com.sun.star.io.NotConnectedException; 38 import com.sun.star.io.XInputStream; 39 import com.sun.star.io.XSeekable; 40 41 /** 42 * XInputStream interface implementation. 43 */ 44 public class MyInputStream implements XSeekable, XInputStream { 45 46 /** 47 * Member properties 48 */ 49 private int offset = 0; 50 private int read = offset; 51 private byte[] bigbuffer; 52 53 /** 54 * Constructor 55 */ 56 public MyInputStream() { 57 } 58 59 // XSeekable. Makes it possible to seek to a certain position within a stream. 60 61 /** 62 * Returns the length of the stream. 63 * 64 *@return long The length of the storage medium on which the stream works. 65 */ 66 public synchronized long getLength() 67 throws com.sun.star.io.IOException,com.sun.star.uno.RuntimeException { 68 if ( bigbuffer != null ) { 69 return bigbuffer.length - offset; 70 } else { 71 return 0; 72 } 73 } 74 75 /** 76 * Returns the current offset of the stream. 77 * 78 *@return long The current offset in this stream. 79 */ 80 public synchronized long getPosition() 81 throws com.sun.star.io.IOException,com.sun.star.uno.RuntimeException { 82 return read - offset ; 83 } 84 85 /** 86 * Changes the seek pointer to a new location relative to the beginning of the stream. 87 * 88 *@param long 89 */ 90 public synchronized void seek(long p0) 91 throws IllegalArgumentException, com.sun.star.io.IOException, 92 com.sun.star.uno.RuntimeException { 93 if( bigbuffer != null ) { 94 p0 +=offset; 95 read = ( int ) p0; 96 if( read < offset || read > bigbuffer.length ) 97 throw new IllegalArgumentException(); 98 } 99 } 100 101 // XInputStream. This is the basic interface to read data from a stream. 102 103 /** 104 * States how many bytes can be read or skipped without blocking. 105 * 106 *@return int If not available, then returned 0 107 */ 108 public synchronized int available() 109 throws NotConnectedException, com.sun.star.io.IOException, 110 com.sun.star.uno.RuntimeException { 111 if( bigbuffer != null ) 112 return ( bigbuffer.length - read ); 113 else 114 return 0; 115 } 116 117 /** 118 * Closes the stream. . 119 */ 120 public void closeInput() 121 throws NotConnectedException,com.sun.star.io.IOException, 122 com.sun.star.uno.RuntimeException { 123 read = -1; 124 } 125 126 /** 127 * Reads the specified number of bytes in the given sequence. 128 * 129 *@param byte[][] 130 *@param int 131 *@return int 132 */ 133 public synchronized int readBytes(byte[][] p0, int p1) 134 throws NotConnectedException, BufferSizeExceededException, 135 com.sun.star.io.IOException, com.sun.star.uno.RuntimeException { 136 if( bigbuffer != null ) { 137 if( read == -1 ) 138 return 0; 139 int i = 0; 140 int available; 141 if ( p1 > bigbuffer.length - read ) 142 available = bigbuffer.length - read; 143 else 144 available = p1; 145 146 p0[0] = new byte[p1]; 147 while( available != 0 ) { 148 p0[0][i++] = bigbuffer[read++]; 149 --available; 150 } 151 return i; 152 } else { 153 p0[0] = new byte[0]; 154 return 0; 155 } 156 } 157 158 /** 159 * Reads the available number of bytes at maximum nMaxBytesToRead . 160 * This method blocks the thread until at least one byte is available. 161 * 162 *@param byte[][] 163 *@param int 164 *@return int 165 */ 166 public synchronized int readSomeBytes(byte[][] p0, int p1) 167 throws NotConnectedException, 168 BufferSizeExceededException, 169 com.sun.star.io.IOException, 170 com.sun.star.uno.RuntimeException { 171 return readBytes( p0,p1 ); 172 } 173 174 /** 175 * Skips the next nBytesToSkip bytes (must be positive). 176 * It is up to the implementation whether this method is blocking the thread or not. 177 * 178 *@param int 179 */ 180 public synchronized void skipBytes(int p0) 181 throws NotConnectedException, BufferSizeExceededException, 182 com.sun.star.io.IOException, com.sun.star.uno.RuntimeException { 183 read += p0; 184 if( read > bigbuffer.length ) 185 read = bigbuffer.length; 186 187 if( read < offset ) 188 read = offset; 189 } 190 } 191