Esempio n. 1
0
  public static String readFully(InputStream is, Charset cs) throws IOException {

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    StreamPump.doPump(is, os);

    if (cs == null) cs = Charset.defaultCharset();

    return new String(os.toByteArray(), cs);
  }
Esempio n. 2
0
  public static String readUnicodeFile(File f) throws IOException {
    ByteArrayOutputStream bais = new ByteArrayOutputStream();

    FileInputStream fis = new FileInputStream(f);
    StreamPump.doPump(fis, bais, false);
    fis.close();

    bais.close();

    byte[] barr = bais.toByteArray();
    String enc = "UTF-8";

    if (barr.length >= 2 && (barr[0] == -1 && barr[1] == -2) || (barr[0] == -2 && barr[1] == -1))
      enc = "UTF-16";

    return new String(bais.toByteArray(), enc);
  }