Search in sources :

Example 56 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project fastjson by alibaba.

the class SerialWriterStringEncoderTest2 method test_error_1.

public void test_error_1() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    CharsetEncoder realEncoder = charset.newEncoder();
    CharsetEncoder charsetEncoder = new MockCharsetEncoder(charset, realEncoder);
    Exception error = null;
    char[] chars = "abc".toCharArray();
    try {
        encode(charsetEncoder, chars, 0, chars.length);
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 57 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project nokogiri by sparklemotion.

the class SaveContextVisitor method encodeStringToHtmlEntity.

private CharSequence encodeStringToHtmlEntity(CharSequence text) {
    if (encoding == null)
        return text;
    CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
    StringBuilder sb = new StringBuilder(text.length() + 16);
    // make sure we can handle code points that are higher than 2 bytes
    for (int i = 0; i < text.length(); ) {
        int code = Character.codePointAt(text, i);
        // TODO not sure about bigger offset then 2 ?!
        int offset = code > 65535 ? 2 : 1;
        CharSequence substr = text.subSequence(i, i + offset);
        boolean canEncode = encoder.canEncode(substr);
        if (canEncode) {
            sb.append(substr);
        } else {
            sb.append("&#x").append(Integer.toHexString(code)).append(';');
        }
        i += offset;
    }
    return sb;
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 58 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project netty by netty.

the class ByteBufUtil method encodeString0.

static ByteBuf encodeString0(ByteBufAllocator alloc, boolean enforceHeap, CharBuffer src, Charset charset, int extraCapacity) {
    final CharsetEncoder encoder = CharsetUtil.encoder(charset);
    int length = (int) ((double) src.remaining() * encoder.maxBytesPerChar()) + extraCapacity;
    boolean release = true;
    final ByteBuf dst;
    if (enforceHeap) {
        dst = alloc.heapBuffer(length);
    } else {
        dst = alloc.buffer(length);
    }
    try {
        final ByteBuffer dstBuf = dst.internalNioBuffer(dst.readerIndex(), length);
        final int pos = dstBuf.position();
        CoderResult cr = encoder.encode(src, dstBuf, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = encoder.flush(dstBuf);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        dst.writerIndex(dst.writerIndex() + dstBuf.position() - pos);
        release = false;
        return dst;
    } catch (CharacterCodingException x) {
        throw new IllegalStateException(x);
    } finally {
        if (release) {
            dst.release();
        }
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 59 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project dbeaver by serge-rider.

the class FileRefDocumentProvider method doSaveDocument.

@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException {
    try {
        IStorage storage = EditorUtils.getStorageFromInput(element);
        File localFile = null;
        if (storage == null) {
            localFile = EditorUtils.getLocalFileFromInput(element);
            if (localFile == null) {
                throw new DBException("Can't obtain file from editor input");
            }
        }
        String encoding = (storage instanceof IEncodedStorage ? ((IEncodedStorage) storage).getCharset() : GeneralUtils.UTF8_ENCODING);
        Charset charset = Charset.forName(encoding);
        CharsetEncoder encoder = charset.newEncoder();
        encoder.onMalformedInput(CodingErrorAction.REPLACE);
        encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
        byte[] bytes;
        ByteBuffer byteBuffer = encoder.encode(CharBuffer.wrap(document.get()));
        if (byteBuffer.hasArray()) {
            bytes = byteBuffer.array();
        } else {
            bytes = new byte[byteBuffer.limit()];
            byteBuffer.get(bytes);
        }
        InputStream stream = new ByteArrayInputStream(bytes, 0, byteBuffer.limit());
        if (storage instanceof IFile) {
            IFile file = (IFile) storage;
            if (file.exists()) {
                // inform about the upcoming content change
                fireElementStateChanging(element);
                try {
                    file.setContents(stream, true, true, monitor);
                } catch (CoreException x) {
                    // inform about failure
                    fireElementStateChangeFailed(element);
                    throw x;
                } catch (RuntimeException x) {
                    // inform about failure
                    fireElementStateChangeFailed(element);
                    throw x;
                }
            } else {
                try {
                    monitor.beginTask("Save file '" + file.getName() + "'", 2000);
                    //ContainerCreator creator = new ContainerCreator(file.getWorkspace(), file.getParent().getFullPath());
                    //creator.createContainer(new SubProgressMonitor(monitor, 1000));
                    file.create(stream, false, monitor);
                } finally {
                    monitor.done();
                }
            }
        } else if (storage instanceof IPersistentStorage) {
            monitor.beginTask("Save document", 1);
            ((IPersistentStorage) storage).setContents(monitor, stream);
        } else if (localFile != null) {
            try (OutputStream os = new FileOutputStream(localFile)) {
                IOUtils.copyStream(stream, os);
            }
        } else {
            throw new DBException("Storage [" + storage + "] doesn't support save");
        }
    } catch (Exception e) {
        if (e instanceof CoreException) {
            throw (CoreException) e;
        } else {
            throw new CoreException(GeneralUtils.makeExceptionStatus(e));
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) Charset(java.nio.charset.Charset) IPersistentStorage(org.jkiss.dbeaver.ui.editors.IPersistentStorage) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) DBException(org.jkiss.dbeaver.DBException)

Example 60 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project XobotOS by xamarin.

the class Manifest method write.

/**
     * Writes out the attribute information of the specified manifest to the
     * specified {@code OutputStream}
     *
     * @param manifest
     *            the manifest to write out.
     * @param out
     *            The {@code OutputStream} to write to.
     * @throws IOException
     *             If an error occurs writing the {@code Manifest}.
     */
static void write(Manifest manifest, OutputStream out) throws IOException {
    CharsetEncoder encoder = Charsets.UTF_8.newEncoder();
    ByteBuffer buffer = ByteBuffer.allocate(LINE_LENGTH_LIMIT);
    String version = manifest.mainAttributes.getValue(Attributes.Name.MANIFEST_VERSION);
    if (version != null) {
        writeEntry(out, Attributes.Name.MANIFEST_VERSION, version, encoder, buffer);
        Iterator<?> entries = manifest.mainAttributes.keySet().iterator();
        while (entries.hasNext()) {
            Attributes.Name name = (Attributes.Name) entries.next();
            if (!name.equals(Attributes.Name.MANIFEST_VERSION)) {
                writeEntry(out, name, manifest.mainAttributes.getValue(name), encoder, buffer);
            }
        }
    }
    out.write(LINE_SEPARATOR);
    Iterator<String> i = manifest.getEntries().keySet().iterator();
    while (i.hasNext()) {
        String key = i.next();
        writeEntry(out, NAME_ATTRIBUTE, key, encoder, buffer);
        Attributes attrib = manifest.entries.get(key);
        Iterator<?> entries = attrib.keySet().iterator();
        while (entries.hasNext()) {
            Attributes.Name name = (Attributes.Name) entries.next();
            writeEntry(out, name, attrib.getValue(name), encoder, buffer);
        }
        out.write(LINE_SEPARATOR);
    }
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharsetEncoder (java.nio.charset.CharsetEncoder)84 ByteBuffer (java.nio.ByteBuffer)43 Charset (java.nio.charset.Charset)27 CharacterCodingException (java.nio.charset.CharacterCodingException)16 CharBuffer (java.nio.CharBuffer)15 CoderResult (java.nio.charset.CoderResult)13 ByteArrayInputStream (java.io.ByteArrayInputStream)6 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)6 IStatus (org.eclipse.core.runtime.IStatus)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)5 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)5 Attributes (java.util.jar.Attributes)5 CoreException (org.eclipse.core.runtime.CoreException)5 Status (org.eclipse.core.runtime.Status)5 OutputStreamWriter (java.io.OutputStreamWriter)4 SequenceInputStream (java.io.SequenceInputStream)4 BufferedWriter (java.io.BufferedWriter)3 OutputStream (java.io.OutputStream)3