Java Array Remove removeByteOrderMark(final byte[] input)

Here you can find the source of removeByteOrderMark(final byte[] input)

Description

This method removes the Byte Order Mark (BOM) from an array of bytes.

License

Open Source License

Return

Returns a copy of the input array without the Byte Order Mark

Declaration

public static byte[] removeByteOrderMark(final byte[] input) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/* w w  w . jav a 2  s .c  om*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.util.Arrays;

public class Main {
    /**
     * This method removes the Byte Order Mark (BOM) from an array of bytes. The following Byte Order Marks of the
     * following encodings are checked and removed.
     * <ul>
     * <li>UTF-8
     * <li>UTF-16BE
     * <li>UTF-16LE
     * <li>UTF-32BE
     * <li>UTF-32LE
     * </ul>
     *
     * @return Returns a copy of the input array without the Byte Order Mark
     */
    public static byte[] removeByteOrderMark(final byte[] input) {
        if (input == null) {
            return null;
        }

        int skip = 0;

        // UTF-8
        if (input.length >= 3 && (input[0] == (byte) 0xEF) && (input[1] == (byte) 0xBB)
                && (input[2] == (byte) 0xBF)) {
            skip = 3;
        }

        // UTF-16BE
        else if (input.length >= 2 && (input[0] == (byte) 0xFE) && (input[1] == (byte) 0xFF)) {
            skip = 2;
        }

        // UTF-16LE
        else if (input.length >= 4 && (input[0] == (byte) 0xFF) && (input[1] == (byte) 0xFE)
                && (input[2] != (byte) 0x00) && (input[3] != (byte) 0x00)) {
            skip = 2;
        }

        // UTF-32BE
        else if (input.length >= 4 && (input[0] == (byte) 0x00) && (input[1] == (byte) 0x00)
                && (input[2] == (byte) 0xFE) && (input[3] == (byte) 0xFF)) {
            skip = 4;
        }

        // UTF-32LE
        else if (input.length >= 4 && (input[0] == (byte) 0xFF) && (input[1] == (byte) 0xFE)
                && (input[2] == (byte) 0x00) && (input[3] == (byte) 0x00)) {
            skip = 4;
        }

        if (skip > 0) {
            return Arrays.copyOfRange(input, skip, input.length);
        }
        return input;
    }
}

Related

  1. removeAll(T[] array, Collection toRemove)
  2. removeAll(T[] items, T item)
  3. RemoveArgs(String[] args, int startIndex)
  4. removeAt(T[] array, int index)
  5. removeByPrefix(String[] array, String prefix)
  6. removeCommonWords(String[] words)
  7. removeElement(final E[] array, final int index)
  8. removeElement(Object[] array, Object entryToRemove)
  9. removeElement(String array[], String element, int occurrences)