Java InputStreamReader Create getReader(final File file)

Here you can find the source of getReader(final File file)

Description

Method getReader

License

Open Source License

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
IOException an exception

Return

TODO: Finish documentation

Declaration

public static Reader getReader(final File file) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w w  w  .j  av  a 2s.  co m
 * Copyright (c) 2003 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial concept and implementation
 */

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;

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

import java.io.Reader;

public class Main {
    /** Field ENCODING */
    static final String ENCODING = "encoding=";
    /** Empty byte array */
    static final byte NO_BYTES[] = new byte[0];

    /**
     * Method getReader
     *
     * @param file
     *
     * @return TODO: Finish documentation
     *
     * @throws IOException
     */
    public static Reader getReader(final File file) throws IOException {
        final FileInputStream fileinputstream = new FileInputStream(file);
        final byte bytes[] = readUpTo(fileinputstream, 100);
        fileinputstream.close();

        final String s = normalizeEncoding(getEncoding(bytes));
        return new InputStreamReader(new FileInputStream(file), s);
    }

    /**
     * Method getReader
     *
     * @param inputstream
     *
     * @return TODO: Finish documentation
     *
     * @throws IOException
     */
    public static Reader getReader(InputStream inputstream) throws IOException {
        if (!(inputstream instanceof BufferedInputStream)) {
            inputstream = new BufferedInputStream(inputstream);
        }

        inputstream.mark(100);

        final byte bytes[] = readUpTo(inputstream, 100);
        inputstream.reset();

        final String s = normalizeEncoding(getEncoding(bytes));
        return new InputStreamReader(inputstream, s);
    }

    /**
     * Method readUpTo
     *
     * @param inputstream
     * @param bytes
     * @param i
     * @param j
     *
     * @return TODO: Finish documentation
     *
     * @throws IOException
     */
    public static int readUpTo(final InputStream inputstream, final byte bytes[], final int i, final int j)
            throws IOException {
        int k = 0;

        do {
            if (k >= j) {
                break;
            }

            final int l = inputstream.read(bytes, i + k, j - k);

            if (l < 0) {
                break;
            }

            k += l;
        } while (true);

        return k;
    }

    /**
     * Method readUpTo
     *
     * @param inputstream
     * @param i
     *
     * @return TODO: Finish documentation
     *
     * @throws IOException
     */
    public static byte[] readUpTo(final InputStream inputstream, final int i) throws IOException {
        if (i <= 0) {
            return NO_BYTES;
        }

        byte bytes[] = new byte[i];
        final int j = readUpTo(inputstream, bytes, 0, i);

        if (j < i) {
            final byte morebytes[] = bytes;
            bytes = new byte[j];

            System.arraycopy(morebytes, 0, bytes, 0, j);
        }

        return bytes;
    }

    /**
     * Method normalizeEncoding
     *
     * @param encoding
     *
     * @return TODO: Finish documentation
     */
    public static String normalizeEncoding(final String encoding) {
        return ((encoding != null) && !encoding.equalsIgnoreCase("UTF-8")) ? encoding : "UTF8";
    }

    /**
     * Method getEncoding
     *
     * @param bytes
     *
     * @return TODO: Finish documentation
     */
    public static String getEncoding(final byte bytes[]) {
        if (isUTF16(bytes)) {
            return "UTF-16";
        }

        final String s = new String(bytes);
        final int i = s.indexOf(ENCODING);

        if (i == -1) {
            return null;
        } else {
            final int j = i + ENCODING.length() + 1;
            final char c = s.charAt(j - 1);
            final int k = s.indexOf(c, j);
            return s.substring(j, k);
        }
    }

    /**
     * Method isUTF16
     *
     * @param abyte0
     *
     * @return TODO: Finish documentation
     */
    public static boolean isUTF16(final byte abyte0[]) {
        if (abyte0.length < 2) {
            return false;
        } else {
            return ((abyte0[0] == -1) && (abyte0[1] == -2)) || ((abyte0[0] == -2) && (abyte0[1] == -1));
        }
    }
}

Related

  1. getReader(final InputStream is)
  2. getReader(String name, String extension)
  3. getReaderForFile(IFile file)
  4. readTextFile(File f)