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();
}
}
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 + "))");
}
}
}
}
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();
}
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);
}
}
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;
}
Aggregations