|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object eu.medsea.util.EncodingGuesser
public class EncodingGuesser
This class contains a list of known encodings used by TextMimeType. It is used by the TextMimeDetector but can be used as a stand alone utility class in other parts of your program if you want.
The getPossibleEncodings() method takes a byte [] as its source and the bigger the array the better the detection ratio will be.
The class is initialised with an empty list of encodings so it is effectively disabled by default. You can set the supported encodings to ALL of the encodings supported by your JVM at any point during your program execution using the following method EncodingGuesser.setSupportedEncodings(EncodingGuesser.getCanonicalEncodingNamesSupportedByJVM()); You can also clear the encodings and disable the detector at any point by calling EncodingGuesser.setSupportedEncodings(new ArrayList()). If later on you dynamically add more encodings they will NOT be detected automatically by this class but you can recall the above method.
As the JVM can have a large number of encodings and each one is checked against the byte array it may be wise to remove all encodings you are sure you will not use to trim down on the number of tests. It will not stop at the first match but will try to match as many encodings as possible and return this as a Collection.
A common scenario is where an application can handle only a small set of text encodings such as UTF-8 and windows-1252. If this is your case you can use the setSupportedEncodings() method so that these are the only encodings in the supported encodings Collection. This will dramatically improve the performance of this class.
It's possible that small byte arrays that should contain binary data are considered possible text matches but generally binary data, such as images, should return no matches.
There are some optimisations that are applicable to text files containing BOM's (Byte Order Marks) such as UTF-8, UTF-16LE, UTF-16BE, UTF-32LE and UTF-32BE. These are not required but if present will greatly improve the resultant possible matches returned from the getPossibleEncodings() method.
Constructor Summary | |
---|---|
EncodingGuesser()
|
Method Summary | |
---|---|
static boolean |
compareByteArrays(byte[] a,
int aOffset,
byte[] b,
int bOffset,
int length)
Utility method to compare a region of two byte arrays for equality |
static byte[] |
getByteArraySubArray(byte[] a,
int offset,
int length)
Get a sub array of this byte array starting at offset until length |
static Collection |
getCanonicalEncodingNamesSupportedByJVM()
Utility method to get all of the current encoding names, in canonical format, supported by your JVM at the time this is called. |
static String |
getDefaultEncoding()
Get the JVM default canonical encoding. |
static int |
getLengthBOM(String encoding,
byte[] data)
Get the length of a BOM for this this encoding and byte array |
static Collection |
getPossibleEncodings(byte[] data)
Get a Collection of all the possible encodings this byte array could be used to represent. |
static Collection |
getSupportedEncodings()
Get the Collection of currently supported encodings |
static Collection |
getValidEncodings(String[] encodings)
Get a Collection containing entries in both the supported encodings and the passed in String [] of encodings. |
static boolean |
isKnownEncoding(String encoding)
Check if the encoding String is one of the encodings supported. |
static boolean |
removeEncoding(String encoding)
Allows you to remove an encoding from the supported encodings you are not interested in. |
static boolean |
removeEncodings(String[] encodings)
Remove all valid encodings in the string array |
static Collection |
setSupportedEncodings(Collection encodings)
Set the supported encodings |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public EncodingGuesser()
Method Detail |
---|
public static boolean isKnownEncoding(String encoding)
encoding
-
public static Collection getPossibleEncodings(byte[] data)
data
-
public static boolean removeEncoding(String encoding)
encoding
-
public static boolean removeEncodings(String[] encodings)
encodings
- String [] containing the encodings to remove
public static Collection getValidEncodings(String[] encodings)
encodings
-
public static String getDefaultEncoding()
public static Collection getSupportedEncodings()
public static Collection setSupportedEncodings(Collection encodings)
encodings.
- If this is null the supported encodings are left unchanged.
public static int getLengthBOM(String encoding, byte[] data)
encoding
- data
-
public static byte[] getByteArraySubArray(byte[] a, int offset, int length)
a
- offset
- length
-
public static boolean compareByteArrays(byte[] a, int aOffset, byte[] b, int bOffset, int length)
a
- aOffset
- b
- bOffset
- length
-
public static Collection getCanonicalEncodingNamesSupportedByJVM()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |