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 
23 
24 // imports
25 import com.sun.star.io.BufferSizeExceededException;
26 import com.sun.star.io.NotConnectedException;
27 import com.sun.star.io.XInputStream;
28 import com.sun.star.io.XSeekable;
29 
30 /**
31  * XInputStream interface implementation.
32  */
33 public class MyInputStream implements XSeekable, XInputStream {
34 
35     /**
36      * Member properties
37      */
38     private int offset = 0;
39 	private int read = offset;
40 	private byte[] bigbuffer;
41 
42     /**
43      * Constructor
44      */
MyInputStream()45 	public MyInputStream() {
46 	}
47 
48 	    // XSeekable. Makes it possible to seek to a certain position within a stream.
49 
50     /**
51      *   Returns the length of the stream.
52      *
53      *@return  long  The length of the storage medium on which the stream works.
54      */
getLength()55     public synchronized long getLength()
56         throws com.sun.star.io.IOException,com.sun.star.uno.RuntimeException {
57 		if ( bigbuffer != null ) {
58 			return bigbuffer.length - offset;
59 		} else {
60             return 0;
61 		}
62 	}
63 
64     /**
65      *   Returns the current offset of the stream.
66      *
67      *@return  long  The current offset in this stream.
68      */
getPosition()69 	public synchronized long getPosition()
70         throws com.sun.star.io.IOException,com.sun.star.uno.RuntimeException {
71 		return read - offset ;
72 	}
73 
74     /**
75      *  Changes the seek pointer to a new location relative to the beginning of the stream.
76      *
77      *@param  long
78      */
seek(long p0)79 	public synchronized void seek(long p0)
80         throws IllegalArgumentException, com.sun.star.io.IOException,
81             com.sun.star.uno.RuntimeException {
82 		if( bigbuffer != null ) {
83             p0 +=offset;
84             read = ( int ) p0;
85             if( read < offset || read > bigbuffer.length )
86 			    throw new IllegalArgumentException();
87         }
88     }
89 
90 	    // XInputStream. This is the basic interface to read data from a stream.
91 
92     /**
93      *   States how many bytes can be read or skipped without blocking.
94      *
95      *@return  int  If not available, then returned 0
96      */
available()97     public synchronized int available()
98         throws NotConnectedException, com.sun.star.io.IOException,
99             com.sun.star.uno.RuntimeException {
100 		if( bigbuffer != null )
101 			return ( bigbuffer.length - read );
102 		else
103 			return 0;
104 	}
105 
106 	/**
107      *   Closes the stream. .
108      */
closeInput()109     public void closeInput()
110 	    throws NotConnectedException,com.sun.star.io.IOException,
111         com.sun.star.uno.RuntimeException {
112 		read = -1;
113     }
114 
115     /**
116      *   Reads the specified number of bytes in the given sequence.
117      *
118      *@param    byte[][]
119      *@param    int
120      *@return   int
121      */
readBytes(byte[][] p0, int p1)122 	public synchronized int readBytes(byte[][] p0, int p1)
123 		throws NotConnectedException, BufferSizeExceededException,
124             com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
125 		if( bigbuffer != null ) {
126 			if( read == -1 )
127       		    return 0;
128             int i = 0;
129             int available;
130             if ( p1 > bigbuffer.length - read )
131                 available = bigbuffer.length - read;
132             else
133                 available = p1;
134 
135 			p0[0] = new byte[p1];
136 			while( available != 0 ) {
137 			    p0[0][i++] = bigbuffer[read++];
138 				--available;
139 			}
140 			return i;
141         } else {
142 			p0[0] = new byte[0];
143 			return 0;
144 		}
145     }
146 
147 	/**
148      *  Reads the available number of bytes at maximum  nMaxBytesToRead .
149      *  This method blocks the thread until at least one byte is available.
150      *
151      *@param    byte[][]
152      *@param    int
153      *@return   int
154      */
readSomeBytes(byte[][] p0, int p1)155     public synchronized int readSomeBytes(byte[][] p0, int p1)
156 		    throws 	NotConnectedException,
157         		    BufferSizeExceededException,
158 		            com.sun.star.io.IOException,
159 		            com.sun.star.uno.RuntimeException {
160 		    return readBytes( p0,p1 );
161 	}
162 
163     /**
164      *  Skips the next nBytesToSkip bytes (must be positive).
165      *  It is up to the implementation whether this method is blocking the thread or not.
166      *
167      *@param    int
168      */
skipBytes(int p0)169     public synchronized void skipBytes(int p0)
170         throws 	NotConnectedException, BufferSizeExceededException,
171 		    com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
172         read += p0;
173 	    if( read > bigbuffer.length )
174     	    read = bigbuffer.length;
175 
176 	    if( read < offset )
177 		    read = offset;
178     }
179 }
180