Search in sources :

Example 66 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project robovm by robovm.

the class Charset_TestGenerator method genEncoded.

static void genEncoded(Charset charset, CharBuffer cb) {
    System.out.println(charset.name());
    Dumper out = new Dumper1();
    CharsetEncoder encoder = charset.newEncoder();
    encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    try {
        ByteBuffer bb = encoder.encode(cb);
        //            bb.rewind();
        while (bb.hasRemaining()) {
            out.consume(bb.get());
        }
    } catch (CharacterCodingException e) {
        System.out.println(e);
    //                e.printStackTrace();
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 67 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project robovm by robovm.

the class OldCharset_AbstractTest method NNtest_CodecDynamicIndividuals.

public void NNtest_CodecDynamicIndividuals() throws CharacterCodingException {
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    for (int code = 32; code <= 65533; code++) {
        if (encoder.canEncode((char) code)) {
            //                inputCB.rewind();
            CharBuffer inputCB = CharBuffer.allocate(1);
            inputCB.put((char) code);
            inputCB.rewind();
            ByteBuffer intermediateBB = encoder.encode(inputCB);
            inputCB.rewind();
            intermediateBB.rewind();
            try {
                CharBuffer outputCB = decoder.decode(intermediateBB);
                outputCB.rewind();
                assertEqualCBs("decode(encode(A)) must be identical with A = " + code, inputCB, outputCB);
                if (code == 165) {
                    outputCB.rewind();
                    System.out.println("WOW:" + outputCB.get());
                }
            } catch (CharacterCodingException e) {
                fail("failed to decode(encode(" + code + "))");
            }
        }
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 68 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project mockito by mockito.

the class InvocationInfoTest method should_know_valid_throwables.

@Test
public void should_know_valid_throwables() throws Exception {
    //when
    Invocation invocation = new InvocationBuilder().method("canThrowException").toInvocation();
    InvocationInfo info = new InvocationInfo(invocation);
    //then
    assertThat(info.isValidException(new Exception())).isFalse();
    assertThat(info.isValidException(new CharacterCodingException())).isTrue();
}
Also used : Invocation(org.mockito.invocation.Invocation) TestBase.getLastInvocation(org.mockitoutil.TestBase.getLastInvocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) CharacterCodingException(java.nio.charset.CharacterCodingException) CharacterCodingException(java.nio.charset.CharacterCodingException) Test(org.junit.Test)

Example 69 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project spring-security-oauth by spring-projects.

the class Hex method utf8Encode.

/**
	 * UTF-8 encoding/decoding. Using a charset rather than `String.getBytes` is less forgiving
	 * and will raise an exception for invalid data.
	 */
public static byte[] utf8Encode(CharSequence string) {
    try {
        ByteBuffer bytes = UTF8.newEncoder().encode(CharBuffer.wrap(string));
        byte[] bytesCopy = new byte[bytes.limit()];
        System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit());
        return bytesCopy;
    } catch (CharacterCodingException e) {
        throw new RuntimeException(e);
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 70 with CharacterCodingException

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

the class DiffContentFactoryImpl method createFromBytesImpl.

@NotNull
private static DocumentContent createFromBytesImpl(@Nullable Project project, @NotNull byte[] content, @NotNull FileType fileType, @NotNull String fileName, @Nullable VirtualFile highlightFile, @NotNull Charset charset) {
    if (fileType.isBinary()) {
        fileType = PlainTextFileType.INSTANCE;
        Charset guessedCharset = guessCharsetFromContent(content);
        if (guessedCharset != null)
            charset = guessedCharset;
    }
    Charset bomCharset = CharsetToolkit.guessFromBOM(content);
    boolean isBOM = bomCharset != null;
    if (isBOM)
        charset = bomCharset;
    boolean malformedContent = false;
    String text;
    try {
        text = CharsetToolkit.tryDecodeString(content, charset);
    } catch (CharacterCodingException e) {
        text = CharsetToolkit.decodeString(content, charset);
        malformedContent = true;
    }
    DocumentContent documentContent = createImpl(project, text, fileType, fileName, highlightFile, charset, isBOM, true, true);
    if (malformedContent) {
        String notificationText = "Content was decoded with errors (using " + "'" + charset.name() + "' charset)";
        DiffUtil.addNotification(DiffNotifications.createNotification(notificationText, LightColors.RED), documentContent);
    }
    return documentContent;
}
Also used : Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CharacterCodingException (java.nio.charset.CharacterCodingException)80 ByteBuffer (java.nio.ByteBuffer)39 CoderResult (java.nio.charset.CoderResult)13 CharBuffer (java.nio.CharBuffer)12 IOException (java.io.IOException)10 CharsetEncoder (java.nio.charset.CharsetEncoder)10 CharsetDecoder (java.nio.charset.CharsetDecoder)9 HumanReadableException (com.facebook.buck.util.HumanReadableException)4 Charset (java.nio.charset.Charset)4 Date (java.util.Date)4 Path (java.nio.file.Path)3 ParseException (java.text.ParseException)3 Test (org.junit.Test)3 Portfolio (com.datastax.demo.portfolio.Portfolio)2 Point (java.awt.Point)2 OutputStream (java.io.OutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)2 MalformedInputException (java.nio.charset.MalformedInputException)2 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)2