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;
}
use of java.nio.charset.CharacterCodingException in project eiger by wlloyd.
the class CassandraServer method uncompress.
private static String uncompress(ByteBuffer query, Compression compression) throws InvalidRequestException {
String queryString = null;
// Decompress the query string.
try {
switch(compression) {
case GZIP:
FastByteArrayOutputStream byteArray = new FastByteArrayOutputStream();
byte[] outBuffer = new byte[1024], inBuffer = new byte[1024];
Inflater decompressor = new Inflater();
int lenRead = 0;
while (true) {
if (decompressor.needsInput())
lenRead = query.remaining() < 1024 ? query.remaining() : 1024;
query.get(inBuffer, 0, lenRead);
decompressor.setInput(inBuffer, 0, lenRead);
int lenWrite = 0;
while ((lenWrite = decompressor.inflate(outBuffer)) != 0) byteArray.write(outBuffer, 0, lenWrite);
if (decompressor.finished())
break;
}
decompressor.end();
queryString = new String(byteArray.toByteArray(), 0, byteArray.size(), "UTF-8");
break;
case NONE:
try {
queryString = ByteBufferUtil.string(query);
} catch (CharacterCodingException ex) {
throw new InvalidRequestException(ex.getMessage());
}
break;
}
} catch (DataFormatException e) {
throw new InvalidRequestException("Error deflating query string.");
} catch (UnsupportedEncodingException e) {
throw new InvalidRequestException("Unknown query string encoding.");
}
return queryString;
}
use of java.nio.charset.CharacterCodingException in project jeromq by zeromq.
the class TestZMQ method testByteBufferLargeDirect.
@Test
public void testByteBufferLargeDirect() throws InterruptedException, IOException, CharacterCodingException {
int port = Utils.findOpenPort();
ZMQ.Context context = ZMQ.context(1);
int[] array = new int[2048 * 2000];
for (int i = 0; i < array.length; ++i) {
array[i] = i;
}
ByteBuffer bSend = ByteBuffer.allocateDirect(Integer.SIZE / 8 * array.length).order(ByteOrder.nativeOrder());
bSend.asIntBuffer().put(array);
ByteBuffer bRec = ByteBuffer.allocateDirect(bSend.capacity()).order(ByteOrder.nativeOrder());
int[] recArray = new int[array.length];
ZMQ.Socket push = null;
ZMQ.Socket pull = null;
try {
push = context.socket(ZMQ.PUSH);
pull = context.socket(ZMQ.PULL);
pull.bind("tcp://*:" + port);
push.connect("tcp://localhost:" + port);
push.sendByteBuffer(bSend, 0);
pull.recvByteBuffer(bRec, 0);
bRec.flip();
bRec.asIntBuffer().get(recArray);
assertArrayEquals(array, recArray);
} finally {
try {
push.close();
} catch (Exception ignore) {
ignore.printStackTrace();
}
try {
pull.close();
} catch (Exception ignore) {
ignore.printStackTrace();
}
try {
context.term();
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
}
use of java.nio.charset.CharacterCodingException in project jeromq by zeromq.
the class TestZMQ method testByteBufferLarge.
@Test
public void testByteBufferLarge() throws InterruptedException, IOException, CharacterCodingException {
int port = Utils.findOpenPort();
ZMQ.Context context = ZMQ.context(1);
int[] array = new int[2048 * 2000];
for (int i = 0; i < array.length; ++i) {
array[i] = i;
}
ByteBuffer bSend = ByteBuffer.allocate(Integer.SIZE / 8 * array.length).order(ByteOrder.nativeOrder());
bSend.asIntBuffer().put(array);
ByteBuffer bRec = ByteBuffer.allocate(bSend.capacity()).order(ByteOrder.nativeOrder());
int[] recArray = new int[array.length];
ZMQ.Socket push = null;
ZMQ.Socket pull = null;
try {
push = context.socket(ZMQ.PUSH);
pull = context.socket(ZMQ.PULL);
pull.bind("tcp://*:" + port);
push.connect("tcp://localhost:" + port);
push.sendByteBuffer(bSend, 0);
pull.recvByteBuffer(bRec, 0);
bRec.flip();
bRec.asIntBuffer().get(recArray);
assertArrayEquals(array, recArray);
} finally {
try {
push.close();
} catch (Exception ignore) {
ignore.printStackTrace();
}
try {
pull.close();
} catch (Exception ignore) {
ignore.printStackTrace();
}
try {
context.term();
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
}
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();
}
}
Aggregations