
package uk.co.wingpath.io;

import java.io.*;

/**
* This class extends {@link BufferedReader} for reading UTF-8 encoded files.
* It skips any BOM (Byte Order Marker) that may have been inserted by
* Microsoft utilities at the start of the file.
*/

public class Utf8Reader
    extends BufferedReader
{
    private static int Utf8BOM = '\uFEFF';

    private static Reader getReader (File file)
        throws FileNotFoundException
    {
        try
        {
            return new InputStreamReader (new FileInputStream (file), "UTF-8");
        }
        catch (UnsupportedEncodingException e)
        {
            // Shouldn't happen.
            assert false;
            return null;
        }
    }

    /**
    * Constructs a Utf8Reader for reading the specified file.
    * @param file the file to be read.
    * @throws IOException if the file does not exist,
    * is a directory rather than a regular file,
    * or for some other reason cannot be opened for reading.
    */
    public Utf8Reader (File file)
        throws IOException
    {
        super (getReader (file));
        mark (1);
        int c = read ();
        if (c != Utf8BOM)
            reset ();
    }
}
