m.c.m.proxyma.buffers
Class SmartBuffer

java.lang.Object
  extended by m.c.m.proxyma.buffers.SmartBuffer
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, ByteBuffer

public class SmartBuffer
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable, ByteBuffer

This class implements a generalyzed Buffer that can be used to store large and small binary data. It uses both a RamBuffer and a FileBuffer to mantain the data. If the data that comes into the buffer are more than its soft-limit, the buffer will store the exceding data into a temporary file to prevent high memory consumption. You can't write simultaneouusly from separate threads (write operations are not thread-safe).

NOTE: this software is released under GPL License. See the LICENSE of this distribution for more informations.

Version:
$Id: SmartBuffer.java 138 2010-06-20 13:53:32Z marcolinuz $
Author:
Marco Casavecchia Morganti (marcolinuz) [ICQ UIN: 245662445]
See Also:
Serialized Form

Constructor Summary
SmartBuffer()
          Create a smart buffer that can countain into RAM a maximum of 256Kb of data.
SmartBuffer(int maxRamSize)
          Create a smart buffer that can countain into RAM a specific amount of data.
 
Method Summary
 long appendByte(int data)
          Append the passed byte to the buffer.
 long appendBytes(byte[] data, int toAppend)
          Append the passed byte array to the buffer.
 java.lang.Object clone()
          This method clones the current object.
protected  FileBuffer getFileBuffer()
          Obtain the internal FileBuffer to read into it.
protected  RamBuffer getRamBuffer()
          Obtain the internal RamBuffer to read into it.
 long getSize()
          Returns the size (in bytes) of the data into the buffer.
 boolean isLocked()
          return false if the buffer is still appendable
 void lock()
          Sets the buffer as ReadOnly.. no more data can be written into it.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SmartBuffer

public SmartBuffer()
Create a smart buffer that can countain into RAM a maximum of 256Kb of data. If the data that comes into the buffer are more than this limit, the buffer will store the exceding data into a temporary file.


SmartBuffer

public SmartBuffer(int maxRamSize)
Create a smart buffer that can countain into RAM a specific amount of data. If the data that comes into the buffer are more than this limit, the buffer will store the exceding data into a temporary file.

Parameters:
maxRamSize -
Method Detail

appendBytes

public long appendBytes(byte[] data,
                        int toAppend)
                 throws java.io.IOException,
                        java.lang.IllegalStateException
Append the passed byte array to the buffer.

Specified by:
appendBytes in interface ByteBuffer
Parameters:
data - a byte array that countains the data to store
size - the number of bytes to copy.
Returns:
the total size of the buffer (total number of introduced bytes).
Throws:
IOException, - IllegalStateException
java.io.IOException
java.lang.IllegalStateException

appendByte

public long appendByte(int data)
                throws java.io.IOException,
                       java.lang.IllegalStateException
Append the passed byte to the buffer.

Specified by:
appendByte in interface ByteBuffer
Parameters:
data - an integer that rappresents the byte data.
Returns:
the total size of the buffer (total number of introduced bytes).
Throws:
IOException, - IllegalStateException
java.io.IOException
java.lang.IllegalStateException

lock

public void lock()
Sets the buffer as ReadOnly.. no more data can be written into it. If so, an IllegalStateException is raised.

Specified by:
lock in interface ByteBuffer

getSize

public long getSize()
Returns the size (in bytes) of the data into the buffer.

Specified by:
getSize in interface ByteBuffer
Returns:
the size of the buffer in bytes.

isLocked

public boolean isLocked()
return false if the buffer is still appendable

Specified by:
isLocked in interface ByteBuffer
Returns:
the current status.

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
This method clones the current object.
Because it uses the same buffers of its parents as storage area, the new clone will be locked.
..in other words the clone can't be modified.

Specified by:
clone in interface ByteBuffer
Overrides:
clone in class java.lang.Object
Returns:
a new and separate instance of the object.
Throws:
java.lang.CloneNotSupportedException - if the clone operation is not supported

getFileBuffer

protected FileBuffer getFileBuffer()
Obtain the internal FileBuffer to read into it.

Returns:
the filebuffer.

getRamBuffer

protected RamBuffer getRamBuffer()
Obtain the internal RamBuffer to read into it.

Returns:
the filebuffer.


Copyright © 2010. All Rights Reserved.