/*******************************************************************************
 * Copyright (c) 2015 Kaloyan Raev.
 * 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:
 *    Kaloyan Raev - initial API and implementation
 *******************************************************************************/
package org.eclipselabs.dte;

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

import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;

public class TextFileDetector {

    /**
     * Checks if a file is text or binary.
     * 
     * @param is
     *            input stream of the file
     * @return <code>true</code> if the file is more likely to be text file than
     *         binary file, <code>false</code> otherwise.
     * @throws IOException
     */
    public static boolean isTextFile(InputStream is) throws IOException {
        CharsetDetector detector = new CharsetDetector();
        detector.setText(is);
        CharsetMatch match = detector.detect();

        // The file is text if a charset with confidence of at least 10 (out
        // of 100) is detected. Empty files have confidence 10 for UTF-8.
        return match.getConfidence() >= 10;
    }

}

@@ -677,16 +677,82 @@
 Match;%0A%0A
+/**%0A * Utility class for detecting if file is text or binary.%0A */%0A
 public c
@@ -835,16 +835,182 @@
      * %0A
+     * %3Cp%3E%0A     * The provided input stream will not be closed and the position in the%0A     * stream will be unchanged after leaving this method.%0A     * %3C/p%3E%0A     * %0A
      * @
@@ -1200,16 +1200,16 @@
 erwise.%0A
-
      * @
@@ -1227,16 +1227,82 @@
 ception%0A
+     *             in case of error while reading from the stream%0A
      */%0A

Improve Javadoc in TextFileDetector