Example usage for java.io InputStreamReader getEncoding

List of usage examples for java.io InputStreamReader getEncoding

Introduction

In this page you can find the example usage for java.io InputStreamReader getEncoding.

Prototype

public String getEncoding() 

Source Link

Document

Returns the name of the character encoding being used by this stream.

Usage

From source file:org.pentaho.di.ui.trans.steps.textfileinput.TextFileInputDialog.java

private void getCSV() {
    TextFileInputMeta meta = new TextFileInputMeta();
    getInfo(meta);//w  w w . j  ava  2s .  com
    TextFileInputMeta previousMeta = (TextFileInputMeta) meta.clone();
    FileInputList textFileList = meta.getTextFileList(transMeta);
    InputStream fileInputStream;
    CompressionInputStream inputStream = null;
    StringBuilder lineStringBuilder = new StringBuilder(256);
    int fileFormatType = meta.getFileFormatTypeNr();

    String delimiter = transMeta.environmentSubstitute(meta.getSeparator());
    String enclosure = transMeta.environmentSubstitute(meta.getEnclosure());
    String escapeCharacter = transMeta.environmentSubstitute(meta.getEscapeCharacter());

    if (textFileList.nrOfFiles() > 0) {
        int clearFields = meta.hasHeader() ? SWT.YES : SWT.NO;
        int nrInputFields = meta.getInputFields().length;

        if (meta.hasHeader() && nrInputFields > 0) {
            MessageBox mb = new MessageBox(shell, SWT.YES | SWT.NO | SWT.CANCEL | SWT.ICON_QUESTION);
            mb.setMessage(BaseMessages.getString(PKG, "TextFileInputDialog.ClearFieldList.DialogMessage"));
            mb.setText(BaseMessages.getString(PKG, "TextFileInputDialog.ClearFieldList.DialogTitle"));
            clearFields = mb.open();
            if (clearFields == SWT.CANCEL) {
                return;
            }
        }

        try {
            wFields.table.removeAll();

            FileObject fileObject = textFileList.getFile(0);
            fileInputStream = KettleVFS.getInputStream(fileObject);
            Table table = wFields.table;

            CompressionProvider provider = CompressionProviderFactory.getInstance()
                    .createCompressionProviderInstance(meta.getFileCompression());
            inputStream = provider.createInputStream(fileInputStream);

            InputStreamReader reader;
            if (meta.getEncoding() != null && meta.getEncoding().length() > 0) {
                reader = new InputStreamReader(inputStream, meta.getEncoding());
            } else {
                reader = new InputStreamReader(inputStream);
            }

            EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());

            if (clearFields == SWT.YES || !meta.hasHeader() || nrInputFields > 0) {
                // Scan the header-line, determine fields...
                String line;

                if (meta.hasHeader() || meta.getInputFields().length == 0) {
                    line = TextFileInput.getLine(log, reader, encodingType, fileFormatType, lineStringBuilder);
                    if (line != null) {
                        // Estimate the number of input fields...
                        // Chop up the line using the delimiter
                        String[] fields = TextFileInput.guessStringsFromLine(transMeta, log, line, meta,
                                delimiter, enclosure, escapeCharacter);

                        for (int i = 0; i < fields.length; i++) {
                            String field = fields[i];
                            if (field == null || field.length() == 0
                                    || (nrInputFields == 0 && !meta.hasHeader())) {
                                field = "Field" + (i + 1);
                            } else {
                                // Trim the field
                                field = Const.trim(field);
                                // Replace all spaces & - with underscore _
                                field = Const.replace(field, " ", "_");
                                field = Const.replace(field, "-", "_");
                            }

                            TableItem item = new TableItem(table, SWT.NONE);
                            item.setText(1, field);
                            item.setText(2, "String"); // The default type is String...
                        }

                        wFields.setRowNums();
                        wFields.optWidth(true);

                        // Copy it...
                        getInfo(meta);
                    }
                }

                // Sample a few lines to determine the correct type of the fields...
                String shellText = BaseMessages.getString(PKG, "TextFileInputDialog.LinesToSample.DialogTitle");
                String lineText = BaseMessages.getString(PKG,
                        "TextFileInputDialog.LinesToSample.DialogMessage");
                EnterNumberDialog end = new EnterNumberDialog(shell, 100, shellText, lineText);
                int samples = end.open();
                if (samples >= 0) {
                    getInfo(meta);

                    TextFileCSVImportProgressDialog pd = new TextFileCSVImportProgressDialog(shell, meta,
                            transMeta, reader, samples, clearFields == SWT.YES);
                    String message = pd.open();
                    if (message != null) {
                        wFields.removeAll();

                        // OK, what's the result of our search?
                        getData(meta);

                        // If we didn't want the list to be cleared, we need to re-inject the previous values...
                        //
                        if (clearFields == SWT.NO) {
                            getFieldsData(previousMeta, true);
                            wFields.table.setSelection(previousMeta.getInputFields().length,
                                    wFields.table.getItemCount() - 1);
                        }

                        wFields.removeEmptyRows();
                        wFields.setRowNums();
                        wFields.optWidth(true);

                        EnterTextDialog etd = new EnterTextDialog(shell,
                                BaseMessages.getString(PKG, "TextFileInputDialog.ScanResults.DialogTitle"),
                                BaseMessages.getString(PKG, "TextFileInputDialog.ScanResults.DialogMessage"),
                                message, true);
                        etd.setReadOnly();
                        etd.open();
                    }
                }
            } else {
                MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
                mb.setMessage(BaseMessages.getString(PKG,
                        "TextFileInputDialog.UnableToReadHeaderLine.DialogMessage"));
                mb.setText(BaseMessages.getString(PKG, "System.Dialog.Error.Title"));
                mb.open();
            }
        } catch (IOException e) {
            new ErrorDialog(shell, BaseMessages.getString(PKG, "TextFileInputDialog.IOError.DialogTitle"),
                    BaseMessages.getString(PKG, "TextFileInputDialog.IOError.DialogMessage"), e);
        } catch (KettleException e) {
            new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"),
                    BaseMessages.getString(PKG, "TextFileInputDialog.ErrorGettingFileDesc.DialogMessage"), e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                // Ignore errors
            }
        }
    } else {
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
        mb.setMessage(BaseMessages.getString(PKG, "TextFileInputDialog.NoValidFileFound.DialogMessage"));
        mb.setText(BaseMessages.getString(PKG, "System.Dialog.Error.Title"));
        mb.open();
    }
}

From source file:org.pentaho.di.ui.trans.steps.fileinput.text.TextFileInputDialog.java

private List<String> getFirst(int nrlines, boolean skipHeaders) throws KettleException {
    TextFileInputMeta meta = new TextFileInputMeta();
    getInfo(meta, true);//from  w  w  w  .  java2  s . co  m
    FileInputList textFileList = meta.getFileInputList(transMeta);

    InputStream fi;
    CompressionInputStream f = null;
    StringBuilder lineStringBuilder = new StringBuilder(256);
    int fileFormatType = meta.getFileFormatTypeNr();

    List<String> retval = new ArrayList<>();

    if (textFileList.nrOfFiles() > 0) {
        FileObject file = textFileList.getFile(0);
        try {
            fi = KettleVFS.getInputStream(file);

            CompressionProvider provider = CompressionProviderFactory.getInstance()
                    .createCompressionProviderInstance(meta.content.fileCompression);
            f = provider.createInputStream(fi);

            InputStreamReader reader;
            if (meta.getEncoding() != null && meta.getEncoding().length() > 0) {
                reader = new InputStreamReader(f, meta.getEncoding());
            } else {
                reader = new InputStreamReader(f);
            }
            EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());

            int linenr = 0;
            int maxnr = nrlines + (meta.content.header ? meta.content.nrHeaderLines : 0);

            if (skipHeaders) {
                // Skip the header lines first if more then one, it helps us position
                if (meta.content.layoutPaged && meta.content.nrLinesDocHeader > 0) {
                    int skipped = 0;
                    String line = TextFileInputUtils.getLine(log, reader, encodingType, fileFormatType,
                            lineStringBuilder);
                    while (line != null && skipped < meta.content.nrLinesDocHeader - 1) {
                        skipped++;
                        line = TextFileInputUtils.getLine(log, reader, encodingType, fileFormatType,
                                lineStringBuilder);
                    }
                }

                // Skip the header lines first if more then one, it helps us position
                if (meta.content.header && meta.content.nrHeaderLines > 0) {
                    int skipped = 0;
                    String line = TextFileInputUtils.getLine(log, reader, encodingType, fileFormatType,
                            lineStringBuilder);
                    while (line != null && skipped < meta.content.nrHeaderLines - 1) {
                        skipped++;
                        line = TextFileInputUtils.getLine(log, reader, encodingType, fileFormatType,
                                lineStringBuilder);
                    }
                }
            }

            String line = TextFileInputUtils.getLine(log, reader, encodingType, fileFormatType,
                    lineStringBuilder);
            while (line != null && (linenr < maxnr || nrlines == 0)) {
                retval.add(line);
                linenr++;
                line = TextFileInputUtils.getLine(log, reader, encodingType, fileFormatType, lineStringBuilder);
            }
        } catch (Exception e) {
            throw new KettleException(
                    BaseMessages.getString(PKG, "TextFileInputDialog.Exception.ErrorGettingFirstLines",
                            "" + nrlines, file.getName().getURI()),
                    e);
        } finally {
            try {
                if (f != null) {
                    f.close();
                }
            } catch (Exception e) {
                // Ignore errors
            }
        }
    }

    return retval;
}

From source file:org.pentaho.di.ui.trans.steps.fileinput.text.TextFileInputDialog.java

private void getCSV() {
    TextFileInputMeta meta = new TextFileInputMeta();
    getInfo(meta, true);//from   w w  w  .j a  v  a2  s  .  c om

    // CSV without separator defined
    if (meta.content.fileType.equalsIgnoreCase("CSV")
            && (meta.content.separator == null || meta.content.separator.isEmpty())) {
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
        mb.setMessage(BaseMessages.getString(PKG, "TextFileInput.Exception.NoSeparator"));
        mb.setText(BaseMessages.getString(PKG, "TextFileInputDialog.DialogTitle"));
        mb.open();
        return;
    }

    TextFileInputMeta previousMeta = (TextFileInputMeta) meta.clone();
    FileInputList textFileList = meta.getFileInputList(transMeta);
    InputStream fileInputStream;
    CompressionInputStream inputStream = null;
    StringBuilder lineStringBuilder = new StringBuilder(256);
    int fileFormatType = meta.getFileFormatTypeNr();

    String delimiter = transMeta.environmentSubstitute(meta.content.separator);
    String enclosure = transMeta.environmentSubstitute(meta.content.enclosure);
    String escapeCharacter = transMeta.environmentSubstitute(meta.content.escapeCharacter);

    if (textFileList.nrOfFiles() > 0) {
        int clearFields = meta.content.header ? SWT.YES : SWT.NO;
        int nrInputFields = meta.inputFiles.inputFields.length;

        if (nrInputFields > 0) {
            MessageBox mb = new MessageBox(shell, SWT.YES | SWT.NO | SWT.CANCEL | SWT.ICON_QUESTION);
            mb.setMessage(BaseMessages.getString(PKG, "TextFileInputDialog.ClearFieldList.DialogMessage"));
            mb.setText(BaseMessages.getString(PKG, "TextFileInputDialog.ClearFieldList.DialogTitle"));
            clearFields = mb.open();
            if (clearFields == SWT.CANCEL) {
                return;
            }
        }

        try {
            wFields.table.removeAll();

            FileObject fileObject = textFileList.getFile(0);
            fileInputStream = KettleVFS.getInputStream(fileObject);
            Table table = wFields.table;

            CompressionProvider provider = CompressionProviderFactory.getInstance()
                    .createCompressionProviderInstance(meta.content.fileCompression);
            inputStream = provider.createInputStream(fileInputStream);

            InputStreamReader reader;
            if (meta.getEncoding() != null && meta.getEncoding().length() > 0) {
                reader = new InputStreamReader(inputStream, meta.getEncoding());
            } else {
                reader = new InputStreamReader(inputStream);
            }

            EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());

            // Scan the header-line, determine fields...
            String line = TextFileInputUtils.getLine(log, reader, encodingType, fileFormatType,
                    lineStringBuilder);
            if (line != null) {
                // Estimate the number of input fields...
                // Chop up the line using the delimiter
                String[] fields = TextFileInputUtils.guessStringsFromLine(transMeta, log, line, meta, delimiter,
                        enclosure, escapeCharacter);

                for (int i = 0; i < fields.length; i++) {
                    String field = fields[i];
                    if (field == null || field.length() == 0 || !meta.content.header) {
                        field = "Field" + (i + 1);
                    } else {
                        // Trim the field
                        field = Const.trim(field);
                        // Replace all spaces & - with underscore _
                        field = Const.replace(field, " ", "_");
                        field = Const.replace(field, "-", "_");
                    }

                    TableItem item = new TableItem(table, SWT.NONE);
                    item.setText(1, field);
                    item.setText(2, "String"); // The default type is String...
                }

                wFields.setRowNums();
                wFields.optWidth(true);

                // Copy it...
                getInfo(meta, true);

                // Sample a few lines to determine the correct type of the fields...
                String shellText = BaseMessages.getString(PKG, "TextFileInputDialog.LinesToSample.DialogTitle");
                String lineText = BaseMessages.getString(PKG,
                        "TextFileInputDialog.LinesToSample.DialogMessage");
                EnterNumberDialog end = new EnterNumberDialog(shell, 100, shellText, lineText);
                int samples = end.open();
                if (samples >= 0) {
                    getInfo(meta, true);

                    TextFileCSVImportProgressDialog pd = new TextFileCSVImportProgressDialog(shell, meta,
                            transMeta, reader, samples, clearFields == SWT.YES);
                    String message = pd.open();
                    if (message != null) {
                        wFields.removeAll();

                        // OK, what's the result of our search?
                        getData(meta);

                        // If we didn't want the list to be cleared, we need to re-inject the previous values...
                        //
                        if (clearFields == SWT.NO) {
                            getFieldsData(previousMeta, true);
                            wFields.table.setSelection(previousMeta.inputFiles.inputFields.length,
                                    wFields.table.getItemCount() - 1);
                        }

                        wFields.removeEmptyRows();
                        wFields.setRowNums();
                        wFields.optWidth(true);

                        EnterTextDialog etd = new EnterTextDialog(shell,
                                BaseMessages.getString(PKG, "TextFileInputDialog.ScanResults.DialogTitle"),
                                BaseMessages.getString(PKG, "TextFileInputDialog.ScanResults.DialogMessage"),
                                message, true);
                        etd.setReadOnly();
                        etd.open();
                    }
                }
            } else {
                MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
                mb.setMessage(BaseMessages.getString(PKG,
                        "TextFileInputDialog.UnableToReadHeaderLine.DialogMessage"));
                mb.setText(BaseMessages.getString(PKG, "System.Dialog.Error.Title"));
                mb.open();
            }
        } catch (IOException e) {
            new ErrorDialog(shell, BaseMessages.getString(PKG, "TextFileInputDialog.IOError.DialogTitle"),
                    BaseMessages.getString(PKG, "TextFileInputDialog.IOError.DialogMessage"), e);
        } catch (KettleException e) {
            new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"),
                    BaseMessages.getString(PKG, "TextFileInputDialog.ErrorGettingFileDesc.DialogMessage"), e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                // Ignore errors
            }
        }
    } else {
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
        mb.setMessage(BaseMessages.getString(PKG, "TextFileInputDialog.NoValidFileFound.DialogMessage"));
        mb.setText(BaseMessages.getString(PKG, "System.Dialog.Error.Title"));
        mb.open();
    }
}

From source file:org.regenstrief.util.Util.java

/**
 * Retrieves the system's default character encoding
 * //  w  w  w .j  a  va 2  s .  c  om
 * @return the system's default character encoding
 **/
public final static String getDefaultEncoding() {
    try {
        final InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(new byte[0]));
        final String enc = isr.getEncoding();

        isr.close();
        return enc;
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}