View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2001-2008 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  It is licensed under the following two licenses as alternatives:
6    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
7    *    2. Apache License (the "AL") Version 2.0
8    *
9    *  You may not use this file except in compliance with at least one of
10   *  the above two licenses.
11   *
12   *  You may obtain a copy of the LGPL at
13   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
14   *
15   *  You may obtain a copy of the AL at
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the LGPL or the AL for the specific language governing permissions and
22   *  limitations.
23   *
24   *  @version $Id: MIDletResourceInputStream.java 1733 2008-05-30 06:53:29Z vlads $
25   */
26  package org.microemu.app.util;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  
31  /**
32   * Special InputStream wrapper for loading resources is needed to change
33   * behavior of read(byte[] b) under Java SE. All existing Java ME
34   * implementations always read all bytes available in one read call.
35   */
36  public class MIDletResourceInputStream extends InputStream {
37  
38  	private InputStream is;
39  
40  	public MIDletResourceInputStream(InputStream is) {
41  		this.is = is;
42  	}
43  
44  	public int available() throws IOException {
45  		return is.available();
46  	}
47  
48  	public int read() throws IOException {
49  		return is.read();
50  	}
51  
52  	public int read(byte[] b) throws IOException {
53  		int result = 0;
54  		int count = 0;
55  		do {
56  			count = is.read(b, result, b.length - result);
57  			if (count != -1) {
58  				result += count;
59  				if (result == b.length) {
60  					return result;
61  				}
62  			} else if (result != 0) {
63  				return result;
64  			}
65  		} while (count != -1);
66  
67  		return -1;
68  	}
69  
70  }