pl.psnc.dl.ege.utils
Class DataBuffer

java.lang.Object
  extended by pl.psnc.dl.ege.utils.DataBuffer

public class DataBuffer
extends java.lang.Object

Keeps collection of buffered byte data, which is used mainly by conversion with validation process.

Each singular data item can be kept in memory - if it does not reaches specified threshold (in number of bytes). If it does DataBuffer is permitted to save data as temporary file.

Important: while buffer is referenced it is not cleared from data; assigning new items indefinitely may result in stack overflow.
Memory can be relieved by class methods.
Each allocation in buffer is of type : single-write/multiple-read.

Author:
mariuszs

Field Summary
static int DEFAULT_ITEM_MAX_SIZE
          Default value : max size of item (in number of bytes), that allows to keep it in memory.
 
Constructor Summary
DataBuffer(int itemMaxSize, java.lang.String tmpDirectory)
          Creates instance of data buffer with specified temporary files directory and threshold for every contained data item.
DataBuffer(java.lang.String tmpDirectory)
          Creates instance of data buffer with specified temporary files directory - where overwhelmed data is kept.
 
Method Summary
 java.lang.String allocate()
          Allocates clean data item in buffer.
 java.lang.String allocate(java.io.InputStream inputStream)
          Reads data from specified input stream and creates single data item.
 java.lang.String allocate(java.io.InputStream inputStream, java.lang.String itemName)
          Reads data from specified input stream and creates single data item.
 void clear(boolean forceDelete)
          Relieves all stored in buffer data items If 'forceDelete' parameter is set to 'true' all temporary files will be deleted immediately, otherwise they will be deleted after release of memory by garbage collector.
 java.io.InputStream getDataAsStream(java.lang.String id)
          Returns specified by id - data item as input stream.
 java.lang.String getDataDir(java.lang.String id)
          Returns temp dir of allocated item - can be null if item is stored in memory.
 java.io.OutputStream getElementOutputStream(java.lang.String id)
          Returns buffer allocation output stream.
 java.lang.String getTemporaryDir()
           
 boolean removeData(java.lang.String id, boolean forceDelete)
          Relieves selected data item.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ITEM_MAX_SIZE

public static final int DEFAULT_ITEM_MAX_SIZE
Default value : max size of item (in number of bytes), that allows to keep it in memory.

See Also:
Constant Field Values
Constructor Detail

DataBuffer

public DataBuffer(java.lang.String tmpDirectory)
Creates instance of data buffer with specified temporary files directory - where overwhelmed data is kept.

Parameters:
tmpDirectory - temporary files directory

DataBuffer

public DataBuffer(int itemMaxSize,
                  java.lang.String tmpDirectory)
Creates instance of data buffer with specified temporary files directory and threshold for every contained data item.

Parameters:
itemMaxSize - maximum size of data item
tmpDirectory - temporary files directory
Method Detail

allocate

public java.lang.String allocate()

Allocates clean data item in buffer.

Method returns id of allocated data item.

Returns:
'id' of allocated data item.

getElementOutputStream

public java.io.OutputStream getElementOutputStream(java.lang.String id)
                                            throws java.lang.IllegalStateException

Returns buffer allocation output stream.

If output stream of selected item was closed, using this method again on the same item will result in IllegalStateException.

Parameters:
id - of buffer item
Returns:
buffer item output stream
Throws:
java.lang.IllegalStateException

allocate

public java.lang.String allocate(java.io.InputStream inputStream)
Reads data from specified input stream and creates single data item.
If item maximum size is reached, data is written to temporary file. Method returns unique id of created item.

Parameters:
inputStream - streamed input data
Returns:
'id' of allocated data item

allocate

public java.lang.String allocate(java.io.InputStream inputStream,
                                 java.lang.String itemName)
Reads data from specified input stream and creates single data item.

If item maximum size is reached, data is written to temporary file with name of 'itemName'.

Method returns unique id of created item.

Parameters:
inputStream -
itemName -
Returns:

getDataAsStream

public java.io.InputStream getDataAsStream(java.lang.String id)

Returns specified by id - data item as input stream.

If item does not exists in buffer method returns 'null'.

Parameters:
'id' - of a data item.
Returns:
streamed data item

getDataDir

public java.lang.String getDataDir(java.lang.String id)
Returns temp dir of allocated item - can be null if item is stored in memory.

Parameters:
id -
Returns:

removeData

public boolean removeData(java.lang.String id,
                          boolean forceDelete)

Relieves selected data item.

If 'forceDelete' parameter is set to 'true' temporary file (if it was created) will be deleted immediately, otherwise it will be deleted after release of memory by garbage collector.
Method returns 'false' if selected item does not exists in buffer, otherwise it returns 'true'.

Parameters:
'id' - of data item

clear

public void clear(boolean forceDelete)

Relieves all stored in buffer data items

If 'forceDelete' parameter is set to 'true' all temporary files will be deleted immediately, otherwise they will be deleted after release of memory by garbage collector.


getTemporaryDir

public java.lang.String getTemporaryDir()


Copyright © 2010. All Rights Reserved.