merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / UCB / MyInputStream.java
blobb6a3ed8e5d821d1594cebac2892c84dd6e62a9ee
1 /*************************************************************************
3 * $RCSfile: MyInputStream.java,v $
5 * $Revision: 1.5 $
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
10 * the BSD license.
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
17 * are met:
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 *************************************************************************/
41 // imports
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;
47 /**
48 * XInputStream interface implementation.
50 public class MyInputStream implements XSeekable, XInputStream {
52 /**
53 * Member properties
55 private int offset = 0;
56 private int read = offset;
57 private byte[] bigbuffer;
59 /**
60 * Constructor
62 public MyInputStream() {
65 // XSeekable. Makes it possible to seek to a certain position within a stream.
67 /**
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;
76 } else {
77 return 0;
81 /**
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 ;
91 /**
92 * Changes the seek pointer to a new location relative to the beginning of the stream.
94 *@param long
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 ) {
100 p0 +=offset;
101 read = ( int ) p0;
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 );
119 else
120 return 0;
124 * Closes the stream. .
126 public void closeInput()
127 throws NotConnectedException,com.sun.star.io.IOException,
128 com.sun.star.uno.RuntimeException {
129 read = -1;
133 * Reads the specified number of bytes in the given sequence.
135 *@param byte[][]
136 *@param int
137 *@return int
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 ) {
143 if( read == -1 )
144 return 0;
145 int i = 0;
146 int available;
147 if ( p1 > bigbuffer.length - read )
148 available = bigbuffer.length - read;
149 else
150 available = p1;
152 p0[0] = new byte[p1];
153 while( available != 0 ) {
154 p0[0][i++] = bigbuffer[read++];
155 --available;
157 return i;
158 } else {
159 p0[0] = new byte[0];
160 return 0;
165 * Reads the available number of bytes at maximum nMaxBytesToRead .
166 * This method blocks the thread until at least one byte is available.
168 *@param byte[][]
169 *@param int
170 *@return int
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.
184 *@param int
186 public synchronized void skipBytes(int p0)
187 throws NotConnectedException, BufferSizeExceededException,
188 com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
189 read += p0;
190 if( read > bigbuffer.length )
191 read = bigbuffer.length;
193 if( read < offset )
194 read = offset;