cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / MyInputStream.java
blob048f2950cf898e3666c974853215d73e452f99de
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
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;
41 /**
42 * XInputStream interface implementation.
44 public class MyInputStream implements XSeekable, XInputStream {
46 /**
47 * Member properties
49 private int offset = 0;
50 private int read = offset;
51 private byte[] bigbuffer;
53 /**
54 * Constructor
56 public MyInputStream() {
59 // XSeekable. Makes it possible to seek to a certain position within a stream.
61 /**
62 * Returns the length of the stream.
64 *@return long The length of the storage medium on which the stream works.
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;
75 /**
76 * Returns the current offset of the stream.
78 *@return long The current offset in this stream.
80 public synchronized long getPosition()
81 throws com.sun.star.io.IOException,com.sun.star.uno.RuntimeException {
82 return read - offset ;
85 /**
86 * Changes the seek pointer to a new location relative to the beginning of the stream.
88 public synchronized void seek(long p0)
89 throws IllegalArgumentException, com.sun.star.io.IOException,
90 com.sun.star.uno.RuntimeException {
91 if( bigbuffer != null ) {
92 p0 +=offset;
93 read = ( int ) p0;
94 if( read < offset || read > bigbuffer.length )
95 throw new IllegalArgumentException();
99 // XInputStream. This is the basic interface to read data from a stream.
102 * States how many bytes can be read or skipped without blocking.
104 *@return int If not available, then returned 0
106 public synchronized int available()
107 throws NotConnectedException, com.sun.star.io.IOException,
108 com.sun.star.uno.RuntimeException {
109 if( bigbuffer != null )
110 return ( bigbuffer.length - read );
111 else
112 return 0;
116 * Closes the stream. .
118 public void closeInput()
119 throws NotConnectedException,com.sun.star.io.IOException,
120 com.sun.star.uno.RuntimeException {
121 read = -1;
125 * Reads the specified number of bytes in the given sequence.
127 public synchronized int readBytes(byte[][] p0, int p1)
128 throws NotConnectedException, BufferSizeExceededException,
129 com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
130 if( bigbuffer != null ) {
131 if( read == -1 )
132 return 0;
133 int i = 0;
134 int available;
135 if ( p1 > bigbuffer.length - read )
136 available = bigbuffer.length - read;
137 else
138 available = p1;
140 p0[0] = new byte[p1];
141 while( available != 0 ) {
142 p0[0][i++] = bigbuffer[read++];
143 --available;
145 return i;
146 } else {
147 p0[0] = new byte[0];
148 return 0;
153 * Reads the available number of bytes at maximum nMaxBytesToRead .
154 * This method blocks the thread until at least one byte is available.
156 public synchronized int readSomeBytes(byte[][] p0, int p1)
157 throws NotConnectedException,
158 BufferSizeExceededException,
159 com.sun.star.io.IOException,
160 com.sun.star.uno.RuntimeException {
161 return readBytes( p0,p1 );
165 * Skips the next nBytesToSkip bytes (must be positive).
166 * It is up to the implementation whether this method is blocking the thread or not.
168 public synchronized void skipBytes(int p0)
169 throws NotConnectedException, BufferSizeExceededException,
170 com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
171 read += p0;
172 if( read > bigbuffer.length )
173 read = bigbuffer.length;
175 if( read < offset )
176 read = offset;
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */