Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / MyInputStream.java
blob89c092706c4b4676be223f6a6c092d77a9dcde84
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
35 import com.sun.star.io.BufferSizeExceededException;
36 import com.sun.star.io.NotConnectedException;
37 import com.sun.star.io.XInputStream;
38 import com.sun.star.io.XSeekable;
40 /**
41 * XInputStream interface implementation.
43 public class MyInputStream implements XSeekable, XInputStream {
45 /**
46 * Member properties
48 private int offset = 0;
49 private int read = offset;
50 private byte[] bigbuffer;
52 /**
53 * Constructor
55 public MyInputStream() {
58 // XSeekable. Makes it possible to seek to a certain position within a stream.
60 /**
61 * Returns the length of the stream.
63 *@return long The length of the storage medium on which the stream works.
65 public synchronized long getLength()
66 throws com.sun.star.io.IOException,com.sun.star.uno.RuntimeException {
67 if ( bigbuffer != null ) {
68 return bigbuffer.length - offset;
69 } else {
70 return 0;
74 /**
75 * Returns the current offset of the stream.
77 *@return long The current offset in this stream.
79 public synchronized long getPosition()
80 throws com.sun.star.io.IOException,com.sun.star.uno.RuntimeException {
81 return read - offset ;
84 /**
85 * Changes the seek pointer to a new location relative to the beginning of the stream.
87 public synchronized void seek(long p0)
88 throws IllegalArgumentException, com.sun.star.io.IOException,
89 com.sun.star.uno.RuntimeException {
90 if( bigbuffer != null ) {
91 p0 +=offset;
92 read = ( int ) p0;
93 if( read < offset || read > bigbuffer.length )
94 throw new IllegalArgumentException();
98 // XInputStream. This is the basic interface to read data from a stream.
101 * States how many bytes can be read or skipped without blocking.
103 *@return int If not available, then returned 0
105 public synchronized int available()
106 throws NotConnectedException, com.sun.star.io.IOException,
107 com.sun.star.uno.RuntimeException {
108 if( bigbuffer != null )
109 return ( bigbuffer.length - read );
110 else
111 return 0;
115 * Closes the stream. .
117 public void closeInput()
118 throws NotConnectedException,com.sun.star.io.IOException,
119 com.sun.star.uno.RuntimeException {
120 read = -1;
124 * Reads the specified number of bytes in the given sequence.
126 public synchronized int readBytes(byte[][] p0, int p1)
127 throws NotConnectedException, BufferSizeExceededException,
128 com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
129 if( bigbuffer != null ) {
130 if( read == -1 )
131 return 0;
132 int i = 0;
133 int available;
134 if ( p1 > bigbuffer.length - read )
135 available = bigbuffer.length - read;
136 else
137 available = p1;
139 p0[0] = new byte[p1];
140 while( available != 0 ) {
141 p0[0][i++] = bigbuffer[read++];
142 --available;
144 return i;
145 } else {
146 p0[0] = new byte[0];
147 return 0;
152 * Reads the available number of bytes at maximum nMaxBytesToRead .
153 * This method blocks the thread until at least one byte is available.
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 );
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.
167 public synchronized void skipBytes(int p0)
168 throws NotConnectedException, BufferSizeExceededException,
169 com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
170 read += p0;
171 if( read > bigbuffer.length )
172 read = bigbuffer.length;
174 if( read < offset )
175 read = offset;