Package org.mmisw.orrclient.gwt.client.rpc

Examples of org.mmisw.orrclient.gwt.client.rpc.ReadFileResult


   * @param file The file to read
   * @return the result of the operation.
   * @throws Exception
   */
  private static ReadFileResult readFileWithConversionToUtf8(File file) throws Exception {
    ReadFileResult result = new ReadFileResult();
   
    try {
      String str = IOUtils.toString(new FileInputStream(file), "UTF-8");
      result.setLogInfo("OK: file can be read as UTF-8 (no conversion necessary)");
      result.setError("setting error to avoid unnecesary conversion");
      result.setContents(str);
      return result;

    }
    catch(Throwable ex) {
      result.setLogInfo("OK: file cannot be read as UTF-8 directly.");
    }
   
    byte[] bytes = IOUtils.toByteArray(new FileInputStream(file));
   
    Collection<String> charsets = Utf8Util.isUtf8(bytes);
   
    if ( charsets == null ) {
      // charsets == null means the bytes are good UTF-8, so this
      // should NOT happen.
      result.addLogInfo("OK: already in UTF-8.");
      result.setError("setting error to avoid unnecesary conversion");
      result.setContents(new String(bytes, "UTF-8"));
      return result;
    }
   
    result.addLogInfo("Charset of the file may be one of: " +charsets+ "\n");
    result.addLogInfo("Attempting conversiones..\n");
   
    for ( String charsetName : charsets ) {
      try {
        String outputStr = _asString(bytes, charsetName);
        result.addLogInfo("Conversion from " +charsetName+ ": OK.\n");
        result.setContents(outputStr);
        return result;
      }
      catch(CharacterCodingException ex) {
        // continue with the other possible charsets...
      }
    }
   
    result.setError("None of the conversions from the possible detected charsets "
        + " was successful: " +charsets
    );
    result.setContents(null);
    return result;
  }
View Full Code Here


    File file = new File(filename);
   
    File outFile = new File(file.getParent(), "utf8-" +file.getName());
    String outFilename = outFile.getPath();
   
    ReadFileResult result = readFileWithConversionToUtf8(file);
    System.out.println("readFileWithConversionToUtf8:");
    String error = result.getError();
    if ( error != null ) {
      System.out.println("error: " +result.getError());
    }
    System.out.println("logInfo:\n\t" +result.getLogInfo().replaceAll("\n", "\n\t"));
   
    if ( error == null ) {
      _writeStringTo(result.getContents(), outFilename);
      System.out.println("Output written to " +outFilename)
     
    }
  }
View Full Code Here

TOP

Related Classes of org.mmisw.orrclient.gwt.client.rpc.ReadFileResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.