FileDocCategorySizeDatePackage
ResourceInputStream.javaAPI DocJ2ME MIDP 2.07004Thu Nov 07 12:02:20 GMT 2002com.sun.midp.io

ResourceInputStream

public class ResourceInputStream extends InputStream
Input stream class for accessing resource files in classpath.

Fields Summary
private int
handle
native handle to underlying stream
protected int
pos
The index of the next character to read from the input stream buffer. This value should always be nonnegative and not larger than the value of size.
protected int
size
The index one greater than the last valid character in the input stream buffer. This value should always be nonnegative.
Constructors Summary
public ResourceInputStream(String name)
Construct a resource input stream for accessing objects in the jar file.

param
name the name of the resource in classpath to access. The name must not have a leading '/'.
exception
IOException if an I/O error occurs.

	if (handle != -1) { 
	    throw new IOException();
	}  

	String fixedName = fixResourceName(name);

	handle = open(fixedName.getBytes());  // open() sets 'size' data member
	pos = 0;
    
Methods Summary
public intavailable()
Gets the number of bytes remaining to be read.

return
the number of bytes remaining in the resource.
exception
IOException if an I/O error occurs.

	return size - pos;
    
private native voidclose(int handle)
native interface to close the opened resource.

param
handle for the opened resource
exception
IOException if I/O error occurs.

public voidclose()
closes the open resource stream.

exception
IOException if an I/O error occurs.

 
	close(handle);
	handle = -1;
    
private native voidfinalize()
native finalizaion

private java.lang.StringfixResourceName(java.lang.String name)
Fixes the resource name to be conformant with the CLDC 1.0 specification. We are not allowed to use "../" to get outside of the .jar file.

param
name the name of the resource in classpath to access.
return
the fixed string.
exception
IOException if the resource name points to a classfile, as determined by the resource name's extension.


                                                                                                   
          
	Vector dirVector = new Vector();
	int    startIdx = 0;
	int    endIdx = 0;
	String curDir;

	while ((endIdx = name.indexOf('/", startIdx)) != -1) {
	    if (endIdx == startIdx) {
		// We have a leading '/' or two consecutive '/'s
		startIdx++;
		continue;
	    }

	    curDir = name.substring(startIdx, endIdx);
	    startIdx = endIdx + 1;

	    if (curDir.equals(".")) {
		// Ignore a single '.' directory
		continue;
	    }
	    if (curDir.equals("..")) {
		// Go up a level
		int size = dirVector.size();

		if (size > 0) {
		    dirVector.removeElementAt(size - 1);
		} else {
		    // Do not allow "/../resource"
		    throw new IOException();
		}
		continue;
	    }

	    dirVector.addElement(curDir);
	}

	// save directory structure
	int nameLength = name.length();
	StringBuffer dirName = new StringBuffer(nameLength);
	int numElements = dirVector.size();

	for (int i = 0; i < numElements; i++) {
	    dirName.append((String)dirVector.elementAt(i));
	    dirName.append("/");
	}

	// save filename
	if (startIdx < nameLength) {
	    String filename = name.substring(startIdx);

	    // Throw IOE if the resource ends with ".class", but, not
            //  if the entire name is ".class"
	    if ((filename.endsWith(".class")) && 
		(! ".class".equals(filename))) {
		throw new IOException();
	    }
	    dirName.append(name.substring(startIdx));
	}

	return dirName.toString();
    
private native intopen(byte[] name)
native interface to open resource as stream.

param
name name of the resource in CLASSPATH
return
the Object reference to the located resource.
exception
IOException if I/O error occurs.

public intread()
Reads the next byte of data from the input stream.

return
the next byte of data, or -1 if the end of the stream is reached.
exception
IOException if an I/O error occurs.

 
        if (pos < size) {
            pos++;
        } else {
            return -1;
        }
        return read(handle);
    
public intread(byte[] b, int off, int len)
Reads bytes into a byte array.

param
b the buffer to read into.
param
off offset to start at in the buffer.
param
len number of bytes to read.
return
the number of bytes read, or -1 if the end of the stream is reached.
exception
IOException if an I/O error occurs.

        if (b == null) {
            throw new NullPointerException();
        } else if ((off < 0) || (off > b.length) || (len < 0) ||
                   ((off + len) > b.length) || ((off + len) < 0)) {
            throw new IndexOutOfBoundsException();
        }
        if (pos >= size) {
            return -1;
        }
        if (pos + len > size) {
            len = size - pos;
        }
        if (len <= 0) {
            return 0;
        }
        int result = readBytes(handle, b, off, pos, len);
	if (result > -1) {
	    pos += result;
	}
	return result;
    
private native intread(int handle)
native interface to read a byte from the opened resource.

param
handle for the opened resource
return
a character read from the opened resource.
exception
IOException if I/O error occurs.

private native intreadBytes(int handle, byte[] b, int offset, int pos, int len)
native interface to read bytes from the opened resource.

param
handle for the opened resource
param
b array for the data
param
offset Offset in byte array to start reading into
param
pos position in resource to start reading
param
len number of bytes to read
return
number of characters read into the buffer
exception
IOException if I/O error occurs.

public synchronized voidreset()
Resets the buffer position to zero. The value of pos is set to 0.

        if (handle == -1) {
            return;
	}
        pos = 0;