Here you can find the source of createBufferedReader( InputStream inputStream, String strCodepage)
public static BufferedReader createBufferedReader( InputStream inputStream, String strCodepage) throws UnsupportedEncodingException
//package com.java2s; //License from project: LGPL import java.io.*; import javax.swing.JOptionPane; import java.awt.Component; public class Main { private static String WARNING = "[!]"; private static String WARN_ENCODING = "[!]"; private static boolean s_bWasUtf8Reader = false; public static BufferedReader createBufferedReader( InputStream inputStream, String strCodepage) throws UnsupportedEncodingException { return createBufferedReader(inputStream, strCodepage, false, null); }/*from w ww .j a v a 2s .c o m*/ /** * @returns a BufferedReader from the given InputStream. Additionally if * a codepage string is given the Reader does the corresponding conversion; * if no codepage is given the default one (Windows) or 1252 (Linux) * is used. * If the given encoding is not supported and a parent component is given * a warning message will be displayed. */ public static BufferedReader createBufferedReader( InputStream inputStream, String strCodepage, boolean bShowWarning, Component parentComponent) throws UnsupportedEncodingException { s_bWasUtf8Reader = false; InputStream inInternal = inputStream; try { // if the file has an utf-8 bom then strCodepage is overwritten // BufferedInputStream bis = new BufferedInputStream(inputStream); bis.mark(3); byte[] aBom = new byte[3]; int iRead = bis.read(aBom); if (iRead > 0) { if (iRead == 3 && (aBom[0] & 0xff) == 0xef && (aBom[1] & 0xff) == 0xbb && (aBom[2] & 0xff) == 0xbf) strCodepage = "utf-8"; else bis.reset(); } inInternal = bis; } catch (IOException exc) { exc.printStackTrace(); // severe error, but it will also happen elsewhere and be handled there } InputStreamReader isr = null; try { if (strCodepage != null && (strCodepage.startsWith("Cp") || strCodepage.startsWith("utf") || strCodepage .startsWith("UTF"))) { isr = new InputStreamReader(inInternal, strCodepage); } else { // prior to 1.7.0.p3 there is no CODEPAGE tag boolean bIsWindows = File.separator.equals("\\"); if (bIsWindows) isr = new InputStreamReader(inInternal); else isr = new InputStreamReader(inInternal, "Cp1252"); // otherwise defaults to "utf-8" with linux } } catch (UnsupportedEncodingException exc) { exc.printStackTrace(); isr = new InputStreamReader(inInternal, "ISO8859_1"); } if (strCodepage != null && bShowWarning) { String strEncoding = isr.getEncoding(); if (!EqualsEncodings(strEncoding, strCodepage)) { JOptionPane .showMessageDialog(parentComponent, WARN_ENCODING, WARNING, JOptionPane.WARNING_MESSAGE); } } if (IsUtf8Encoding(isr.getEncoding())) s_bWasUtf8Reader = true; else s_bWasUtf8Reader = false; return new BufferedReader(isr); } private static boolean EqualsEncodings(String strEncoding1, String strEncoding2) { strEncoding1 = strEncoding1.toLowerCase(); strEncoding2 = strEncoding2.toLowerCase(); if (strEncoding1.equals(strEncoding2)) return true; else { if (strEncoding1.startsWith("utf") && strEncoding2.startsWith("utf")) { // remove the first "-" in the strings if (strEncoding1.indexOf('-') > -1) strEncoding1 = strEncoding1.substring(0, strEncoding1.indexOf('-')) + strEncoding1.substring(strEncoding2 .indexOf('-') + 1); if (strEncoding2.indexOf('-') > -1) strEncoding2 = strEncoding2.substring(0, strEncoding2.indexOf('-')) + strEncoding2.substring(strEncoding2 .indexOf('-') + 1); if (strEncoding1.equals(strEncoding2)) return true; } } return false; } private static boolean IsUtf8Encoding(String strEncoding) { strEncoding = strEncoding.toLowerCase(); if (strEncoding.equals("utf8") || strEncoding.equals("utf-8")) return true; else return false; } }