Returns a byte order marker (BOM) for a specified character set. - Java java.lang

Java examples for java.lang:char

Description

Returns a byte order marker (BOM) for a specified character set.

Demo Code

/**/*www  .j a v a 2  s.c o m*/
 * Sapelli data collection platform: http://sapelli.org
 * 
 * Copyright 2012-2014 University College London - ExCiteS group
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
//package com.java2s;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;

public class Main {
    public static final Charset UTF8 = Charset.forName("UTF-8");
    public static final Charset UTF16LE = Charset.forName("UTF-16LE");
    public static final Charset UTF16BE = Charset.forName("UTF-16BE");
    public static final Charset UTF32LE = Charset.forName("UTF-32LE");
    public static final Charset UTF32BE = Charset.forName("UTF-32BE");
    public static final byte[] UTF8_BOM = new byte[] { (byte) 0xEF,
            (byte) 0xBB, (byte) 0xBF };
    public static final byte[] UTF16LE_BOM = new byte[] { (byte) 0xFF,
            (byte) 0xFE };
    public static final byte[] UTF16BE_BOM = new byte[] { (byte) 0xFE,
            (byte) 0xFF };
    public static final byte[] UTF32LE_BOM = new byte[] { (byte) 0xFF,
            (byte) 0xFE, (byte) 0x00, (byte) 0x00 };
    public static final byte[] UTF32BE_BOM = new byte[] { (byte) 0x00,
            (byte) 0x00, (byte) 0xFE, (byte) 0xFF };

    /**
     * Returns a byte order marker (BOM) for a specified character set.
     * 
     * @param charsetName
     * @return the BOM for the named charset, or null no matching BOM was found
     * @throws IllegalCharsetNameException if the specified charset name is illegal.
     * @throws UnsupportedCharsetException if the desired charset is not supported by this runtime.
     */
    public static byte[] getBom(String charsetName)
            throws IllegalCharsetNameException, UnsupportedCharsetException {
        return getBom(Charset.forName(charsetName));
    }

    /**
     * Returns a byte order marker (BOM) for a specified character set.
     * 
     * @param charset
     * @return the BOM for the given charset, or null no matching BOM was found
     */
    public static byte[] getBom(Charset charset) {
        if (UTF8.equals(charset))
            return UTF8_BOM;
        if (UTF16LE.equals(charset))
            return UTF16LE_BOM;
        if (UTF16BE.equals(charset))
            return UTF16BE_BOM;
        if (UTF32LE.equals(charset))
            return UTF32LE_BOM;
        if (UTF32BE.equals(charset))
            return UTF32BE_BOM;
        else
            return null;
    }
}

Related Tutorials