Search in sources :

Example 86 with Charset

use of java.nio.charset.Charset in project android by JetBrains.

the class GradleImport method copyTextFile.

public void copyTextFile(@Nullable ImportModule module, @NonNull File source, @NonNull File dest) throws IOException {
    assert isTextFile(source) : source;
    Charset encoding = null;
    // A specific encoding configured for a given file always wins:
    if (module != null) {
        encoding = module.getFileEncoding(source);
        if (encoding != null) {
            copyTextFileWithEncoding(source, dest, encoding);
            return;
        }
        encoding = module.getProjectEncoding(source);
    }
    if (encoding == null) {
        encoding = getEncodingFromWorkspaceSetting();
    }
    // For XML files we can sometimes read the encoding right out of the XML prologue
    if (SdkUtils.endsWithIgnoreCase(source.getPath(), DOT_XML)) {
        String defaultCharset = encoding != null ? encoding.name() : SdkConstants.UTF_8;
        String xml = PositionXmlParser.getXmlString(Files.toByteArray(source), defaultCharset);
        // Replace prologue if it specifies the encoding
        if (xml.startsWith("<?xml")) {
            int prologueEnd = xml.indexOf("?>");
            if (prologueEnd != -1) {
                xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + xml.substring(prologueEnd + 2);
            }
        }
        Files.write(xml, dest, Charsets.UTF_8);
    } else if (encoding != null) {
        copyTextFileWithEncoding(source, dest, encoding);
    } else {
        Files.copy(source, dest);
    }
}
Also used : Charset(java.nio.charset.Charset)

Example 87 with Charset

use of java.nio.charset.Charset in project adempiere by adempiere.

the class WFileImport method cmd_loadFile.

/**************************************************************************
	 *	Load File
	 */
private void cmd_loadFile() {
    Media media = null;
    media = Fileupload.get();
    if (media == null)
        return;
    if (media.isBinary()) {
        m_file_istream = media.getStreamData();
    } else {
        ListItem listitem = fCharset.getSelectedItem();
        if (listitem == null) {
            m_file_istream = new ReaderInputStream(media.getReaderData());
        } else {
            Charset charset = (Charset) listitem.getValue();
            m_file_istream = new ReaderInputStream(media.getReaderData(), charset.name());
        }
    }
    log.config(media.getName());
    bFile.setLabel(media.getName());
    cmd_reloadFile();
}
Also used : ReaderInputStream(org.adempiere.webui.util.ReaderInputStream) Media(org.zkoss.util.media.Media) Charset(java.nio.charset.Charset) ListItem(org.adempiere.webui.component.ListItem)

Example 88 with Charset

use of java.nio.charset.Charset in project adempiere by adempiere.

the class WFileImport method dynInit.

/**
	 *	Dynamic Init
	 */
private void dynInit() {
    //	Load Formats
    pickFormat.appendItem(s_none, s_none);
    String sql = MRole.getDefault().addAccessSQL("SELECT Name FROM AD_ImpFormat", "AD_ImpFormat", MRole.SQL_FULLYQUALIFIED, MRole.SQL_RO);
    try {
        PreparedStatement pstmt = DB.prepareStatement(sql, null);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) pickFormat.appendItem(rs.getString(1), rs.getString(1));
        rs.close();
        pstmt.close();
    } catch (SQLException e) {
        log.log(Level.SEVERE, sql, e);
    }
    pickFormat.setSelectedIndex(0);
    pickFormat.addEventListener(Events.ON_SELECT, this);
    Charset charset = Ini.getCharset();
    for (int i = 0; i < fCharset.getItemCount(); i++) {
        ListItem listitem = fCharset.getItemAtIndex(i);
        Charset compare = (Charset) listitem.getValue();
        if (charset == compare) {
            fCharset.setSelectedIndex(i);
            break;
        }
    }
    fCharset.addEventListener(Events.ON_SELECT, this);
    confirmPanel.setEnabled("Ok", false);
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Charset(java.nio.charset.Charset) PreparedStatement(java.sql.PreparedStatement) ListItem(org.adempiere.webui.component.ListItem)

Example 89 with Charset

use of java.nio.charset.Charset in project adempiere by adempiere.

the class WFileImport method cmd_reloadFile.

/**
	 * Reload/Load file
	 */
private void cmd_reloadFile() {
    if (m_file_istream == null)
        return;
    m_data.clear();
    rawData.setText("");
    try {
        //  see NaturalAccountMap
        ListItem listitem = fCharset.getSelectedItem();
        Charset charset = null;
        if (listitem == null)
            return;
        charset = (Charset) listitem.getValue();
        BufferedReader in = new BufferedReader(new InputStreamReader(m_file_istream, charset), 10240);
        //	not safe see p108 Network pgm
        String s = null;
        String concat = "";
        while ((s = in.readLine()) != null) {
            m_data.add(s);
            concat += s;
            concat += "\n";
            if (m_data.size() < MAX_LOADED_LINES) {
                rawData.setValue(concat);
            }
        }
        in.close();
    } catch (Exception e) {
        log.log(Level.SEVERE, "", e);
        bFile.setLabel(Msg.getMsg(Env.getCtx(), "FileImportFile"));
    }
    //	second line as first may be heading
    int index = 1;
    if (m_data.size() == 1)
        index = 0;
    int length = 0;
    if (m_data.size() > 0)
        length = m_data.get(index).toString().length();
    info.setValue(Msg.getMsg(Env.getCtx(), "Records") + "=" + m_data.size() + ", " + Msg.getMsg(Env.getCtx(), "Length") + "=" + length + "   ");
    //setCursor (Cursor.getDefaultCursor());
    log.config("Records=" + m_data.size() + ", Length=" + length);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Charset(java.nio.charset.Charset) ListItem(org.adempiere.webui.component.ListItem) SQLException(java.sql.SQLException)

Example 90 with Charset

use of java.nio.charset.Charset in project adempiere by adempiere.

the class WFileImport method jbInit.

//	init
/**
	 *	Static Init
	 *  @throws Exception
	 */
private void jbInit() throws Exception {
    Charset[] charsets = Ini.getAvailableCharsets();
    for (int i = 0; i < charsets.length; i++) fCharset.appendItem(charsets[i].displayName(), charsets[i]);
    bFile.setLabel(Msg.getMsg(Env.getCtx(), "FileImportFile"));
    bFile.setTooltiptext(Msg.getMsg(Env.getCtx(), "FileImportFileInfo"));
    bFile.addEventListener(Events.ON_CLICK, this);
    fCharset.setMold("select");
    fCharset.setRows(0);
    fCharset.setTooltiptext(Msg.getMsg(Env.getCtx(), "Charset", false));
    info.setValue("   ");
    labelFormat.setValue(Msg.translate(Env.getCtx(), "AD_ImpFormat_ID"));
    pickFormat.setMold("select");
    pickFormat.setRows(0);
    bNext.setTooltiptext(Msg.getMsg(Env.getCtx(), "Next"));
    bNext.setLabel(">");
    bNext.addEventListener(Events.ON_CLICK, this);
    record.setValue("------");
    bPrevious.setTooltiptext(Msg.getMsg(Env.getCtx(), "Previous"));
    bPrevious.setLabel("<");
    bPrevious.addEventListener(Events.ON_CLICK, this);
    northPanel.appendChild(bFile);
    northPanel.appendChild(fCharset);
    northPanel.appendChild(info);
    northPanel.appendChild(labelFormat);
    northPanel.appendChild(pickFormat);
    northPanel.appendChild(bPrevious);
    northPanel.appendChild(record);
    northPanel.appendChild(bNext);
    rawData.setWidth("100%");
    rawData.setCols(80);
    rawData.setRows(MAX_SHOWN_LINES);
    rawData.setHeight("40%");
    previewPanel.setWidth("100%");
    previewPanel.setHeight("58%");
    previewPanel.setStyle("overflow: auto");
    // Elaine 2008/11/07 - fix text area is not expanded in IE7
    centerPanel.setWidth("100%");
    centerPanel.setHeight("100%");
    centerPanel.appendChild(rawData);
    centerPanel.appendChild(new Separator());
    centerPanel.appendChild(previewPanel);
    confirmPanel.addActionListener(Events.ON_CLICK, this);
}
Also used : Charset(java.nio.charset.Charset) Separator(org.zkoss.zul.Separator)

Aggregations

Charset (java.nio.charset.Charset)1427 IOException (java.io.IOException)268 Test (org.junit.Test)186 InputStream (java.io.InputStream)115 ByteBuffer (java.nio.ByteBuffer)111 File (java.io.File)106 ArrayList (java.util.ArrayList)106 InputStreamReader (java.io.InputStreamReader)102 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)66 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)58 CharsetDecoder (java.nio.charset.CharsetDecoder)57 List (java.util.List)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 ByteArrayInputStream (java.io.ByteArrayInputStream)54 CharsetEncoder (java.nio.charset.CharsetEncoder)50 Path (java.nio.file.Path)50 FileInputStream (java.io.FileInputStream)49 BufferedReader (java.io.BufferedReader)48