I am looking for a utility method or constant in Java that will return me the bytes that correspond to the appropriate byte order mark for an encoding, but I can't seem to find one. Is there one? I really would like to do something like:
byte[] bom = Charset.forName( CharEncoding.UTF8 ).getByteOrderMark();
Where CharEncoding
comes from Apache Commons.
Apache Commons IO contains what you are looking for, see org.apache.commons.io.ByteOrderMark
.
You can generate the BOM like this:
byte[] utf8_bom = "\uFEFF".getBytes("UTF-8");
byte[] utf16le_bom = "\uFEFF".getBytes("UnicodeLittleUnmarked");
If you wish to create the BOMs for other encodings using this method, make sure you use the version of the encoding that does not automatically insert the BOM or it will be repeated. This technique only applies to Unicode encodings and will not produce meaningful results for others (like Windows-1252).
There isn't anything in the JDK as far as I can see, nor any of the Apache projects.
Eclipse EMF has an Enum however that provides support:
org.eclipse.emf.ecore.resource.ContentHandler.ByteOrderMark
I'm not sure whether that's of any help to you?
There's some more info here on the various BOM's for each encoding type, you could write a simple helper class or enum for this...
http://mindprod.com/jgloss/bom.html
Hope that helps. I'm surprised this isn't in Commons I/O to be honest.
It worth noting that many encodings don't use any byte order marks. e.g. an empty string in UTF-8 is just an empty byte[]. While there is a BOM specified for UTF-8 it is rarely used in Java and is not always supported.
来源:https://stackoverflow.com/questions/712004/does-java-have-methods-to-get-the-various-byte-order-marks