FileDocCategorySizeDatePackage
RandomAccessStream.javaAPI DocJ2ME MIDP 2.011962Thu Nov 07 12:02:22 GMT 2002com.sun.midp.io.j2me.storage

RandomAccessStream

public class RandomAccessStream extends com.sun.midp.io.ConnectionBaseAdapter
A secure storage file stream that can be repositioned.

Fields Summary
public static final int
READ_WRITE_TRUNCATE
Signals the native code to open a truncated file for read-write.
private int
handle
Native file handle, set to -1 when not connected.
Constructors Summary
public RandomAccessStream()
Constructs a RandomAccessStream.


        
      
        MIDletSuite midletSuite = Scheduler.getScheduler().getMIDletSuite();

        // if a MIDlet suite is not scheduled, assume the JAM is calling.
        if (midletSuite != null) {
            midletSuite.checkIfPermissionAllowed(Permissions.MIDP);
        }
    
public RandomAccessStream(com.sun.midp.security.SecurityToken callerSecurityToken)
Constructs a RandomAccess Stream for callers that have a different permissions than the currently running MIDlet suite.

param
callerSecurityToken security token of the caller

        callerSecurityToken.checkIfPermissionAllowed(Permissions.MIDP);
    
Methods Summary
private static native voidclose(int handle)
Close a native stream.

param
handle native stream handle
exception
IOException if an I/O error occurs

public voidconnect(java.lang.String name, int mode, boolean timeouts)
Connect to a file in storage.

param
name The URL for the connection, without the without the protcol part.
param
mode {@link javax.microedition.io.Connection} READ, WRITE, or READ_WRITE
param
timeouts A flag to indicate that the called wants timeout exceptions, ignored
exception
IllegalArgumentException If a parameter is invalid.
exception
ConnectionNotFoundException If the connection cannot be found.
exception
IOException If some other kind of I/O error occurs.

        connect(name, mode);
    
public voidconnect(java.lang.String name, int mode)
Called to connect a RandomAccessStream to a file in storage.

param
name contains storage filename
param
mode {@link javax.microedition.io.Connection} READ, WRITE, or READ_WRITE
exception
IOException if an I/O error occurs

        byte[] asciiFilename;

        if (handle != -1) {
            throw new
                IOException("Disconnect the stream before reconnecting.");
        }

        asciiFilename = Util.toCString(name);
        handle = open(asciiFilename, mode);

        /* This object is re-used by internal methods */
        connectionOpen = true;

        if (mode == Connector.READ) {
            maxOStreams = 0;
        } else {
            maxOStreams = 1;
        }

        maxIStreams = 1;
    
public voiddisconnect()
Disconnect the stream.

Any streams obtained with either openInputStream or openOutputStream will throw an exception if used after this call.

exception
IOException if an I/O error occurs when closing the connection.

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

        close(handle);
        handle = -1;
        connectionOpen = false;
    
private native voidfinalize()
Ensures native resources are freed when Object is collected.

public intgetSizeOf()
Get the size of this stream.

return
size of this stream in bytes
exception
IOException if an I/O error occurs

        return sizeOf(handle);
    
private native intopen(byte[] szFilename, int mode)
Open a stream to a native file.

param
szFilename filename as an 8 bit zero terminated string.
param
mode {@link javax.microedition.io.Connection} READ, WRITE, or READ_WRITE
return
handle to a native stream
exception
IOException if an I/O error occurs

private static native voidposition(int handle, int absolutePosition)
Set the current position of a native stream.

param
handle native stream handle
param
absolutePosition desired position from the beginning of the stream.
exception
IOException if an I/O error occurs

private static native intread(int handle, byte[] buffer, int offset, int length)
Read at least one byte from a native stream.

param
handle native stream handle
param
buffer where to put the bytes
param
offset where in the buffer to starting putting bytes
param
length how many bytes to read
return
number of bytes read of -1 for the end of stream
exception
IOException if an I/O error occurs

public intreadBytes(byte[] b, int off, int len)
Reads up to len bytes of data from the input stream into an array of bytes, blocks until at least one byte is available.

Do not use this method if openInputStream has been called since the input stream may be buffering data.

param
b the buffer into which the data is read.
param
off the start offset in array b at which the data is written.
param
len the maximum number of bytes to read.
return
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
exception
IOException if an I/O error occurs.

        if (len == 0) {
            return 0;
        }

        // test before we goto the native code
        int test = b[off] + b[len - 1] + b[off + len - 1];

        return read(handle, b, off, len);
    
protected intreadBytesNonBlocking(byte[] b, int off, int len)
Reads up to len bytes of data from the input stream into an array of bytes, but does not block if no bytes available.

The readBytesNonBlocking method of ConnectionBaseAdapter does nothing and returns 0.

param
b the buffer into which the data is read.
param
off the start offset in array b at which the data is written.
param
len the maximum number of bytes to read.
return
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
exception
IOException if an I/O error occurs.

        // Read bytes should be non blocking
        return readBytes(b, off, len);
    
public voidsetPosition(int absolutePosition)
Set the absolute postion of this stream.

Do not use this method if either openInputStream or openOutputStream has been called since the streams may be buffering data.

param
absolutePosition position from the byte 0 of this stream
exception
IOException if an I/O error occurs

        position(handle, absolutePosition);
    
private static native intsizeOf(int handle)
Get the total size of a native stream.

param
handle native stream handle
return
size of the stream in bytes
exception
IOException if an I/O error occurs

public voidtruncate(int size)
Truncate the size of the stream to size bytes. This method cannot be used to make the size of the underlying stream larger.

param
size new size of this stream in bytes
exception
IOException if an I/O error occurs or size is larger than the stream size.

	truncateStream(handle, size);
    
private static native voidtruncateStream(int handle, int size)
Set the size of a native stream.

param
handle native stream handle
param
size size to truncate the native stream to.
exception
IOException if an I/O error occurs

private static native voidwrite(int handle, byte[] buffer, int offset, int length)
Write bytes to a native stream.

param
handle native stream handle
param
buffer what bytes write
param
offset where in the buffer the bytes start
param
length how many bytes to write
exception
IOException if an I/O error occurs

public intwriteBytes(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to this output stream.

Polling the native code is done here to allow for simple asynchronous native code to be written. Not all implementations work this way (they block in the native code) but the same Java code works for both.

Do not use this method if openOutputStream has been called since the output stream may be buffering data.

param
b the data.
param
off the start offset in the data.
param
len the number of bytes to write.
return
number of bytes written
exception
IOException if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.

        if (len == 0) {
            return 0;
        }

        // test before we goto the native code
        int test = b[off] + b[len - 1] + b[off + len - 1];

        write(handle, b, off, len);
        return len;
    
public intwriteStream(java.io.InputStream in)
Write the given stream fully into this one.

param
in stream write from
return
number of bytes written from the stream
exception
IOException if an I/O error occurs

        byte[] temp = new byte[1024];
        int bytesRead;
        int totalBytesWritten = 0;

        for (;;) {
            bytesRead = in.read(temp);
            if (bytesRead == -1) {
                return totalBytesWritten;
            }

            writeBytes(temp, 0, bytesRead);
            totalBytesWritten += bytesRead;
        }