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:org.apache.synapse.transport.utils.config.HttpTransportConfiguration.java

private CodingErrorAction getCodingErrorAction(String action) {
    if ("report".equals(action)) {
        return CodingErrorAction.REPORT;
    } else if ("ignore".equals(action)) {
        return CodingErrorAction.IGNORE;
    } else if ("replace".equals(action)) {
        return CodingErrorAction.REPLACE;
    } else {/*  w ww.j ava  2 s  . co  m*/
        return CodingErrorAction.REPORT;
    }
}

From source file:org.apache.tajo.util.StringUtils.java

public static char[] convertBytesToChars(byte[] src, Charset charset) {
    CharsetDecoder decoder = charset.newDecoder();
    char[] resultArray = new char[(int) (src.length * decoder.maxCharsPerByte())];

    if (src.length != 0) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(src);
        CharBuffer charBuffer = CharBuffer.wrap(resultArray);

        decoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
        decoder.reset();/*ww w .  j a  v a 2  s . c om*/

        CoderResult coderResult = decoder.decode(byteBuffer, charBuffer, true);
        if (coderResult.isUnderflow()) {
            coderResult = decoder.flush(charBuffer);

            if (coderResult.isUnderflow()) {
                if (resultArray.length != charBuffer.position()) {
                    resultArray = Arrays.copyOf(resultArray, charBuffer.position());
                }
            }
        }
    }

    return resultArray;
}

From source file:org.apache.arrow.vector.util.Text.java

private static String decode(ByteBuffer utf8, boolean replace) throws CharacterCodingException {
    CharsetDecoder decoder = DECODER_FACTORY.get();
    if (replace) {
        decoder.onMalformedInput(java.nio.charset.CodingErrorAction.REPLACE);
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    }//from   w  w w  . j  a  va2  s .  co m
    String str = decoder.decode(utf8).toString();
    // set decoder back to its default value: REPORT
    if (replace) {
        decoder.onMalformedInput(CodingErrorAction.REPORT);
        decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return str;
}

From source file:org.cloudata.core.common.io.CText.java

private static String decode(ByteBuffer utf8, boolean replace) throws CharacterCodingException {
    synchronized (DECODER) {
        if (replace) {
            DECODER.onMalformedInput(java.nio.charset.CodingErrorAction.REPLACE);
            DECODER.onUnmappableCharacter(CodingErrorAction.REPLACE);
        }/*  w w  w.  jav  a 2 s.  c om*/
        String str = DECODER.decode(utf8).toString();
        // set decoder back to its default value: REPORT
        if (replace) {
            DECODER.onMalformedInput(CodingErrorAction.REPORT);
            DECODER.onUnmappableCharacter(CodingErrorAction.REPORT);
        }
        return str;
    }

}

From source file:org.apache.tajo.util.StringUtils.java

public static byte[] convertCharsToBytes(char[] src, Charset charset) {
    CharsetEncoder encoder = charset.newEncoder();
    byte[] resultArray = new byte[(int) (src.length * encoder.maxBytesPerChar())];

    if (src.length != 0) {
        CharBuffer charBuffer = CharBuffer.wrap(src);
        ByteBuffer byteBuffer = ByteBuffer.wrap(resultArray);

        encoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
        encoder.reset();//from  w w w.  j a va2 s.  c  o m

        CoderResult coderResult = encoder.encode(charBuffer, byteBuffer, true);
        if (coderResult.isUnderflow()) {
            coderResult = encoder.flush(byteBuffer);

            if (coderResult.isUnderflow()) {
                if (resultArray.length != byteBuffer.position()) {
                    resultArray = Arrays.copyOf(resultArray, byteBuffer.position());
                }
            }
        }
    }

    return resultArray;
}

From source file:org.apache.arrow.vector.util.Text.java

/**
 * Converts the provided String to bytes using the UTF-8 encoding. If <code>replace</code> is true, then malformed
 * input is replaced with the substitution character, which is U+FFFD. Otherwise the method throws a
 * MalformedInputException.//  w w w.  j  av  a2 s.c o m
 *
 * @return ByteBuffer: bytes stores at ByteBuffer.array() and length is ByteBuffer.limit()
 */
public static ByteBuffer encode(String string, boolean replace) throws CharacterCodingException {
    CharsetEncoder encoder = ENCODER_FACTORY.get();
    if (replace) {
        encoder.onMalformedInput(CodingErrorAction.REPLACE);
        encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    }
    ByteBuffer bytes = encoder.encode(CharBuffer.wrap(string.toCharArray()));
    if (replace) {
        encoder.onMalformedInput(CodingErrorAction.REPORT);
        encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return bytes;
}

From source file:com.hpe.application.automation.tools.run.RunLoadRunnerScript.java

private void createHtmlReports(FilePath buildWorkDir, String scriptName, FilePath outputHTML,
        FilePath xsltOnNode) throws IOException, InterruptedException, XMLStreamException {
    if (!buildWorkDir.exists()) {
        throw new IllegalArgumentException("Build worker doesn't exist");
    }//from   w ww  . ja va  2 s.  co m
    if ("".equals(scriptName)) {
        throw new IllegalArgumentException("Script name is empty");
    }
    if (!xsltOnNode.exists()) {
        throw new IllegalArgumentException("LR Html report doesn't exist on the node");
    }
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        StreamSource xslStream = new StreamSource(xsltOnNode.read());
        Transformer transformer = factory.newTransformer(xslStream);

        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
        decoder.onMalformedInput(CodingErrorAction.REPLACE).replacement();

        final InputStreamReader inputStreamReader = new InputStreamReader(
                new BOMInputStream(buildWorkDir.child(scriptName).child("Results.xml").read()), decoder);

        StreamSource in = new StreamSource(new LrScriptResultsSanitizer(inputStreamReader));
        StreamResult out = new StreamResult(outputHTML.write());
        transformer.transform(in, out);
        final URL lrHtmlCSSPath = jenkinsInstance.pluginManager.uberClassLoader.getResource(LR_SCRIPT_HTML_CSS);
        if (lrHtmlCSSPath == null) {
            throw new LrScriptParserException(
                    "For some reason the jenkins instance is null - is it an improper set tests?");
        }

        FilePath lrScriptHtmlReportCss = buildWorkDir.child(scriptName).child(LR_SCRIPT_HTML_REPORT_CSS);
        lrScriptHtmlReportCss.copyFrom(lrHtmlCSSPath);

        logger.println("The generated HTML file is:" + outputHTML);
    } catch (TransformerConfigurationException e) {
        logger.println("TransformerConfigurationException");
        logger.println(e);
    } catch (TransformerException e) {
        logger.println("TransformerException");
        logger.println(e);
    } catch (LrScriptParserException e) {
        logger.println("General exception");
        logger.println(e);
    }
}

From source file:org.cloudata.core.common.io.CText.java

/**
 * Converts the provided String to bytes using the
 * UTF-8 encoding. If <code>replace</code> is true, then
 * malformed input is replaced with the/*w  w w.  ja v a  2 s.c  o  m*/
 * substitution character, which is U+FFFD. Otherwise the
 * method throws a MalformedInputException.
 * @return ByteBuffer: bytes stores at ByteBuffer.array() 
 *                     and length is ByteBuffer.limit()
 */
public static ByteBuffer encode(String string, boolean replace) throws CharacterCodingException {
    synchronized (ENCODER) {
        if (replace) {
            ENCODER.onMalformedInput(CodingErrorAction.REPLACE);
            ENCODER.onUnmappableCharacter(CodingErrorAction.REPLACE);
        }
        ByteBuffer bytes = ENCODER.encode(CharBuffer.wrap(string.toCharArray()));
        if (replace) {
            ENCODER.onMalformedInput(CodingErrorAction.REPORT);
            ENCODER.onUnmappableCharacter(CodingErrorAction.REPORT);
        }
        return bytes;
    }
}

From source file:org.apache.tika.parser.rtf.TextExtractor.java

private CharsetDecoder getDecoder() throws TikaException {
    Charset charset = getCharset();

    // Common case: charset is same as last time, so
    // just reuse it:
    if (lastCharset == null || !charset.equals(lastCharset)) {
        decoder = charset.newDecoder();/*  w w w .j av  a 2  s  . co  m*/
        decoder.onMalformedInput(CodingErrorAction.REPLACE);
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
        lastCharset = charset;
    }

    return decoder;
}