ewe.io
Class MemoryFile

java.lang.Object
  extended byewe.util.Errorable
      extended byewe.io.BasicStreamObject
          extended byewe.io.StreamObject
              extended byewe.io.RandomStreamObject
                  extended byewe.io.MemoryFile
All Implemented Interfaces:
BasicRandomAccessStream, BasicStream, FastStream, RandomAccessStream, Stream, Streamable
Direct Known Subclasses:
StringStream

public class MemoryFile
extends RandomStreamObject
implements FastStream

This is used for creating a RandomAccessStream from a byte array or from a file on disk.


Field Summary
 ByteArray data
           
protected  int mode
           
 
Fields inherited from class ewe.io.StreamObject
napIterations, napTime, READWRITE_CLOSED, READWRITE_ERROR, READWRITE_WOULDBLOCK
 
Fields inherited from class ewe.io.BasicStreamObject
closed
 
Fields inherited from class ewe.util.Errorable
error
 
Constructor Summary
MemoryFile()
           
MemoryFile(byte[] bytes, int start, int length, int mode)
          Deprecated. - use new MemoryFile(byte [] bytes,int start,int length,String mode) instead.
MemoryFile(byte[] bytes, int start, int length, String mode)
          Create a memory file from an array of bytes.
MemoryFile(ByteArray dataToUse)
          This opens it in READ_ONLY mode.
MemoryFile(ByteArray dataToUse, String mode)
           
MemoryFile(File in, String mode, boolean useFastStream)
          This creates a MemoryFile from an input File.
MemoryFile(Stream is, String mode)
          This creates a MemoryFile from an input stream.
MemoryFile(String path, int mode)
          Deprecated. - use new MemoryFile(Stream is,String mode) instead.
 
Method Summary
 boolean canWrite()
          Tests if the RandomAccessStream was opened in read-write mode as opposed to read-ony mode.
static MemoryFile createFrom(Stream is, StringBuffer error)
          Deprecated. - use new MemoryFile(Stream is,String mode) instead.
 boolean flushStream()
          This is non-blocking.
 int getFilePosition()
          Get the file position.
 int getLength()
          Returns the length of the file in bytes.
 boolean isOpen()
          Returns if the stream is open or not.
 int nonBlockingRead(byte[] buf, int start, int count)
          This is the non-blocking read operation.
 int nonBlockingWrite(byte[] buf, int start, int count)
          This is the non-blocking write operation.
 int quickRead(byte[] data, int offset, int length, boolean readAll)
          This attempts to read using a fast native or direct method.
 void quickWrite(byte[] data, int offset, int length)
          This attempts to write using a fast native or direct access method.
 boolean seek(int pos)
          Set the file position.
 boolean setStreamLength(long length)
          Set the length of the RandomAccessStream if possible.
 
Methods inherited from class ewe.io.RandomStreamObject
getStreamLength, length, nonBlockingRead, nonBlockingWrite, seek, seekPosition, setLength, tell, tellPosition, toRandomStream
 
Methods inherited from class ewe.io.StreamObject
close, closeStream, doRead, doWrite, flush, nap, nap, read, readAByte, readBytes, readBytes, write, writeAByte, writeBytes, writeBytes
 
Methods inherited from class ewe.io.BasicStreamObject
getException, getName, read, read, throwIOException, toInputStream, toOutputStream, toStream, write, write
 
Methods inherited from class ewe.util.Errorable
returnError, returnError, returnError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 
Methods inherited from interface ewe.io.BasicStream
close, closeStream
 
Methods inherited from interface ewe.io.Stream
flush, read, read, read, readBytes, readBytes, toInputStream, toOutputStream, write, write, write, writeBytes, writeBytes
 

Field Detail

mode

protected int mode

data

public ByteArray data
Constructor Detail

MemoryFile

public MemoryFile()

MemoryFile

public MemoryFile(ByteArray dataToUse)
This opens it in READ_ONLY mode.


MemoryFile

public MemoryFile(ByteArray dataToUse,
                  String mode)

MemoryFile

public MemoryFile(byte[] bytes,
                  int start,
                  int length,
                  int mode)
Deprecated. - use new MemoryFile(byte [] bytes,int start,int length,String mode) instead.


MemoryFile

public MemoryFile(byte[] bytes,
                  int start,
                  int length,
                  String mode)
Create a memory file from an array of bytes.

Parameters:
bytes -
start -
length -
mode -

MemoryFile

public MemoryFile(String path,
                  int mode)
Deprecated. - use new MemoryFile(Stream is,String mode) instead.


MemoryFile

public MemoryFile(File in,
                  String mode,
                  boolean useFastStream)
           throws IOException
This creates a MemoryFile from an input File. All bytes from the stream are read in and written to the memory file. Alterations to the returned MemoryFile will not affect the original data source. The original File is closed.


MemoryFile

public MemoryFile(Stream is,
                  String mode)
           throws IOException
This creates a MemoryFile from an input stream. All bytes from the stream are read in and written to the memory file. Alterations to the returned MemoryFile will not affect the original data source. The original Stream is closed.

Method Detail

isOpen

public boolean isOpen()
Description copied from interface: BasicStream
Returns if the stream is open or not.

Specified by:
isOpen in interface BasicStream
Overrides:
isOpen in class StreamObject

flushStream

public boolean flushStream()
                    throws IOException
Description copied from interface: BasicStream
This is non-blocking. It returns true if the flush completed, false if it did not, or throws an exception on error.

Specified by:
flushStream in interface BasicStream
Throws:
IOException

nonBlockingRead

public int nonBlockingRead(byte[] buf,
                           int start,
                           int count)
Description copied from interface: BasicStream
This is the non-blocking read operation. It should never attempt to wait() or sleep() in a Coroutine. It should return as quickly as possible. This makes it safe to be called from within a native method. Note that this should NEVER be called with a count of zero.

Specified by:
nonBlockingRead in interface BasicStream
Overrides:
nonBlockingRead in class StreamObject
Parameters:
buf - Destination byte array to hold incoming data.
start - Starting index in buff for incoming data.
count - Maximum number of bytes to read - should never be zero.
Returns:
greater than 0 = Number of bytes read.
0 = No bytes available to read at this time.
-1 = Stream end reached no further bytes to read.
-2 = IO Error.
Note that these values are different to the Stream.readBytes method.

nonBlockingWrite

public int nonBlockingWrite(byte[] buf,
                            int start,
                            int count)
Description copied from interface: BasicStream
This is the non-blocking write operation. It should never attempt to wait() or sleep() in a Coroutine. It should return as quickly as possible. This makes it safe to be called from within a native method.

Specified by:
nonBlockingWrite in interface BasicStream
Overrides:
nonBlockingWrite in class StreamObject
Parameters:
buf - Source byte array holding data to be written.
start - Starting index in buff for data to be written.
count - Number of bytes to write - should never be zero.
Returns:
greater than 0 = Number of bytes actually written.
0 = No bytes could be written yet - but the stream is still open.
-1 = Stream has been closed - no further writes are possible.
-2 = IO error - something went wrong.
Note that these values are different to the Stream.writeBytes method.

canWrite

public boolean canWrite()
Description copied from interface: BasicRandomAccessStream
Tests if the RandomAccessStream was opened in read-write mode as opposed to read-ony mode.

Specified by:
canWrite in interface BasicRandomAccessStream

getLength

public int getLength()
Returns the length of the file in bytes. If the file is not open 0 will be returned.

Specified by:
getLength in interface RandomAccessStream
Overrides:
getLength in class RandomStreamObject

seek

public boolean seek(int pos)
Description copied from interface: RandomAccessStream
Set the file position. This is a blocking call.

Specified by:
seek in interface RandomAccessStream
Overrides:
seek in class RandomStreamObject

getFilePosition

public int getFilePosition()
Description copied from interface: RandomAccessStream
Get the file position. This is a blocking call.

Specified by:
getFilePosition in interface RandomAccessStream
Overrides:
getFilePosition in class RandomStreamObject

createFrom

public static MemoryFile createFrom(Stream is,
                                    StringBuffer error)
Deprecated. - use new MemoryFile(Stream is,String mode) instead.

This creates a MemoryFile from an input stream. All bytes from the stream are read in and written to the memory file. Alterations to the returned MemoryFile will not affect the original data source. The original Stream is closed.


setStreamLength

public boolean setStreamLength(long length)
                        throws IOException
Description copied from interface: BasicRandomAccessStream
Set the length of the RandomAccessStream if possible. This is non-blocking, it will return false if the operation is still continuing and true if the operation completed. If the operation could not be performed at all an IOException will be thrown.

Make no assumptions about the success of this method. Not all RAS objects will support setStreamLength() or setLength() - not even all Files on all systems will support this. For example, PersonalJava/Java 1.1 does not support this feature and will throw an IOException.

What happens to the file position pointer after this method is called is unpredictable, especially if you are truncating the file. You should ALWAYS reset the file position pointer after calling this method to be where you wish it to be.

Specified by:
setStreamLength in interface BasicRandomAccessStream
Overrides:
setStreamLength in class RandomStreamObject
Throws:
IOException

quickRead

public int quickRead(byte[] data,
                     int offset,
                     int length,
                     boolean readAll)
              throws IOException
Description copied from interface: FastStream
This attempts to read using a fast native or direct method. Failing that a normal read is done. If a native method is used the method will block the entire VM until the data is read, so you should use this with care.

Specified by:
quickRead in interface FastStream
Parameters:
data - The destination for the data.
offset - The index in the destination for the data.
length - The number of bytes to read.
readAll - if this is true then the method will not return until a full length number of bytes have been read. If the stream ends before this then an IOException is thrown.
Returns:
the number of bytes read or -1 if the stream ended with no bytes read.
Throws:
IOException

quickWrite

public void quickWrite(byte[] data,
                       int offset,
                       int length)
                throws IOException
Description copied from interface: FastStream
This attempts to write using a fast native or direct access method. Failing that a normal write is done. If the native method is used the method will block the entire VM until the data is written, so you should use this with care.

Specified by:
quickWrite in interface FastStream
Parameters:
data - The source of the data bytes.
offset - The index in the source of the data.
length - The number of bytes to write.
Throws:
IOException