Search in sources :

Example 26 with Charset

use of java.nio.charset.Charset in project jphp by jphp-compiler.

the class StrUtils method encode.

@FastMethod
@Signature({ @Arg("string"), @Arg("charset") })
public static Memory encode(Environment env, Memory... args) {
    Charset charset;
    try {
        charset = Charset.forName(args[1].toString());
    } catch (Exception e) {
        return Memory.FALSE;
    }
    ByteBuffer buffer = charset.encode(args[0].toString());
    return new BinaryMemory(Arrays.copyOf(buffer.array(), buffer.limit()));
}
Also used : Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) FastMethod(php.runtime.annotation.Runtime.FastMethod)

Example 27 with Charset

use of java.nio.charset.Charset in project XobotOS by xamarin.

the class GsmAlphabet method gsm8BitUnpackedToString.

/**
     * Convert a GSM alphabet string that's stored in 8-bit unpacked
     * format (as it often appears in SIM records) into a String
     *
     * Field may be padded with trailing 0xff's. The decode stops
     * at the first 0xff encountered.
     *
     * Additionally, in some country(ex. Korea), there are non-ASCII or MBCS characters.
     * If a character set is given, characters in data are treat as MBCS.
     */
public static String gsm8BitUnpackedToString(byte[] data, int offset, int length, String characterset) {
    boolean isMbcs = false;
    Charset charset = null;
    ByteBuffer mbcsBuffer = null;
    if (!TextUtils.isEmpty(characterset) && !characterset.equalsIgnoreCase("us-ascii") && Charset.isSupported(characterset)) {
        isMbcs = true;
        charset = Charset.forName(characterset);
        mbcsBuffer = ByteBuffer.allocate(2);
    }
    // Always use GSM 7 bit default alphabet table for this method
    String languageTableToChar = sLanguageTables[0];
    String shiftTableToChar = sLanguageShiftTables[0];
    StringBuilder ret = new StringBuilder(length);
    boolean prevWasEscape = false;
    for (int i = offset; i < offset + length; i++) {
        // Never underestimate the pain that can be caused
        // by signed bytes
        int c = data[i] & 0xff;
        if (c == 0xff) {
            break;
        } else if (c == GSM_EXTENDED_ESCAPE) {
            if (prevWasEscape) {
                // Two escape chars in a row
                // We treat this as a space
                // See Note 1 in table 6.2.1.1 of TS 23.038 v7.00
                ret.append(' ');
                prevWasEscape = false;
            } else {
                prevWasEscape = true;
            }
        } else {
            if (prevWasEscape) {
                char shiftChar = shiftTableToChar.charAt(c);
                if (shiftChar == ' ') {
                    // display character from main table if not present in shift table
                    ret.append(languageTableToChar.charAt(c));
                } else {
                    ret.append(shiftChar);
                }
            } else {
                if (!isMbcs || c < 0x80 || i + 1 >= offset + length) {
                    ret.append(languageTableToChar.charAt(c));
                } else {
                    // isMbcs must be true. So both mbcsBuffer and charset are initialized.
                    mbcsBuffer.clear();
                    mbcsBuffer.put(data, i++, 2);
                    mbcsBuffer.flip();
                    ret.append(charset.decode(mbcsBuffer).toString());
                }
            }
            prevWasEscape = false;
        }
    }
    return ret.toString();
}
Also used : Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer)

Example 28 with Charset

use of java.nio.charset.Charset in project tdi-studio-se by Talend.

the class JSONFileStep1Form method addFieldsListeners.

/**
     * Main Fields addControls.
     */
@Override
protected void addFieldsListeners() {
    // fileFieldXsd : Event modifyText
    // fileFieldXsd.addModifyListener(new ModifyListener() {
    //
    // public void modifyText(final ModifyEvent e) {
    // getConnection().setXsdFilePath(fileFieldXsd.getText());
    // treePopulator.populateTree(fileFieldXsd.getText(), treeNode);
    // checkFieldsValue();
    // }
    // });
    // fileFieldJSON.addSelectionListener(new SelectionListener() {
    //
    // public void widgetSelected(SelectionEvent event) {
    // if (fileFieldJSON.getResult() == null) {
    // return;
    // }
    // String text = fileFieldJSON.getText();
    // if (isContextMode()) {
    // ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(
    // connectionItem.getConnection(), true);
    // text = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, text));
    // }
    // // getConnection().setJSONFilePath(PathUtils.getPortablePath(JSONXsdFilePath.getText()));
    // File file = new File(text);
    // if (file.exists()) {
    // if (file.exists()) {
    // String tempxml = JSONUtil.changeJsonToXml(text);
    // JSONFileStep1Form.this.wizard.setTempJsonPath(tempxml);
    // valid = treePopulator.populateTree(tempxml, treeNode);
    // }
    // // add for bug TDI-20432
    // checkFieldsValue();
    // }
    //
    // }
    //
    // public void widgetDefaultSelected(SelectionEvent e) {
    //
    // }
    // });
    readbyCombo.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            EJsonReadbyMode eJsonReadbyMode = EJsonReadbyMode.getEJsonReadbyModeByDisplayName(readbyCombo.getText());
            if (eJsonReadbyMode == null) {
                eJsonReadbyMode = getDefaultJsonReadbyMode();
            }
            String readbyMode = eJsonReadbyMode.getValue();
            JSONFileStep1Form.this.wizard.setReadbyMode(readbyMode);
            String jsonPath = fileFieldJSON.getText();
            String text = validateJsonFilePath(jsonPath);
            if (text == null || text.isEmpty()) {
                return;
            }
            String tempxml = null;
            if (EJsonReadbyMode.JSONPATH.getValue().equals(readbyMode)) {
                tempxml = text;
            } else {
                tempxml = JSONUtil.changeJsonToXml(text);
            }
            JSONFileStep1Form.this.wizard.setTempJsonPath(tempxml);
            switchPopulator(readbyMode, tempxml);
        }
    });
    // fileFieldJSON : Event modifyText
    fileFieldJSON.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(final ModifyEvent e) {
            String jsonPath = fileFieldJSON.getText();
            String _jsonPath = jsonPath;
            if (isContextMode()) {
                ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connectionItem.getConnection(), connectionItem.getConnection().getContextName());
                jsonPath = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, jsonPath));
            }
            String text = validateJsonFilePath(jsonPath);
            if (text == null || text.isEmpty()) {
                return;
            }
            String tempxml = null;
            String readbyMode = JSONFileStep1Form.this.wizard.getReadbyMode();
            if (EJsonReadbyMode.JSONPATH.getValue().equals(readbyMode)) {
                tempxml = text;
            } else {
                tempxml = JSONUtil.changeJsonToXml(text);
            }
            File file = new File(text);
            if (!file.exists()) {
                file = new File(JSONUtil.tempJSONXsdPath);
            }
            JSONFileStep1Form.this.wizard.setTempJsonPath(tempxml);
            String limitString = commonNodesLimitation.getText();
            try {
                limit = Integer.valueOf(limitString);
                labelLimitation.setToolTipText(MessageFormat.format(Messages.JSONLimitToolTip, limit));
            } catch (Exception excpt) {
            // nothing need to do
            }
            switchPopulator(readbyMode, tempxml);
            // }
            // add for bug TDI-20432
            checkFieldsValue();
            if (getConnection().getJSONFilePath() != null && !getConnection().getJSONFilePath().equals(text)) {
                getConnection().getLoop().clear();
                xsdPathChanged = true;
            } else {
                xsdPathChanged = false;
            }
            if (isContextMode()) {
                jsonPath = _jsonPath;
            }
            if (Path.fromOSString(jsonPath).toFile().isFile()) {
                getConnection().setJSONFilePath(PathUtils.getPortablePath(jsonPath));
            } else {
                getConnection().setJSONFilePath(jsonPath);
            }
            JSONWizard wizard = ((JSONWizard) getPage().getWizard());
            wizard.setTreeRootNode(treeNode);
            BufferedReader in = null;
            try {
                Charset guessedCharset = CharsetToolkit.guessEncoding(file, 4096);
                String str;
                in = new BufferedReader(new InputStreamReader(new FileInputStream(file), guessedCharset.displayName()));
                while ((str = in.readLine()) != null) {
                    if (str.contains("encoding")) {
                        //$NON-NLS-1$
                        //$NON-NLS-1$
                        String regex = "^<\\?JSON\\s*version=\\\"[^\\\"]*\\\"\\s*encoding=\\\"([^\\\"]*)\\\"\\?>$";
                        Perl5Compiler compiler = new Perl5Compiler();
                        Perl5Matcher matcher = new Perl5Matcher();
                        Pattern pattern = null;
                        try {
                            pattern = compiler.compile(regex);
                            if (matcher.contains(str, pattern)) {
                                MatchResult matchResult = matcher.getMatch();
                                if (matchResult != null) {
                                    encoding = matchResult.group(1);
                                }
                            }
                        } catch (MalformedPatternException malE) {
                            ExceptionHandler.process(malE);
                        }
                    }
                }
            } catch (Exception ex) {
                String fileStr = text;
                String msgError = //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                "JSON" + " \"" + fileStr.replace("\\\\", "\\") + //$NON-NLS-1$
                "\"\n";
                if (ex instanceof FileNotFoundException) {
                    msgError = msgError + "is not found";
                } else if (ex instanceof EOFException) {
                    msgError = msgError + "have an incorrect character EOF";
                } else if (ex instanceof IOException) {
                    msgError = msgError + "is locked by another soft";
                } else {
                    msgError = msgError + "doesn't exist";
                }
                if (!isReadOnly()) {
                    updateStatus(IStatus.ERROR, msgError);
                }
            // ExceptionHandler.process(ex);
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception ex2) {
                    ExceptionHandler.process(ex2);
                }
            }
            if (getConnection().getEncoding() == null || "".equals(getConnection().getEncoding())) {
                //$NON-NLS-1$
                getConnection().setEncoding(encoding);
                if (encoding != null && !("").equals(encoding)) {
                    //$NON-NLS-1$
                    encodingCombo.setText(encoding);
                } else {
                    //$NON-NLS-1$
                    encodingCombo.setText("UTF-8");
                }
            }
            // if (tempJSONXsdPath != null && getConnection().getFileContent() != null
            // && getConnection().getFileContent().length > 0 && !isModifing) {
            // valid = treePopulator.populateTree(tempJSONXsdPath, treeNode);
            // } else {
            // valid = treePopulator.populateTree(text, treeNode);
            // }
            checkFieldsValue();
            isModifing = true;
        }
    });
    // Event encodingCombo
    encodingCombo.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(final ModifyEvent e) {
            getConnection().setEncoding(encodingCombo.getText());
            checkFieldsValue();
        }
    });
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) ModifyListener(org.eclipse.swt.events.ModifyListener) InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) Charset(java.nio.charset.Charset) EJsonReadbyMode(org.talend.repository.ui.wizards.metadata.connection.files.json.EJsonReadbyMode) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) IOException(java.io.IOException) MatchResult(org.apache.oro.text.regex.MatchResult) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) BusinessException(org.talend.commons.exception.BusinessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PersistenceException(org.talend.commons.exception.PersistenceException) FileInputStream(java.io.FileInputStream) ModifyEvent(org.eclipse.swt.events.ModifyEvent) BufferedReader(java.io.BufferedReader) EOFException(java.io.EOFException) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) File(java.io.File)

Example 29 with Charset

use of java.nio.charset.Charset in project tdi-studio-se by Talend.

the class JSONFileStep2Form method checkFilePathAndManageIt.

/**
     * checkFileFieldsValue active fileViewer if file exist.
     * 
     * @throws IOException
     */
private void checkFilePathAndManageIt() {
    updateStatus(IStatus.OK, null);
    filePathIsDone = false;
    if (getConnection().getJSONFilePath() == "") {
        //$NON-NLS-1$
        fileJSONText.setText("When Filepath is specified, you can read here the" + " " + TreePopulator.getMaximumRowsToPreview() + " " + "first rows of the file.");
    } else {
        fileJSONText.setText("Check if the file exist ...");
        StringBuilder previewRows = new StringBuilder();
        BufferedReader in = null;
        //$NON-NLS-1$
        String pathStr = "";
        try {
            if (tempJSONXsdPath != null) {
                pathStr = tempJSONXsdPath;
            } else {
                pathStr = getConnection().getJSONFilePath();
            }
            if (isContextMode()) {
                ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connectionItem.getConnection(), connectionItem.getConnection().getContextName());
                pathStr = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, pathStr));
            }
            File file = new File(pathStr);
            if (!file.exists()) {
                file.createNewFile();
                FileOutputStream outStream;
                try {
                    outStream = new FileOutputStream(file);
                    outStream.write(getConnection().getFileContent());
                    outStream.close();
                } catch (FileNotFoundException e1) {
                    ExceptionHandler.process(e1);
                } catch (IOException e) {
                    ExceptionHandler.process(e);
                }
            }
            Charset guessedCharset = CharsetToolkit.guessEncoding(file, 4096);
            String str;
            in = new BufferedReader(new InputStreamReader(new FileInputStream(pathStr), guessedCharset.displayName()));
            while ((str = in.readLine()) != null) {
                //$NON-NLS-1$
                previewRows.append(str + "\n");
            }
            // show lines
            fileJSONText.setText(new String(previewRows));
            filePathIsDone = true;
        } catch (Exception e) {
            String msgError = "File" + " \"" + fileJSONText.getText().replace("\\\\", "\\") + "\"\n";
            if (e instanceof FileNotFoundException) {
                msgError = msgError + "is not found";
            } else if (e instanceof EOFException) {
                msgError = msgError + "have an incorrect character EOF";
            } else if (e instanceof IOException) {
                msgError = msgError + "is locked by another soft";
            } else {
                msgError = msgError + "doesn't exist";
            }
            fileJSONText.setText(msgError);
            if (!isReadOnly()) {
                updateStatus(IStatus.ERROR, msgError);
            }
            //$NON-NLS-1$
            log.error(msgError + " " + e.getMessage());
        } finally {
            String msgError = "File" + " \"" + fileJSONText.getText().replace("\\\\", "\\") + "\"\n";
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                msgError = msgError + "is locked by another soft";
            }
        }
        checkFieldsValue();
    }
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) Charset(java.nio.charset.Charset) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CoreException(org.eclipse.core.runtime.CoreException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) EOFException(java.io.EOFException) File(java.io.File)

Example 30 with Charset

use of java.nio.charset.Charset in project intellij-community by JetBrains.

the class GroovyConsole method send.

private static void send(@NotNull ProcessHandler processHandler, @NotNull String command) {
    final OutputStream outputStream = processHandler.getProcessInput();
    assert outputStream != null : "output stream is null";
    final Charset charset = processHandler instanceof BaseOSProcessHandler ? ((BaseOSProcessHandler) processHandler).getCharset() : null;
    byte[] bytes = (command + "\n").getBytes(charset != null ? charset : UTF_8);
    try {
        outputStream.write(bytes);
        outputStream.flush();
    } catch (IOException ignored) {
        LOG.warn(ignored);
    }
}
Also used : OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException)

Aggregations

Charset (java.nio.charset.Charset)1400 IOException (java.io.IOException)259 Test (org.junit.Test)186 InputStream (java.io.InputStream)114 ByteBuffer (java.nio.ByteBuffer)110 File (java.io.File)104 ArrayList (java.util.ArrayList)102 InputStreamReader (java.io.InputStreamReader)99 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)64 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 CharsetDecoder (java.nio.charset.CharsetDecoder)55 ByteArrayInputStream (java.io.ByteArrayInputStream)54 List (java.util.List)54 Path (java.nio.file.Path)50 CharsetEncoder (java.nio.charset.CharsetEncoder)49 FileInputStream (java.io.FileInputStream)48 OutputStream (java.io.OutputStream)47