com.octo.captcha.engine.bufferedengine.buffer
Class DiskCaptchaBuffer

java.lang.Object
  extended by com.octo.captcha.engine.bufferedengine.buffer.DiskCaptchaBuffer
All Implemented Interfaces:
CaptchaBuffer

public class DiskCaptchaBuffer
extends java.lang.Object
implements CaptchaBuffer

Simple implmentation of a disk captcha buffer


Constructor Summary
DiskCaptchaBuffer(java.lang.String fileName, boolean persistant)
          Constructor for a disk captcha buffer
 
Method Summary
 float calculateDataFileSparseness()
          The design of the layout on the data file means that there will be small gaps created when DiskElements are reused.
 void clear()
          Clear the buffer from all locale
 void clearFile()
          Removes all cached items from the cache.
 void dispose()
          Shuts down the disk store in preparation for cache shutdown

If a VM crash happens, the shutdown hook will not run.

 java.lang.String getDataFileName()
           
 long getDataFileSize()
           
 java.lang.String getIndexFileName()
           
 long getIndexFileSize()
           
 java.util.Collection getLocales()
          Get all the locales used
 long getTotalFileSize()
           
 long getUsedDataSize()
          When elements are deleted, spaces are left in the file.
 int maxSize()
           
 void putAllCaptcha(java.util.Collection captchas)
          Put a collection of captchas with the default locale
 void putAllCaptcha(java.util.Collection captchas, java.util.Locale locale)
          Put a collection of captchas with his locale
 void putCaptcha(Captcha captcha)
          Put a captcha with default laocale
 void putCaptcha(Captcha captcha, java.util.Locale locale)
          Put a captcha with a locale
protected  java.util.Collection remove(int number, java.util.Locale locale)
          Gets an entry from the Disk Store.
 Captcha removeCaptcha()
          remove a captcha from the buffer
 java.util.Collection removeCaptcha(int number)
          Remove a precise number of captcha
 java.util.Collection removeCaptcha(int number, java.util.Locale locale)
          Remove a precise number of captcha with a locale
 Captcha removeCaptcha(java.util.Locale locale)
          remove a captcha from the buffer corresponding to the locale
 int size()
          Get the size of the buffer for all locales
 int size(java.util.Locale locale)
          Get the size of the buffer for a locale
protected  void store(java.util.Collection collection, java.util.Locale locale)
          Puts items into the store.
protected  void store(java.lang.Object element, java.util.Locale locale)
          Puts items into the store.
 java.lang.String toString()
          Returns a Stringrepresentation of the DiskCaptchaBuffer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DiskCaptchaBuffer

public DiskCaptchaBuffer(java.lang.String fileName,
                         boolean persistant)
Constructor for a disk captcha buffer

Parameters:
fileName - like c:/temp/name
persistant - If the disk buffer is persistant, it will try to load from file name .data et .index existing data
Method Detail

remove

protected java.util.Collection remove(int number,
                                      java.util.Locale locale)
                               throws java.io.IOException
Gets an entry from the Disk Store.

Returns:
The element
Throws:
java.io.IOException

store

protected void store(java.util.Collection collection,
                     java.util.Locale locale)
              throws java.io.IOException
Puts items into the store.

Throws:
java.io.IOException

store

protected void store(java.lang.Object element,
                     java.util.Locale locale)
              throws java.io.IOException
Puts items into the store.

Throws:
java.io.IOException

clearFile

public void clearFile()
               throws java.io.IOException
Removes all cached items from the cache.

Throws:
java.io.IOException

dispose

public void dispose()
Shuts down the disk store in preparation for cache shutdown

If a VM crash happens, the shutdown hook will not run. The data file and the index file will be out of synchronisation. At initialisation we always delete the index file after we have read the elements, so that it has a zero length. On a dirty restart, it still will have and the data file will automatically be deleted, thus preserving safety.

Specified by:
dispose in interface CaptchaBuffer

toString

public java.lang.String toString()
Returns a Stringrepresentation of the DiskCaptchaBuffer

Overrides:
toString in class java.lang.Object

getTotalFileSize

public long getTotalFileSize()
Returns:
the total size of the data file and the index file, in bytes.

getDataFileSize

public long getDataFileSize()
Returns:
the size of the data file in bytes.

calculateDataFileSparseness

public float calculateDataFileSparseness()
The design of the layout on the data file means that there will be small gaps created when DiskElements are reused.

Returns:
the sparseness, measured as the percentage of space in the Data File not used for holding data

getUsedDataSize

public long getUsedDataSize()
When elements are deleted, spaces are left in the file. These spaces are tracked and are reused when new elements need to be written.

This method indicates the actual size used for data, excluding holes. It can be compared with getDataFileSize()as a measure of fragmentation.


getIndexFileSize

public long getIndexFileSize()
Returns:
the size of the index file, in bytes.

getDataFileName

public java.lang.String getDataFileName()
Returns:
the file name of the data file where the disk store stores data, without any path information.

getIndexFileName

public java.lang.String getIndexFileName()
Returns:
the file name of the index file, which maintains a record of elements and their addresses on the data file, without any path information.

removeCaptcha

public Captcha removeCaptcha()
                      throws java.util.NoSuchElementException
Description copied from interface: CaptchaBuffer
remove a captcha from the buffer

Specified by:
removeCaptcha in interface CaptchaBuffer
Returns:
a captcha
Throws:
java.util.NoSuchElementException - if there is no captcha throw NoSuchElementException
See Also:
CaptchaBuffer.removeCaptcha()

removeCaptcha

public java.util.Collection removeCaptcha(int number)
Description copied from interface: CaptchaBuffer
Remove a precise number of captcha

Specified by:
removeCaptcha in interface CaptchaBuffer
Parameters:
number - The number of captchas to remove
Returns:
a collection of captchas
See Also:
CaptchaBuffer.removeCaptcha(int)

putCaptcha

public void putCaptcha(Captcha captcha)
Description copied from interface: CaptchaBuffer
Put a captcha with default laocale

Specified by:
putCaptcha in interface CaptchaBuffer
See Also:
CaptchaBuffer.putCaptcha(com.octo.captcha.Captcha)

putAllCaptcha

public void putAllCaptcha(java.util.Collection captchas)
Description copied from interface: CaptchaBuffer
Put a collection of captchas with the default locale

Specified by:
putAllCaptcha in interface CaptchaBuffer
Parameters:
captchas - The captchas to add
See Also:
CaptchaBuffer.putAllCaptcha(java.util.Collection)

size

public int size()
Description copied from interface: CaptchaBuffer
Get the size of the buffer for all locales

Specified by:
size in interface CaptchaBuffer
Returns:
The size of the buffer
See Also:
CaptchaBuffer.size()

maxSize

public int maxSize()
See Also:
com.octo.captcha.engine.bufferedengine.buffer.CaptchaBuffer#maxSize()

removeCaptcha

public Captcha removeCaptcha(java.util.Locale locale)
                      throws java.util.NoSuchElementException
Description copied from interface: CaptchaBuffer
remove a captcha from the buffer corresponding to the locale

Specified by:
removeCaptcha in interface CaptchaBuffer
Parameters:
locale - The locale the catcha to remove
Returns:
a captcha correponding to the locale
Throws:
java.util.NoSuchElementException - if there is no captcha throw NoSuchElementException
See Also:
CaptchaBuffer.removeCaptcha(java.util.Locale)

removeCaptcha

public java.util.Collection removeCaptcha(int number,
                                          java.util.Locale locale)
Description copied from interface: CaptchaBuffer
Remove a precise number of captcha with a locale

Specified by:
removeCaptcha in interface CaptchaBuffer
Parameters:
number - The number of captchas to remove
locale - The locale of the removed captchas
Returns:
a collection of captchas
See Also:
CaptchaBuffer.removeCaptcha(int, java.util.Locale)

putCaptcha

public void putCaptcha(Captcha captcha,
                       java.util.Locale locale)
Description copied from interface: CaptchaBuffer
Put a captcha with a locale

Specified by:
putCaptcha in interface CaptchaBuffer
Parameters:
captcha - The captcha to add
locale - the locale of the captcha
See Also:
CaptchaBuffer.putCaptcha(com.octo.captcha.Captcha, java.util.Locale)

putAllCaptcha

public void putAllCaptcha(java.util.Collection captchas,
                          java.util.Locale locale)
Description copied from interface: CaptchaBuffer
Put a collection of captchas with his locale

Specified by:
putAllCaptcha in interface CaptchaBuffer
Parameters:
captchas - The captchas to add
locale - The locale of the captchas
See Also:
CaptchaBuffer.putAllCaptcha(java.util.Collection, java.util.Locale)

size

public int size(java.util.Locale locale)
Description copied from interface: CaptchaBuffer
Get the size of the buffer for a locale

Specified by:
size in interface CaptchaBuffer
Parameters:
locale - the locale to get the size
Returns:
The size of the buffer
See Also:
CaptchaBuffer.size(java.util.Locale)

clear

public void clear()
Description copied from interface: CaptchaBuffer
Clear the buffer from all locale

Specified by:
clear in interface CaptchaBuffer
See Also:
CaptchaBuffer.clear()

getLocales

public java.util.Collection getLocales()
Description copied from interface: CaptchaBuffer
Get all the locales used

Specified by:
getLocales in interface CaptchaBuffer
See Also:
CaptchaBuffer.getLocales()


Copyright 2003-2009 JCaptcha. All Rights Reserved.