Search in sources :

Example 91 with Charset

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

the class ImpFormat method loadFile.

//	updateDB
public void loadFile(Properties ctx, File file, String trxName, int AD_Client_ID, int AD_Org_ID, boolean excludeHeaderRow) {
    m_AD_Client_ID = AD_Client_ID;
    m_AD_Org_ID = AD_Org_ID;
    if (file == null)
        throw new AdempiereException("No file");
    try {
        Charset charset = Ini.getCharset();
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset), 10240);
        String s = null;
        boolean first = true;
        while ((s = in.readLine()) != null) {
            if (first && excludeHeaderRow) {
                first = false;
                continue;
            }
            first = false;
            updateDB(ctx, s, trxName);
        }
        in.close();
    } catch (Exception e) {
        throw new AdempiereException("Load error", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) AdempiereException(org.adempiere.exceptions.AdempiereException) BufferedReader(java.io.BufferedReader) Charset(java.nio.charset.Charset) FileInputStream(java.io.FileInputStream) SQLException(java.sql.SQLException) AdempiereException(org.adempiere.exceptions.AdempiereException)

Example 92 with Charset

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

the class VFileImport method cmd_reloadFile.

/**
	 * Reload/Load file
	 */
private void cmd_reloadFile() {
    if (m_file == null)
        return;
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    m_data.clear();
    rawData.setText("");
    try {
        //  see NaturalAccountMap
        Charset charset = (Charset) fCharset.getSelectedItem();
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(m_file), charset), 10240);
        //	not safe see p108 Network pgm
        String s = null;
        while ((s = in.readLine()) != null) {
            m_data.add(s);
            if (m_data.size() <= MAX_LOADED_LINES) {
                rawData.append(s);
                rawData.append("\n");
            }
        }
        in.close();
        rawData.setCaretPosition(0);
    } catch (Exception e) {
        log.log(Level.SEVERE, "", e);
        bFile.setText(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.setText(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) FileInputStream(java.io.FileInputStream) SQLException(java.sql.SQLException)

Example 93 with Charset

use of java.nio.charset.Charset in project asterixdb by apache.

the class DatasourceFactoryProvider method initFactories.

protected static Map<String, Class> initFactories() throws AsterixException {
    Map<String, Class> factories = new HashMap<>();
    ClassLoader cl = ParserFactoryProvider.class.getClassLoader();
    final Charset encoding = Charset.forName("UTF-8");
    try {
        Enumeration<URL> urls = cl.getResources(RESOURCE);
        for (URL url : Collections.list(urls)) {
            InputStream is = url.openStream();
            String config = IOUtils.toString(is, encoding);
            is.close();
            String[] classNames = config.split("\n");
            for (String className : classNames) {
                if (className.startsWith("#")) {
                    continue;
                }
                final Class<?> clazz = Class.forName(className);
                List<String> formats = ((IRecordReaderFactory) clazz.newInstance()).getRecordReaderNames();
                for (String format : formats) {
                    if (factories.containsKey(format)) {
                        throw new AsterixException(ErrorCode.PROVIDER_DATASOURCE_FACTORY_DUPLICATE_FORMAT_MAPPING, format);
                    }
                    factories.put(format, clazz);
                }
            }
        }
    } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new AsterixException(e);
    }
    return factories;
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) URL(java.net.URL) IRecordReaderFactory(org.apache.asterix.external.api.IRecordReaderFactory) AsterixException(org.apache.asterix.common.exceptions.AsterixException)

Example 94 with Charset

use of java.nio.charset.Charset in project asterixdb by apache.

the class StreamRecordReaderProvider method initRecordReaders.

protected static Map<String, List<Pair<String[], Class>>> initRecordReaders() throws AsterixException {
    Map<String, List<Pair<String[], Class>>> recordReaders = new HashMap<>();
    ClassLoader cl = StreamRecordReaderProvider.class.getClassLoader();
    final Charset encoding = Charset.forName("UTF-8");
    try {
        Enumeration<URL> urls = cl.getResources(RESOURCE);
        for (URL url : Collections.list(urls)) {
            InputStream is = url.openStream();
            String config = IOUtils.toString(is, encoding);
            is.close();
            String[] classNames = config.split("\n");
            for (String className : classNames) {
                if (className.startsWith("#")) {
                    continue;
                }
                final Class<?> clazz = Class.forName(className);
                StreamRecordReader newInstance = (StreamRecordReader) clazz.getConstructor().newInstance();
                List<String> formats = newInstance.getRecordReaderFormats();
                String[] configs = newInstance.getRequiredConfigs().split(":");
                for (String format : formats) {
                    if (!recordReaders.containsKey(format)) {
                        recordReaders.put(format, new ArrayList<>());
                    }
                    recordReaders.get(format).add(Pair.of(configs, clazz));
                }
            }
        }
    } catch (IOException | ClassNotFoundException | InvocationTargetException | IllegalAccessException | NoSuchMethodException | InstantiationException e) {
        throw new AsterixException(e);
    }
    return recordReaders;
}
Also used : HashMap(java.util.HashMap) StreamRecordReader(org.apache.asterix.external.input.record.reader.stream.StreamRecordReader) AsterixInputStream(org.apache.asterix.external.api.AsterixInputStream) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException) AsterixException(org.apache.asterix.common.exceptions.AsterixException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 95 with Charset

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

the class Preference method cmd_save.

//	load
/**
	 *	Save Settings
	 */
private void cmd_save() {
    log.config("");
    //  UI
    //	AutoCommit
    Ini.setProperty(Ini.P_A_COMMIT, (autoCommit.isSelected()));
    Env.setAutoCommit(Env.getCtx(), autoCommit.isSelected());
    Ini.setProperty(Ini.P_A_NEW, (autoNew.isSelected()));
    Env.setAutoNew(Env.getCtx(), autoNew.isSelected());
    //	AdempiereSys
    Ini.setProperty(Ini.P_ADEMPIERESYS, adempiereSys.isSelected());
    //	LogMigrationScript
    Ini.setProperty(Ini.P_LOGMIGRATIONSCRIPT, logMigrationScript.isSelected());
    if (MSystem.isSwingRememberPasswordAllowed()) {
        //	AutoLogin
        Ini.setProperty(Ini.P_A_LOGIN, (autoLogin.isSelected()));
        //	Save Password
        Ini.setProperty(Ini.P_STORE_PWD, (storePassword.isSelected()));
    } else {
        Ini.setProperty(Ini.P_A_LOGIN, false);
        Ini.setProperty(Ini.P_STORE_PWD, false);
    }
    //	Show Acct Tab
    Ini.setProperty(Ini.P_SHOW_ACCT, (showAcct.isSelected()));
    Env.setContext(Env.getCtx(), "#ShowAcct", (showAcct.isSelected()));
    //	Show Trl Tab
    Ini.setProperty(Ini.P_SHOW_TRL, (showTrl.isSelected()));
    Env.setContext(Env.getCtx(), "#ShowTrl", (showTrl.isSelected()));
    //	Show Advanced Tab
    Ini.setProperty(Ini.P_SHOW_ADVANCED, (showAdvanced.isSelected()));
    Env.setContext(Env.getCtx(), "#ShowAdvanced", (showAdvanced.isSelected()));
    //  ConnectionProfile
    ValueNamePair ppNew = (ValueNamePair) connectionProfile.getSelectedItem();
    String cpNew = ppNew.getValue();
    String cpOld = CConnection.get().getConnectionProfile();
    CConnection.get().setConnectionProfile(cpNew);
    if (!cpNew.equals(cpOld) && (cpNew.equals(CConnection.PROFILE_WAN) || cpOld.equals(CConnection.PROFILE_WAN)))
        ADialog.info(0, this, "ConnectionProfileChange");
    Ini.setProperty(Ini.P_CACHE_WINDOW, cacheWindow.isSelected());
    //  Print Preview
    Ini.setProperty(Ini.P_PRINTPREVIEW, (printPreview.isSelected()));
    //  Validate Connection on Startup
    Ini.setProperty(Ini.P_VALIDATE_CONNECTION_ON_STARTUP, (validateConnectionOnStartup.isSelected()));
    //  Single Instance per Window
    Ini.setProperty(Ini.P_SINGLE_INSTANCE_PER_WINDOW, (singleInstancePerWindow.isSelected()));
    //  Open Window Maximized
    Ini.setProperty(Ini.P_OPEN_WINDOW_MAXIMIZED, (openWindowMaximized.isSelected()));
    //	TraceLevel/File
    Level level = (Level) traceLevel.getSelectedItem();
    CLogMgt.setLevel(level);
    Ini.setProperty(Ini.P_TRACELEVEL, level.getName());
    Ini.setProperty(Ini.P_TRACEFILE, traceFile.isSelected());
    //  Printer
    String printer = (String) fPrinter.getSelectedItem();
    Env.setContext(Env.getCtx(), "#Printer", printer);
    Ini.setProperty(Ini.P_PRINTER, printer);
    //  Date (remove seconds)
    java.sql.Timestamp ts = (java.sql.Timestamp) fDate.getValue();
    if (ts != null)
        Env.setContext(Env.getCtx(), "#Date", ts);
    // Charset
    Charset charset = (Charset) fCharset.getSelectedItem();
    Ini.setProperty(Ini.P_CHARSET, charset.name());
    //UI
    ValueNamePair laf = plafEditor.getSelectedLook();
    ValueNamePair theme = plafEditor.getSelectedTheme();
    if (laf != null) {
        String clazz = laf.getValue();
        String currentLaf = UIManager.getLookAndFeel().getClass().getName();
        if (clazz != null && clazz.length() > 0 && !currentLaf.equals(clazz)) {
            //laf changed
            AdempierePLAF.setPLAF(laf, theme, true);
            AEnv.updateUI();
        } else {
            if (UIManager.getLookAndFeel() instanceof MetalLookAndFeel) {
                MetalTheme currentTheme = MetalLookAndFeel.getCurrentTheme();
                String themeClass = currentTheme.getClass().getName();
                String sTheme = theme.getValue();
                if (sTheme != null && sTheme.length() > 0 && !sTheme.equals(themeClass)) {
                    ValueNamePair plaf = new ValueNamePair(UIManager.getLookAndFeel().getClass().getName(), UIManager.getLookAndFeel().getName());
                    AdempierePLAF.setPLAF(plaf, theme, true);
                    AEnv.updateUI();
                }
            }
        }
    }
    Ini.saveProperties(Ini.isClient());
    dispose();
}
Also used : MetalTheme(javax.swing.plaf.metal.MetalTheme) Charset(java.nio.charset.Charset) Level(java.util.logging.Level) ValueNamePair(org.compiere.util.ValueNamePair) MetalLookAndFeel(javax.swing.plaf.metal.MetalLookAndFeel)

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