Example usage for java.nio.charset CodingErrorAction REPLACE

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

Introduction

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

Prototype

CodingErrorAction REPLACE

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

Click Source Link

Document

Action indicating that a coding error is to be handled by dropping the erroneous input, appending the coder's replacement value to the output buffer, and resuming the coding operation.

Usage

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

public static void main(String[] args) {

    printBanner();/*from www . ja  va2  s.co  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:Main.java

/**
 * Returns a cached thread-local {@link CharsetEncoder} for the specified
 * <tt>charset</tt>.//from  ww  w  .  j  a  v a2s.co  m
 */
public static CharsetEncoder getEncoder(Charset charset) {
    if (charset == null) {
        throw new NullPointerException("charset");
    }

    Map<Charset, CharsetEncoder> map = encoders.get();
    CharsetEncoder e = map.get(charset);
    if (e != null) {
        e.reset();
        e.onMalformedInput(CodingErrorAction.REPLACE);
        e.onUnmappableCharacter(CodingErrorAction.REPLACE);
        return e;
    }

    e = charset.newEncoder();
    e.onMalformedInput(CodingErrorAction.REPLACE);
    e.onUnmappableCharacter(CodingErrorAction.REPLACE);
    map.put(charset, e);
    return e;
}

From source file:Main.java

/**
 * Returns a cached thread-local {@link CharsetDecoder} for the specified
 * <tt>charset</tt>./*w w  w  . j a va  2s.c  o  m*/
 */
public static CharsetDecoder getDecoder(Charset charset) {
    if (charset == null) {
        throw new NullPointerException("charset");
    }

    Map<Charset, CharsetDecoder> map = decoders.get();
    CharsetDecoder d = map.get(charset);
    if (d != null) {
        d.reset();
        d.onMalformedInput(CodingErrorAction.REPLACE);
        d.onUnmappableCharacter(CodingErrorAction.REPLACE);
        return d;
    }

    d = charset.newDecoder();
    d.onMalformedInput(CodingErrorAction.REPLACE);
    d.onUnmappableCharacter(CodingErrorAction.REPLACE);
    map.put(charset, d);
    return d;
}

From source file:Main.java

/**
 * Returns a new byte array containing the characters of the specified
 * string encoded using the given charset.
 * //from w  w  w .  java 2  s .co  m
 * It is equivalent to <code>input.getBytes(charset)</code> except it has
 * workaround for the bug ID 61917.
 * 
 * @see https://code.google.com/p/android/issues/detail?id=61917
 */
//@formatter:off
/*
 * The original code is available from
 *     https://android.googlesource.com/platform/libcore/+/android-4.4_r1.2/libdvm/src/main/java/java/lang/String.java
 */
//@formatter:on
public static byte[] getBytes(String input, Charset charset) {
    CharBuffer chars = CharBuffer.wrap(input.toCharArray());
    // @formatter:off
    CharsetEncoder encoder = charset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
    // @formatter:on
    ByteBuffer buffer;
    buffer = encode(chars.asReadOnlyBuffer(), encoder);
    byte[] bytes = new byte[buffer.limit()];
    buffer.get(bytes);
    return bytes;

}

From source file:org.sonar.api.batch.fs.internal.charhandler.FileHashComputer.java

public FileHashComputer(String filePath) {
    encoder = StandardCharsets.UTF_8.newEncoder().onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
    this.filePath = filePath;
}

From source file:implementations.terrastoreDB.java

@Override
public int updateDB(String ID, String newValue) {
    int ret;//from ww w .ja v  a  2s .  c  o  m
    org.json.JSONObject myjson = null;
    try {
        myjson = XML.toJSONObject(newValue);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
    try {
        newValue = myjson.toString();
        CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder();
        utf8Decoder.onMalformedInput(CodingErrorAction.REPLACE);
        utf8Decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
        ByteBuffer bytes = ByteBuffer.wrap(newValue.getBytes("UTF8"));
        CharBuffer parsed = utf8Decoder.decode(bytes);
        client.bucket("test").key(ID).put(parsed.toString());
        ret = 1;
    } catch (Exception e) {
        ret = -1;
        e.printStackTrace();
    }
    return ret;
}

From source file:org.sonar.api.batch.fs.internal.charhandler.LineHashComputer.java

public LineHashComputer(LineHashConsumer consumer, File f) {
    this.consumer = consumer;
    this.file = f;
    this.encoder = StandardCharsets.UTF_8.newEncoder().onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
}

From source file:com.pnf.plugin.pdf.XFAParser.java

public void parse(byte[] xmlContent) throws ParserConfigurationException, SAXException, IOException {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    decoder.onMalformedInput(CodingErrorAction.REPLACE);
    decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    CharBuffer parsed = decoder.decode(ByteBuffer.wrap(xmlContent));

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();

    try (@SuppressWarnings("deprecation")
    InputStream is = new ReaderInputStream(new CharArrayReader(parsed.array()))) {
        parser.parse(is, xfa);// ww w .  j  av a2  s. c o  m
    } catch (Exception e) {
        logger.catching(e);
        logger.error("Error while parsing XFA content");
    }
}

From source file:org.nines.FullTextCleaner.java

public FullTextCleaner(String archiveName, ErrorReport errorReport, String custom) {
    this.errorReport = errorReport;
    this.archiveName = archiveName;
    this.log = Logger.getLogger(FullTextCleaner.class.getName());
    this.custom = custom;

    Charset cs = Charset.availableCharsets().get("UTF-8");
    this.decoder = cs.newDecoder();
    this.decoder.onMalformedInput(CodingErrorAction.REPLACE);
    this.decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
}

From source file:org.nines.RdfDocumentParser.java

private static String validateContent(File file, ErrorReport errorReport) {
    InputStreamReader is = null;// ww w  .  j  a v  a2s. co  m
    try {
        Charset cs = Charset.availableCharsets().get("UTF-8");
        CharsetDecoder decoder = cs.newDecoder();
        decoder.onMalformedInput(CodingErrorAction.REPLACE);
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);

        is = new InputStreamReader(new FileInputStream(file), decoder);
        String content = IOUtils.toString(is);

        // look for unescaped sequences and flag them as trouble
        String unescaped = StringEscapeUtils.unescapeXml(content);
        int startPos = 0;
        while (true) {
            int pos = unescaped.indexOf("&#", startPos);
            if (pos > -1) {
                String snip = unescaped.substring(Math.max(0, pos - 25),
                        Math.min(unescaped.length(), pos + 25));
                IndexerError e = new IndexerError(file.getName(), "",
                        "Potentially Invalid Escape sequence.\n   Position: [" + pos + "]\n   Snippet: [" + snip
                                + "]");
                errorReport.addError(e);
                startPos = pos + 2;
            } else {
                break;
            }
        }

        return content;
    } catch (IOException e) {
        errorReport
                .addError(new IndexerError(file.getName(), "", "Error validating content: " + e.getMessage()));
    } finally {
        IOUtils.closeQuietly(is);
    }
    return "";
}