Example usage for java.nio.charset CodingErrorAction REPORT

List of usage examples for java.nio.charset CodingErrorAction REPORT

Introduction

In this page you can find the example usage for java.nio.charset CodingErrorAction REPORT.

Prototype

CodingErrorAction REPORT

To view the source code for java.nio.charset CodingErrorAction REPORT.

Click Source Link

Document

Action indicating that a coding error is to be reported, either by returning a CoderResult object or by throwing a CharacterCodingException , whichever is appropriate for the method implementing the coding process.

Usage

From source file:Main.java

/**
 * Returns the {@code String} instance with detecting the Japanese encoding.
 * @throws UnsupportedEncodingException if it fails to detect the encoding.
 *//*from  w ww .j  a v  a2 s  . c  om*/
static String toStringWithEncodingDetection(ByteBuffer buffer) throws UnsupportedEncodingException {
    for (String encoding : JAPANESE_ENCODING_LIST) {
        buffer.position(0);
        try {
            Charset charset = Charset.forName(encoding);
            CharBuffer result = charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
                    .onUnmappableCharacter(CodingErrorAction.REPORT).decode(buffer);
            String str = result.toString();
            if (str.length() > 0 && str.charAt(0) == 0xFEFF) {
                // Remove leading BOM if necessary.
                str = str.substring(1);
            }
            return str;
        } catch (Exception e) {
            // Ignore exceptions, and retry next encoding.
        }
    }

    throw new UnsupportedEncodingException("Failed to detect encoding");
}

From source file:com.sforce.dataset.DatasetUtilMain.java

public static void main(String[] args) {

    printBanner();/* ww  w . j a  v  a  2s. c  o  m*/

    DatasetUtilParams params = new DatasetUtilParams();

    if (args.length > 0)
        params.server = false;

    System.out.println("");
    System.out.println("DatsetUtils called with {" + args.length + "} Params:");

    for (int i = 0; i < args.length; i++) {
        if ((i & 1) == 0) {
            System.out.print("{" + args[i] + "}");
        } else {
            if (i > 0 && args[i - 1].equalsIgnoreCase("--p"))
                System.out.println(":{*******}");
            else
                System.out.println(":{" + args[i] + "}");
        }

        if (i > 0 && args[i - 1].equalsIgnoreCase("--server")) {
            if (args[i] != null && args[i].trim().equalsIgnoreCase("false"))
                params.server = false;
            else if (args[i] != null && args[i].trim().equalsIgnoreCase("true"))
                params.server = true;
        }
    }
    System.out.println("");

    if (!printlneula(params.server)) {
        System.out.println(
                "You do not have permission to use this software. Please delete it from this computer");
        System.exit(-1);
    }

    String action = null;

    if (args.length >= 2) {
        for (int i = 1; i < args.length; i = i + 2) {
            if (args[i - 1].equalsIgnoreCase("--help") || args[i - 1].equalsIgnoreCase("-help")
                    || args[i - 1].equalsIgnoreCase("help")) {
                printUsage();
            } else if (args[i - 1].equalsIgnoreCase("--u")) {
                params.username = args[i];
            } else if (args[i - 1].equalsIgnoreCase("--p")) {
                params.password = args[i];
            } else if (args[i - 1].equalsIgnoreCase("--sessionId")) {
                params.sessionId = args[i];
            } else if (args[i - 1].equalsIgnoreCase("--token")) {
                params.token = args[i];
            } else if (args[i - 1].equalsIgnoreCase("--endpoint")) {
                params.endpoint = args[i];
            } else if (args[i - 1].equalsIgnoreCase("--action")) {
                action = args[i];
            } else if (args[i - 1].equalsIgnoreCase("--operation")) {
                if (args[i] != null) {
                    if (args[i].equalsIgnoreCase("overwrite")) {
                        params.Operation = args[i];
                    } else if (args[i].equalsIgnoreCase("upsert")) {
                        params.Operation = args[i];
                    } else if (args[i].equalsIgnoreCase("append")) {
                        params.Operation = args[i];
                    } else if (args[i].equalsIgnoreCase("delete")) {
                        params.Operation = args[i];
                    } else {
                        System.out.println("Invalid Operation {" + args[i]
                                + "} Must be Overwrite or Upsert or Append or Delete");
                        System.exit(-1);
                    }
                }
            } else if (args[i - 1].equalsIgnoreCase("--debug")) {
                params.debug = true;
                DatasetUtilConstants.debug = true;
            } else if (args[i - 1].equalsIgnoreCase("--ext")) {
                DatasetUtilConstants.ext = true;
            } else if (args[i - 1].equalsIgnoreCase("--inputFile")) {
                String tmp = args[i];
                if (tmp != null) {
                    File tempFile = new File(tmp);
                    if (tempFile.exists()) {
                        params.inputFile = tempFile.toString();
                    } else {
                        System.out.println("File {" + args[i] + "} does not exist");
                        System.exit(-1);
                    }
                }
            } else if (args[i - 1].equalsIgnoreCase("--dataset")) {
                params.dataset = args[i];
            } else if (args[i - 1].equalsIgnoreCase("--datasetLabel")) {
                params.datasetLabel = args[i];
            } else if (args[i - 1].equalsIgnoreCase("--app")) {
                params.app = args[i];
            } else if (args[i - 1].equalsIgnoreCase("--useBulkAPI")) {
                if (args[i] != null && args[i].trim().equalsIgnoreCase("true"))
                    params.useBulkAPI = true;
            } else if (args[i - 1].equalsIgnoreCase("--uploadFormat")) {
                if (args[i] != null && args[i].trim().equalsIgnoreCase("csv"))
                    params.uploadFormat = "csv";
                else if (args[i] != null && args[i].trim().equalsIgnoreCase("binary"))
                    params.uploadFormat = "binary";
            } else if (args[i - 1].equalsIgnoreCase("--rowLimit")) {
                if (args[i] != null && !args[i].trim().isEmpty())
                    params.rowLimit = (new BigDecimal(args[i].trim())).intValue();
            } else if (args[i - 1].equalsIgnoreCase("--rootObject")) {
                params.rootObject = args[i];
            } else if (args[i - 1].equalsIgnoreCase("--fileEncoding")) {
                params.fileEncoding = args[i];
            } else if (args[i - 1].equalsIgnoreCase("--server")) {
                if (args[i] != null && args[i].trim().equalsIgnoreCase("true"))
                    params.server = true;
                else if (args[i] != null && args[i].trim().equalsIgnoreCase("false"))
                    params.server = false;
            } else if (args[i - 1].equalsIgnoreCase("--codingErrorAction")) {
                if (args[i] != null) {
                    if (args[i].equalsIgnoreCase("IGNORE")) {
                        params.codingErrorAction = CodingErrorAction.IGNORE;
                    } else if (args[i].equalsIgnoreCase("REPORT")) {
                        params.codingErrorAction = CodingErrorAction.REPORT;
                    } else if (args[i].equalsIgnoreCase("REPLACE")) {
                        params.codingErrorAction = CodingErrorAction.REPLACE;
                    }
                }
            } else {
                printUsage();
                System.out.println("\nERROR: Invalid argument: " + args[i - 1]);
                System.exit(-1);
            }
        } //end for

        if (params.username != null) {
            if (params.endpoint == null || params.endpoint.isEmpty()) {
                params.endpoint = DatasetUtilConstants.defaultEndpoint;
            }
        }
    }

    if (params.server) {
        System.out.println();
        System.out.println("\n*******************************************************************************");
        try {
            DatasetUtilServer datasetUtilServer = new DatasetUtilServer();
            datasetUtilServer.init(args, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Server ended, exiting JVM.....");
        System.out.println("*******************************************************************************\n");
        System.out.println("QUITAPP");
        System.exit(0);
    }

    if (params.sessionId == null) {
        if (params.username == null || params.username.trim().isEmpty()) {
            params.username = getInputFromUser("Enter salesforce username: ", true, false);
        }

        if (params.username.equals("-1")) {
            params.sessionId = getInputFromUser("Enter salesforce sessionId: ", true, false);
            params.username = null;
            params.password = null;
        } else {
            if (params.password == null || params.password.trim().isEmpty()) {
                params.password = getInputFromUser("Enter salesforce password: ", true, true);
            }
        }
    }

    if (params.sessionId != null && !params.sessionId.isEmpty()) {
        while (params.endpoint == null || params.endpoint.trim().isEmpty()) {
            params.endpoint = getInputFromUser("Enter salesforce instance url: ", true, false);
            if (params.endpoint == null || params.endpoint.trim().isEmpty())
                System.out.println("\nERROR: endpoint must be specified when sessionId is specified");
        }

        while (params.endpoint.toLowerCase().contains("login.salesforce.com")
                || params.endpoint.toLowerCase().contains("test.salesforce.com")
                || params.endpoint.toLowerCase().contains("test")
                || params.endpoint.toLowerCase().contains("prod")
                || params.endpoint.toLowerCase().contains("sandbox")) {
            System.out.println("\nERROR: endpoint must be the actual serviceURL and not the login url");
            params.endpoint = getInputFromUser("Enter salesforce instance url: ", true, false);
        }
    } else {
        if (params.endpoint == null || params.endpoint.isEmpty()) {
            params.endpoint = getInputFromUser("Enter salesforce instance url (default=prod): ", false, false);
            if (params.endpoint == null || params.endpoint.trim().isEmpty()) {
                params.endpoint = DatasetUtilConstants.defaultEndpoint;
            }
        }
    }

    try {
        if (params.endpoint.equalsIgnoreCase("PROD") || params.endpoint.equalsIgnoreCase("PRODUCTION")) {
            params.endpoint = DatasetUtilConstants.defaultEndpoint;
        } else if (params.endpoint.equalsIgnoreCase("TEST") || params.endpoint.equalsIgnoreCase("SANDBOX")) {
            params.endpoint = DatasetUtilConstants.defaultEndpoint.replace("login", "test");
        }

        URL uri = new URL(params.endpoint);
        String protocol = uri.getProtocol();
        String host = uri.getHost();
        if (protocol == null || !protocol.equalsIgnoreCase("https")) {
            if (host == null || !(host.toLowerCase().endsWith("internal.salesforce.com")
                    || host.toLowerCase().endsWith("localhost"))) {
                System.out.println("\nERROR: Invalid endpoint. UNSUPPORTED_CLIENT: HTTPS Required in endpoint");
                System.exit(-1);
            }
        }

        if (uri.getPath() == null || uri.getPath().isEmpty() || uri.getPath().equals("/")) {
            uri = new URL(uri.getProtocol(), uri.getHost(), uri.getPort(),
                    DatasetUtilConstants.defaultSoapEndPointPath);
        }
        params.endpoint = uri.toString();
    } catch (MalformedURLException e) {
        e.printStackTrace();
        System.out.println("\nERROR: endpoint is not a valid URL");
        System.exit(-1);
    }

    PartnerConnection partnerConnection = null;
    if (params.username != null || params.sessionId != null) {
        try {
            partnerConnection = DatasetUtils.login(0, params.username, params.password, params.token,
                    params.endpoint, params.sessionId, params.debug);
        } catch (ConnectionException e) {
            e.printStackTrace();
            System.exit(-1);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    if (args.length == 0 || action == null) {
        //         System.out.println("\n*******************************************************************************");               
        ////         FileListenerUtil.startAllListener(partnerConnection);
        //         try {
        //            Thread.sleep(1000);
        //         } catch (InterruptedException e) {
        //         }
        //         System.out.println("*******************************************************************************\n");   
        //         System.out.println();         

        //         System.out.println("\n*******************************************************************************");               
        //           try {
        //              DatasetUtilServer datasetUtilServer = new DatasetUtilServer();
        //            datasetUtilServer.init(args, false);
        //         } catch (Exception e) {
        //            e.printStackTrace();
        //         }
        //         System.out.println("*******************************************************************************\n");   
        //         System.out.println();         

        while (true) {
            action = getActionFromUser();
            if (action == null || action.isEmpty()) {
                System.exit(-1);
            }
            params = new DatasetUtilParams();
            getRequiredParams(action, partnerConnection, params);
            @SuppressWarnings("unused")
            boolean status = doAction(action, partnerConnection, params);
            //            if(status)
            //            {
            //               if(action.equalsIgnoreCase("load") && params.debug)
            //                  createListener(partnerConnection, params);
            //            }
        }
    } else {
        doAction(action, partnerConnection, params);
    }

}

From source file:com.liferay.sync.engine.session.SyncManagedHttpClientConnectionFactory.java

@Override
public ManagedHttpClientConnection create(HttpRoute httpRoute, ConnectionConfig connectionConfig) {

    if (connectionConfig == null) {
        connectionConfig = ConnectionConfig.DEFAULT;
    }/*from www  . ja v  a2  s  . com*/

    CharsetDecoder charsetDecoder = null;
    CharsetEncoder charsetEncoder = null;

    Charset charset = connectionConfig.getCharset();

    if (charset != null) {
        charsetDecoder = charset.newDecoder();
        charsetEncoder = charset.newEncoder();

        CodingErrorAction malformedInputAction = connectionConfig.getMalformedInputAction();

        if (malformedInputAction == null) {
            malformedInputAction = CodingErrorAction.REPORT;
        }

        charsetDecoder.onMalformedInput(malformedInputAction);
        charsetEncoder.onMalformedInput(malformedInputAction);

        CodingErrorAction unmappableInputAction = connectionConfig.getUnmappableInputAction();

        if (unmappableInputAction == null) {
            unmappableInputAction = CodingErrorAction.REPORT;
        }

        charsetDecoder.onUnmappableCharacter(unmappableInputAction);
        charsetEncoder.onUnmappableCharacter(unmappableInputAction);
    }

    return new SyncManagedHttpClientConnection("http-outgoing-" + _counter.getAndIncrement(),
            connectionConfig.getBufferSize(), connectionConfig.getFragmentSizeHint(), charsetDecoder,
            charsetEncoder, connectionConfig.getMessageConstraints(), null, null, null,
            DefaultHttpResponseParserFactory.INSTANCE);
}

From source file:net.sf.yal10n.analyzer.ExploratoryEncodingTest.java

/**
 * Test how malformed input is reported.
 * @throws Exception any error//from   w  w w.ja va 2s.  co m
 */
@Test(expected = MalformedInputException.class)
public void testMalformedEncoding() throws Exception {
    ByteBuffer buffer = ByteBuffer.wrap(data);

    utf8.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT)
            .decode(buffer);
}

From source file:com.microsoft.tfs.jni.helpers.FileCopyHelper.java

/**
 * Copies a text file and any extended attributes, converting charsets along
 * the way. If any provided charsets are <code>null</code>, the default
 * charset is assumed./*from ww w .j  av a  2 s. c o m*/
 *
 * This method should not be used for binary files, instead
 * {@link FileCopyHelper#copy(String, String)} should be used.
 *
 * @param source
 *        The path to the source file
 * @param sourceCharset
 *        The charset of the source file (may be <code>null</code>)
 * @param destination
 *        The path to the target
 * @param destinationCharset
 *        The charset of the target (may be <code>null</code>)
 * @throws FileNotFoundException
 *         If the source does not exist, or the target's parent folder does
 *         not exist
 * @throws IOException
 *         If there was an IOException reading the source or writing the
 *         target
 */
public static void copyText(final String source, Charset sourceCharset, final String destination,
        Charset destinationCharset)
        throws FileNotFoundException, IOException, MalformedInputException, UnmappableCharacterException {
    Check.notNull(source, "source"); //$NON-NLS-1$
    Check.notNull(destination, "destination"); //$NON-NLS-1$

    if (sourceCharset == null) {
        sourceCharset = Charset.defaultCharset();
    }

    if (destinationCharset == null) {
        destinationCharset = Charset.defaultCharset();
    }

    Reader in = null;
    Writer out = null;

    try {
        final CharsetDecoder decoder = sourceCharset.newDecoder();
        decoder.onMalformedInput(CodingErrorAction.REPORT);
        decoder.onUnmappableCharacter(CodingErrorAction.REPORT);

        in = new BufferedReader(new InputStreamReader(new FileInputStream(source), decoder));
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destination), destinationCharset));

        copy(in, out);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (final IOException e) {
                log.warn(MessageFormat.format("Could not close {0} for reading: {1}", source, e.getMessage())); //$NON-NLS-1$
            }
        }

        if (out != null) {
            try {
                out.close();
            } catch (final IOException e) {
                log.warn(MessageFormat.format("Could not close {0} for writing: {1}", destination, //$NON-NLS-1$
                        e.getMessage()));
            }
        }
    }

    copyAttributes(source, destination);
}

From source file:net.sf.yal10n.analyzer.ExploratoryEncodingTest.java

/**
 * Test how malformed input is reported in a coder result.
 * @throws Exception any error/*  w  ww  .j av a 2  s . c o  m*/
 */
@Test
public void testMalformedEncodingResult() throws Exception {
    ByteBuffer buffer = ByteBuffer.wrap(data);

    CharsetDecoder decoder = utf8.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
            .onUnmappableCharacter(CodingErrorAction.REPORT);
    CharBuffer chars = CharBuffer.allocate(100);
    CoderResult result = decoder.decode(buffer, chars, false);
    Assert.assertTrue(result.isMalformed());
}

From source file:edu.ucdenver.ccp.nlp.ae.dict_util.GeneInfoToDictionary.java

public GeneInfoToDictionary(File geneFile) throws IOException {
    filterSingleLetterTerms = true;/*from w ww  . j a  v  a2 s . co  m*/
    namespacesToInclude = null;

    BufferedReader reader = null;
    try {

        // Create synonyms hash from 5th and 14th columns. They have duplicates, so create
        // a back hash so you can see how many id's share a particular other designation.
        // 5th col. is synonyms. 14th is Other Designations
        //reader = FileReaderUtil.initBufferedReader(geneFile, CharacterEncoding.UTF_8);
        CharsetDecoder csd = Charset.forName("UTF-8").newDecoder().onMalformedInput(CodingErrorAction.REPORT)
                .onUnmappableCharacter(CodingErrorAction.REPORT);
        InputStream ins = new FileInputStream(geneFile);
        reader = new BufferedReader(new InputStreamReader(ins, csd));
        reader.readLine();
        while (reader.ready()) {
            String line = reader.readLine();
            String[] parts = line.split("\t");
            String id = parts[1];
            String[] synonyms = parts[4].split("\\|");
            String[] other = parts[13].split("\\|");
            ArrayList<String> allSynonyms = new ArrayList<String>();

            if (!parts[4].equals("-")) {
                allSynonyms.addAll(Arrays.asList(synonyms));
            }
            if (!parts[13].equals("-")) {
                allSynonyms.addAll(Arrays.asList(other));
            }

            for (String syn : allSynonyms) {
                if (idToSynonymMap.get(id) == null) {
                    idToSynonymMap.put(id, new ArrayList<String>());
                }
                idToSynonymMap.get(id).add(syn);

                if (synonymToIdMap.get(syn) == null) {
                    synonymToIdMap.put(syn, new ArrayList<String>());
                }
                synonymToIdMap.get(syn).add(id);
            }
        }
    } finally {
        try {
            reader.close();
        } catch (Exception x) {
        }
    }

}

From source file:edu.ucdenver.ccp.nlp.ae.dict_util.OboToDictionary.java

public void convert(File oboFile, File outputFile)
            throws IOException, FileNotFoundException, OBOParseException {

        DefaultOBOParser parser = new DefaultOBOParser();
        OBOParseEngine engine = new OBOParseEngine(parser);
        List<String> paths = new ArrayList<String>();
        paths.add(oboFile.getAbsolutePath());
        engine.setPaths(paths);//www .jav a 2  s . co  m
        engine.parse();
        OBOSession session = parser.getSession();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile, true),
                Charset.forName("UTF-8").newEncoder().onMalformedInput(CodingErrorAction.REPORT)
                        .onUnmappableCharacter(CodingErrorAction.REPORT)));

        writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<synonym>");
        writer.newLine();

        buildEntries(session.getObjects(), writer);

        writer.write("</synonym>\n");
        writer.close();
    }

From source file:com.epam.reportportal.apache.http.impl.conn.ManagedHttpClientConnectionFactory.java

public ManagedHttpClientConnection create(final HttpRoute route, final ConnectionConfig config) {
    final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
    CharsetDecoder chardecoder = null;
    CharsetEncoder charencoder = null;
    final Charset charset = cconfig.getCharset();
    final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null
            ? cconfig.getMalformedInputAction()
            : CodingErrorAction.REPORT;
    final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null
            ? cconfig.getUnmappableInputAction()
            : CodingErrorAction.REPORT;
    if (charset != null) {
        chardecoder = charset.newDecoder();
        chardecoder.onMalformedInput(malformedInputAction);
        chardecoder.onUnmappableCharacter(unmappableInputAction);
        charencoder = charset.newEncoder();
        charencoder.onMalformedInput(malformedInputAction);
        charencoder.onUnmappableCharacter(unmappableInputAction);
    }/* ww w  . j  av  a2  s. c o m*/
    final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
    return new LoggingManagedHttpClientConnection(id, log, headerlog, wirelog, cconfig.getBufferSize(),
            cconfig.getFragmentSizeHint(), chardecoder, charencoder, cconfig.getMessageConstraints(), null,
            null, requestWriterFactory, responseParserFactory);
}

From source file:edu.ucdenver.ccp.nlp.ae.dict_util.GeneInfoToDictionary.java

public void convert(File geneFile, File outputFile) throws IOException, FileNotFoundException {

    BufferedReader reader = null;
    try {/*from w  ww.  j a v  a 2 s. c  om*/
        CharsetDecoder csd = Charset.forName("UTF-8").newDecoder().onMalformedInput(CodingErrorAction.REPORT)
                .onUnmappableCharacter(CodingErrorAction.REPORT);
        InputStream ins = new FileInputStream(geneFile);
        reader = new BufferedReader(new InputStreamReader(ins, csd));

        // Create Dictionary
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(outputFile, true),
                        Charset.forName("UTF-8").newEncoder().onMalformedInput(CodingErrorAction.REPORT)
                                .onUnmappableCharacter(CodingErrorAction.REPORT)));
        writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
        writer.write("<synonym>\n");

        reader.readLine();
        while (reader.ready()) {
            String line = reader.readLine();
            //Pair<id,name>
            ImmutablePair<String, String> data = parseLine(line);
            String entry = createEntry(data.left, data.right);
            writer.write(entry);
        }

        writer.write("</synonym>\n");
        writer.close();
    } finally {
        try {
            reader.close();
        } catch (Exception x) {
        }
    }
}