1 /*************************************************************************
3 * $RCSfile: MyInputStream.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:58:25 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
42 import com
.sun
.star
.io
.BufferSizeExceededException
;
43 import com
.sun
.star
.io
.NotConnectedException
;
44 import com
.sun
.star
.io
.XInputStream
;
45 import com
.sun
.star
.io
.XSeekable
;
48 * XInputStream interface implementation.
50 public class MyInputStream
implements XSeekable
, XInputStream
{
55 private int offset
= 0;
56 private int read
= offset
;
57 private byte[] bigbuffer
;
62 public MyInputStream() {
65 // XSeekable. Makes it possible to seek to a certain position within a stream.
68 * Returns the length of the stream.
70 *@return long The length of the storage medium on which the stream works.
72 public synchronized long getLength()
73 throws com
.sun
.star
.io
.IOException
,com
.sun
.star
.uno
.RuntimeException
{
74 if ( bigbuffer
!= null ) {
75 return bigbuffer
.length
- offset
;
82 * Returns the current offset of the stream.
84 *@return long The current offset in this stream.
86 public synchronized long getPosition()
87 throws com
.sun
.star
.io
.IOException
,com
.sun
.star
.uno
.RuntimeException
{
88 return read
- offset
;
92 * Changes the seek pointer to a new location relative to the beginning of the stream.
96 public synchronized void seek(long p0
)
97 throws IllegalArgumentException
, com
.sun
.star
.io
.IOException
,
98 com
.sun
.star
.uno
.RuntimeException
{
99 if( bigbuffer
!= null ) {
102 if( read
< offset
|| read
> bigbuffer
.length
)
103 throw new IllegalArgumentException();
107 // XInputStream. This is the basic interface to read data from a stream.
110 * States how many bytes can be read or skipped without blocking.
112 *@return int If not available, then returned 0
114 public synchronized int available()
115 throws NotConnectedException
, com
.sun
.star
.io
.IOException
,
116 com
.sun
.star
.uno
.RuntimeException
{
117 if( bigbuffer
!= null )
118 return ( bigbuffer
.length
- read
);
124 * Closes the stream. .
126 public void closeInput()
127 throws NotConnectedException
,com
.sun
.star
.io
.IOException
,
128 com
.sun
.star
.uno
.RuntimeException
{
133 * Reads the specified number of bytes in the given sequence.
139 public synchronized int readBytes(byte[][] p0
, int p1
)
140 throws NotConnectedException
, BufferSizeExceededException
,
141 com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
{
142 if( bigbuffer
!= null ) {
147 if ( p1
> bigbuffer
.length
- read
)
148 available
= bigbuffer
.length
- read
;
152 p0
[0] = new byte[p1
];
153 while( available
!= 0 ) {
154 p0
[0][i
++] = bigbuffer
[read
++];
165 * Reads the available number of bytes at maximum nMaxBytesToRead .
166 * This method blocks the thread until at least one byte is available.
172 public synchronized int readSomeBytes(byte[][] p0
, int p1
)
173 throws NotConnectedException
,
174 BufferSizeExceededException
,
175 com
.sun
.star
.io
.IOException
,
176 com
.sun
.star
.uno
.RuntimeException
{
177 return readBytes( p0
,p1
);
181 * Skips the next nBytesToSkip bytes (must be positive).
182 * It is up to the implementation whether this method is blocking the thread or not.
186 public synchronized void skipBytes(int p0
)
187 throws NotConnectedException
, BufferSizeExceededException
,
188 com
.sun
.star
.io
.IOException
, com
.sun
.star
.uno
.RuntimeException
{
190 if( read
> bigbuffer
.length
)
191 read
= bigbuffer
.length
;