org.random.rjgodoy.trng
Class MH_HttpClient

java.lang.Object
  extended by org.random.rjgodoy.trng.MH_HttpClient

public class MH_HttpClient
extends Object

Mini HTTP-Client for accessing www.random.org.

This class accesses the following services:

 The Quota Service
http://www.random.org/quota?format=plain
 The Random Integer Generator
/integers/?min=-128&max=127&base=10&col=1000000000&format=plain&num= number of bytes
The initialization process uses several system properties for configuration (see MH_SecureRandom).

Note: this class has package-private scope because crypto-related helper classes in a provider package should have package-private scope. Besides, instances of this class are NOT thread safe (there is only one instance of this class, and it use is controlled by a lock in MH_SecureRandomSpi).

TODO: It should be possible to configure which cipher-suite will be used.

Author:
Javier Godoy

Field Summary
static String JVM
           
static String VERSION
           
 
Constructor Summary
MH_HttpClient()
          Initializes an instance of MH_HttpClient.
 
Method Summary
(package private)  long checkQuota()
          Query and returns the available quota.
(package private)  void closeSocket()
          Closes the socket used by this instance.
(package private)  long estimateQuota()
          Returns an estimated quota (avoids requesting the actual quota if it is already known to be positive).
protected  void finalize()
          Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
(package private)  int nextBytes(byte[] bytes, int offset, int length)
          Fetches a block of bytes from www.random.org web-service.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JVM

public static final String JVM

VERSION

public static final String VERSION
See Also:
Constant Field Values
Constructor Detail

MH_HttpClient

MH_HttpClient()
Initializes an instance of MH_HttpClient. The initialization process uses several system properties for configuration (see MH_SecureRandom).

Method Detail

estimateQuota

long estimateQuota()
Returns an estimated quota (avoids requesting the actual quota if it is already known to be positive). If the estimated quota (calculated as the last known quota minus accumulated bit consumption) is positive then the estimated value is immediatly returned, else the actual quota is requested. As a consequence, it is guarranteed that:

Returns:
The estimated quota, or Long.MIN_VALUE if an error occurs.

checkQuota

long checkQuota()
Query and returns the available quota.

If this method fails (because a server or protocol error) returns Long.MIN_VALUE. If this method was successful, the estimated quota is updated and accumulated bit consumtion is reset.

Returns:
The actual quota, or Long.MIN_VALUE if an error occurs.

nextBytes

int nextBytes(byte[] bytes,
              int offset,
              int length)
Fetches a block of bytes from www.random.org web-service.

If this method is successful, it should return length. If this method fails because of a service error (e.g.: the service is unavailable, the service has changed its interface, etc) it returns 0 (i.e. no bytes were read). If this method fails because an error when parsing the results (e.g.: no trailng TAB character) it returns the amount of bytes actually written to bytes buffer.

Parameters:
bytes - destination buffer
offset - offset at which to start storing bytes
length - maximum number of bytes to read.
Returns:
The number of bytes read.
Throws:
ArrayIndexOutOfBoundsException - if any offset or length is negative, if offset is greater than bytes.length or if length+offset is greater than bytes.length.
NullPointerException - if bytes is null.

closeSocket

void closeSocket()
Closes the socket used by this instance. Subsequent calls to checkQuota() and nextBytes(byte[], int, int) will open a new socket.


finalize

protected void finalize()
                 throws Throwable
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. Closes the socket upon finalization.

Overrides:
finalize in class Object
Throws:
Throwable


Submit a bug or request a featureCopyright © (2008-2011) Roberto Javier Godoy
Project Web Hosted by SourceForge.netSouceForge.net is registered trademark of SourceForge, Inc., in the United States and other countries.