Search in sources :

Example 71 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)

Example 72 with CharacterCodingException

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;
}
Also used : FastByteArrayOutputStream(org.apache.cassandra.io.util.FastByteArrayOutputStream) DataFormatException(java.util.zip.DataFormatException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Inflater(java.util.zip.Inflater) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 73 with CharacterCodingException

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();
        }
    }
}
Also used : Socket(org.zeromq.ZMQ.Socket) Context(org.zeromq.ZMQ.Context) ByteBuffer(java.nio.ByteBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 74 with CharacterCodingException

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();
        }
    }
}
Also used : Socket(org.zeromq.ZMQ.Socket) Context(org.zeromq.ZMQ.Context) ByteBuffer(java.nio.ByteBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 75 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)

Aggregations

CharacterCodingException (java.nio.charset.CharacterCodingException)90 ByteBuffer (java.nio.ByteBuffer)46 CharsetEncoder (java.nio.charset.CharsetEncoder)16 CharBuffer (java.nio.CharBuffer)15 IOException (java.io.IOException)14 CoderResult (java.nio.charset.CoderResult)13 CharsetDecoder (java.nio.charset.CharsetDecoder)11 Charset (java.nio.charset.Charset)10 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)8 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)6 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 CoreException (org.eclipse.core.runtime.CoreException)5 IStatus (org.eclipse.core.runtime.IStatus)5 Status (org.eclipse.core.runtime.Status)5 HumanReadableException (com.facebook.buck.util.HumanReadableException)4 SequenceInputStream (java.io.SequenceInputStream)4 Path (java.nio.file.Path)4 Date (java.util.Date)4