Java File Read by Charset isEqual(final File document, final Charset a, final Charset b)

Here you can find the source of isEqual(final File document, final Charset a, final Charset b)

Description

Tests, whether the content of the given file is identical at character level, when it is opened with both different Charsets.

License

Open Source License

Parameter

Parameter Description
document the file to test.
a the first character set to interpret the document in.
b the 2nd character set to interpret the document in.

Exception

Parameter Description
IOException if something goes wrong.

Return

true if both files have all equal contents if they are interpreted as character data in both given encodings (they may differ at binary level if both charsets are different).

Declaration

public static boolean isEqual(final File document, final Charset a, final Charset b) throws IOException 

Method Source Code

//package com.java2s;
/*//  w w  w  .  j  av  a 2  s. co m
 * FileUtil.java, helpers for disk I/O.
 * Copyright (C) 2001 - 2011 Achim Westermann.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * If you modify or optimize the code in a useful way please let me know.
 * Achim.Westermann@gmx.de
 */

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

import java.io.IOException;

import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Main {
    /**
     * Tests, whether the content of the given file is identical at character
     * level, when it is opened with both different Charsets.
     * <p>
     * This is most often the case, if the given file only contains ASCII codes
     * but may also occur, when both codepages cover common ranges and the
     * document only contains values m_out of those ranges (like the EUC-CN
     * charset contains all mappings from BIG5).
     * <p>
     * 
     * @param document
     *          the file to test.
     * 
     * @param a
     *          the first character set to interpret the document in.
     * 
     * @param b
     *          the 2nd character set to interpret the document in.
     * 
     * @throws IOException
     *           if something goes wrong.
     * 
     * @return true if both files have all equal contents if they are interpreted
     *         as character data in both given encodings (they may differ at
     *         binary level if both charsets are different).
     */
    public static boolean isEqual(final File document, final Charset a, final Charset b) throws IOException {
        boolean ret = true;
        FileInputStream aIn = null;
        FileInputStream bIn = null;
        InputStreamReader aReader = null;
        InputStreamReader bReader = null;
        try {
            aIn = new FileInputStream(document);
            bIn = new FileInputStream(document);
            aReader = new InputStreamReader(aIn, a);
            bReader = new InputStreamReader(bIn, b);
            int readA = -1;
            int readB = -1;
            do {
                readA = aReader.read();
                readB = bReader.read();
                if (readA != readB) {
                    // also the case, if one is at the end earlier...
                    ret = false;
                    break;
                }
            } while ((readA != -1) && (readB != -1));
            return ret;
        } finally {
            if (aReader != null) {
                aReader.close();
            }
            if (bReader != null) {
                bReader.close();
            }
        }
    }
}

Related

  1. getPatchFileCharset()
  2. getPropertiesVaule(File file, String key, Charset charset)
  3. getString(File file, Charset charset)
  4. getUncommentedLines(File file, Charset forName)
  5. getZipFile(File src, Charset charset)
  6. load(File file, Charset charset)
  7. loadFile(File file, Charset cs)
  8. loadFile(File logfile, Charset charset)
  9. loadString(File f, Charset charset)