info.monitorenter.cpdetector.io
Class CodepageDetectorProxy

java.lang.Object
  extended by info.monitorenter.cpdetector.io.AbstractCodepageDetector
      extended by info.monitorenter.cpdetector.io.CodepageDetectorProxy
All Implemented Interfaces:
ICodepageDetector, Serializable, Comparable

public final class CodepageDetectorProxy
extends AbstractCodepageDetector

A proxy that delegate the codepage detection to all it's delegates. The first one (added in code-order) that does not return a null Charsetfrom it's delegate method detectCodepage(URL)wins the race and determines the codpage of the document specified by the given URL.

If an underlying ICodepageDetectorthrows an IOException, the delegation search will be terminated by throwing this exception.

Author:
Achim Westermann
See Also:
Serialized Form

Method Summary
 boolean add(ICodepageDetector detector)
          Adds the given instance to this proxie's detection capability.
 Charset detectCodepage(InputStream in, int length)
           Detects the codepage by iteratively delegating the call to all internal ICodepageDetector instances added by add(ICodepageDetector).
 Charset detectCodepage(URL url)
          Delegates to ICodepageDetector.detectCodepage(java.io.InputStream, int) with a buffered input stream.
static CodepageDetectorProxy getInstance()
          Singleton retrieval method.
 String toString()
           
 
Methods inherited from class info.monitorenter.cpdetector.io.AbstractCodepageDetector
compareTo, open
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getInstance

public static CodepageDetectorProxy getInstance()
Singleton retrieval method.

Be sure to configure the instance returned at a single location in your code to avoid unpredictable application - wide side effects.

Returns:
the sole instance in this VM.

add

public boolean add(ICodepageDetector detector)
Adds the given instance to this proxie's detection capability.

Remember that the order of added ICodepageDetector instances is important for the internal delegation (see class description).


detectCodepage

public Charset detectCodepage(URL url)
                       throws IOException
Description copied from class: AbstractCodepageDetector
Delegates to ICodepageDetector.detectCodepage(java.io.InputStream, int) with a buffered input stream.

Specified by:
detectCodepage in interface ICodepageDetector
Overrides:
detectCodepage in class AbstractCodepageDetector
Parameters:
url - Should link to a file containing textual document. No check for images or other resources is made.
Returns:
null, if the codepage of the document specified by the given URL was not detected or the Charsetthat represents the document's codepage.
Throws:
IOException - If a problem with the url - handling occurs.
See Also:
ICodepageDetector.detectCodepage(URL)

detectCodepage

public Charset detectCodepage(InputStream in,
                              int length)
                       throws IOException,
                              IllegalArgumentException

Detects the codepage by iteratively delegating the call to all internal ICodepageDetector instances added by add(ICodepageDetector).

The given InputStream has to support mark such that the call InputStream.mark(int) with argument length does not throw an exception. This is needed, as the stream has to be resetted to the beginning for each internal delegate that tries to detect.

If this is impossible (large documents), prefer using detectCodepage(URL).

Parameters:
in - An InputStream for the document, that supports mark and a readlimit of argument length.
length - The amount of bytes to take into account. This number shouls not be longer than the amount of bytes retrievable from the InputStream but should be as long as possible to give the fallback detection (chardet) more hints to guess.
Throws:
IllegalArgumentException - if more bytes had to be read from the input stream than param length or the given input stream does not support marking.
IOException
See Also:
cpdetector.io.ICodepageDetector#detectCodepage(java.io.InputStream, int length)

toString

public String toString()
Overrides:
toString in class Object
See Also:
Object.toString()


Copyleft ㊢ 2003-2004 MPL 1.1, All Rights Footloose.