Java File Read by Charset bomStream(String charset, String file)

Here you can find the source of bomStream(String charset, String file)

Description

Convert stream to ByteArrayInputStream by given character set.

License

Open Source License

Parameter

Parameter Description
charset target character set.
file a file that contains no BOM head content.

Exception

Parameter Description
IOException I/O operation failed or unsupported character set.

Return

a ByteArrayInputStream contains BOM heads and bytes in original stream

Declaration

public static InputStream bomStream(String charset, String file) throws IOException 

Method Source Code


//package com.java2s;
/*//from   ww w. j av  a  2s  .c o  m
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Files;
import java.nio.file.Paths;

import java.util.HashMap;

import java.util.Map;

import java.util.stream.Collectors;

public class Main {
    /**
     * BOM table for storing BOM header.
     */
    private final static Map<String, byte[]> bom = new HashMap();

    /**
     * Convert stream to ByteArrayInputStream by given character set.
     * @param charset target character set.
     * @param file a file that contains no BOM head content.
     * @return a ByteArrayInputStream contains BOM heads and bytes in original
     *         stream
     * @throws IOException I/O operation failed or unsupported character set.
     */
    public static InputStream bomStream(String charset, String file) throws IOException {
        String localCharset = charset;
        if (charset.equals("UTF-16") || charset.equals("UTF-32")) {
            localCharset += ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN ? "BE" : "LE";
        }
        if (!bom.containsKey(localCharset))
            throw new UnsupportedCharsetException("Charset:" + localCharset);

        byte[] content = Files.readAllLines(Paths.get(file)).stream().collect(Collectors.joining())
                .getBytes(localCharset);
        byte[] head = bom.get(localCharset);
        ByteBuffer bb = ByteBuffer.allocate(content.length + head.length);
        bb.put(head);
        bb.put(content);
        return new ByteArrayInputStream(bb.array());
    }
}

Related

  1. copyFileWithEolConversion(File inFile, File outFile, Charset charset)
  2. detectCharset(File f, String[] charsets)
  3. doTranseFileCharset(File srcFile, File destFile, String srcCharsetName, String destCharsetName)
  4. fileContent(File file, Charset charset)