Search in sources :

Example 26 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project jdk8u_jdk by JetBrains.

the class StringCoding method decode.

static char[] decode(Charset cs, byte[] ba, int off, int len) {
    // (1)We never cache the "external" cs, the only benefit of creating
    // an additional StringDe/Encoder object to wrap it is to share the
    // de/encode() method. These SD/E objects are short-lifed, the young-gen
    // gc should be able to take care of them well. But the best approash
    // is still not to generate them if not really necessary.
    // (2)The defensive copy of the input byte/char[] has a big performance
    // impact, as well as the outgoing result byte/char[]. Need to do the
    // optimization check of (sm==null && classLoader0==null) for both.
    // (3)getClass().getClassLoader0() is expensive
    // (4)There might be a timing gap in isTrusted setting. getClassLoader0()
    // is only chcked (and then isTrusted gets set) when (SM==null). It is
    // possible that the SM==null for now but then SM is NOT null later
    // when safeTrim() is invoked...the "safe" way to do is to redundant
    // check (... && (isTrusted || SM == null || getClassLoader0())) in trim
    // but it then can be argued that the SM is null when the opertaion
    // is started...
    CharsetDecoder cd = cs.newDecoder();
    int en = scale(len, cd.maxCharsPerByte());
    char[] ca = new char[en];
    if (len == 0)
        return ca;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ba = Arrays.copyOfRange(ba, off, off + len);
            off = 0;
        }
    }
    cd.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset();
    if (cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder) cd).decode(ba, off, len, ca);
        return safeTrim(ca, clen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
        CharBuffer cb = CharBuffer.wrap(ca);
        try {
            CoderResult cr = cd.decode(bb, cb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = cd.flush(cb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            // so this shouldn't happen
            throw new Error(x);
        }
        return safeTrim(ca, cb.position(), cs, isTrusted);
    }
}
Also used : ArrayDecoder(sun.nio.cs.ArrayDecoder) CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 27 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project jdk8u_jdk by JetBrains.

the class StringCoding method encode.

static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca = Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder) ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) ArrayEncoder(sun.nio.cs.ArrayEncoder) CoderResult(java.nio.charset.CoderResult)

Example 28 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project eclipse.platform.text by eclipse.

the class FileDocumentProvider method doSaveDocument.

@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException {
    if (element instanceof IFileEditorInput) {
        IFileEditorInput input = (IFileEditorInput) element;
        String encoding = null;
        FileInfo info = (FileInfo) getElementInfo(element);
        IFile file = input.getFile();
        encoding = getCharsetForNewFile(file, document, info);
        if (info != null && info.fBOM == IContentDescription.BOM_UTF_16LE && CHARSET_UTF_16.equals(encoding))
            encoding = CHARSET_UTF_16LE;
        Charset charset;
        try {
            charset = Charset.forName(encoding);
        } catch (UnsupportedCharsetException ex) {
            String message = NLSUtility.format(TextEditorMessages.DocumentProvider_error_unsupported_encoding_message_arg, encoding);
            IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, ex);
            throw new CoreException(s);
        } catch (IllegalCharsetNameException ex) {
            String message = NLSUtility.format(TextEditorMessages.DocumentProvider_error_illegal_encoding_message_arg, encoding);
            IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, ex);
            throw new CoreException(s);
        }
        CharsetEncoder encoder = charset.newEncoder();
        encoder.onMalformedInput(CodingErrorAction.REPLACE);
        encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
        InputStream stream;
        try {
            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);
            }
            stream = new ByteArrayInputStream(bytes, 0, byteBuffer.limit());
        } catch (CharacterCodingException ex) {
            Assert.isTrue(ex instanceof UnmappableCharacterException);
            String message = NLSUtility.format(TextEditorMessages.DocumentProvider_error_charset_mapping_failed_message_arg, encoding);
            IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, EditorsUI.CHARSET_MAPPING_FAILED, message, null);
            throw new CoreException(s);
        }
        /*
			 * XXX:
			 * This is a workaround for a corresponding bug in Java readers and writer,
			 * see http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
			 */
        if (info != null && info.fBOM == IContentDescription.BOM_UTF_8 && CHARSET_UTF_8.equals(encoding))
            stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_8), stream);
        if (info != null && info.fBOM == IContentDescription.BOM_UTF_16LE && CHARSET_UTF_16LE.equals(encoding))
            stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_16LE), stream);
        if (file.exists()) {
            if (info != null && !overwrite)
                checkSynchronizationState(info.fModificationStamp, file);
            // inform about the upcoming content change
            fireElementStateChanging(element);
            try {
                file.setContents(stream, overwrite, true, monitor);
            } catch (CoreException x) {
                // inform about failure
                fireElementStateChangeFailed(element);
                throw x;
            } catch (RuntimeException x) {
                // inform about failure
                fireElementStateChangeFailed(element);
                throw x;
            }
            if (info != null) {
                ResourceMarkerAnnotationModel model = (ResourceMarkerAnnotationModel) info.fModel;
                if (model != null)
                    model.updateMarkers(info.fDocument);
                info.fModificationStamp = computeModificationStamp(file);
            }
        } else {
            SubMonitor subMonitor = SubMonitor.convert(monitor, TextEditorMessages.FileDocumentProvider_task_saving, 2);
            ContainerCreator creator = new ContainerCreator(file.getWorkspace(), file.getParent().getFullPath());
            creator.createContainer(subMonitor.split(1));
            file.create(stream, false, subMonitor.split(1));
        }
    } else {
        super.doSaveDocument(monitor, element, document, overwrite);
    }
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IResourceStatus(org.eclipse.core.resources.IResourceStatus) ResourceMarkerAnnotationModel(org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel) IStatus(org.eclipse.core.runtime.IStatus) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) SubMonitor(org.eclipse.core.runtime.SubMonitor) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) CoreException(org.eclipse.core.runtime.CoreException) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IFileEditorInput(org.eclipse.ui.IFileEditorInput) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) ContainerCreator(org.eclipse.core.filebuffers.manipulation.ContainerCreator)

Example 29 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project linuxtools by eclipse.

the class RpmPackageProposalsList method getRpmInfo.

public String getRpmInfo(String pkgName) {
    // $NON-NLS-1$
    String ret = "";
    try {
        ret = // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
        Utils.runCommandToString(// $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
        "rpm", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
        "-q", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
        pkgName, // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
        "--qf", getformattedRpmInformations());
    } catch (IOException e) {
        SpecfileLog.logError(e);
        return Messages.RpmPackageProposalsList_2 + Messages.RpmPackageProposalsList_3;
    }
    // Create encoder and decoder
    // $NON-NLS-1$
    CharsetDecoder decoder = Charset.forName(System.getProperty("file.encoding")).newDecoder();
    /*
		 * TODO: Jcharset may be used to detect the inputstream encoding if it's
		 * required? http://jchardet.sourceforge.net
		 */
    // $NON-NLS-1$
    CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();
    try {
        ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(ret));
        CharBuffer cbuf = decoder.decode(bbuf);
        ret = cbuf.toString();
    } catch (CharacterCodingException e) {
    // If an error occurs when re-encoding the output, the original
    // output is returned.
    }
    return ret;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 30 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project checker-framework by typetools.

the class StringCoding method decode.

static char[] decode(Charset cs, byte[] ba, int off, int len) {
    // (1)We never cache the "external" cs, the only benefit of creating
    // an additional StringDe/Encoder object to wrap it is to share the
    // de/encode() method. These SD/E objects are short-lifed, the young-gen
    // gc should be able to take care of them well. But the best approash
    // is still not to generate them if not really necessary.
    // (2)The defensive copy of the input byte/char[] has a big performance
    // impact, as well as the outgoing result byte/char[]. Need to do the
    // optimization check of (sm==null && classLoader0==null) for both.
    // (3)getClass().getClassLoader0() is expensive
    // (4)There might be a timing gap in isTrusted setting. getClassLoader0()
    // is only chcked (and then isTrusted gets set) when (SM==null). It is
    // possible that the SM==null for now but then SM is NOT null later
    // when safeTrim() is invoked...the "safe" way to do is to redundant
    // check (... && (isTrusted || SM == null || getClassLoader0())) in trim
    // but it then can be argued that the SM is null when the opertaion
    // is started...
    CharsetDecoder cd = cs.newDecoder();
    int en = scale(len, cd.maxCharsPerByte());
    char[] ca = new char[en];
    if (len == 0)
        return ca;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ba = Arrays.copyOfRange(ba, off, off + len);
            off = 0;
        }
    }
    cd.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset();
    if (cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder) cd).decode(ba, off, len, ca);
        return safeTrim(ca, clen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
        CharBuffer cb = CharBuffer.wrap(ca);
        try {
            CoderResult cr = cd.decode(bb, cb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = cd.flush(cb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            // so this shouldn't happen
            throw new Error(x);
        }
        return safeTrim(ca, cb.position(), cs, isTrusted);
    }
}
Also used : ArrayDecoder(sun.nio.cs.ArrayDecoder) CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Aggregations

CharacterCodingException (java.nio.charset.CharacterCodingException)196 ByteBuffer (java.nio.ByteBuffer)114 CharBuffer (java.nio.CharBuffer)48 CharsetDecoder (java.nio.charset.CharsetDecoder)44 IOException (java.io.IOException)34 CharsetEncoder (java.nio.charset.CharsetEncoder)31 CoderResult (java.nio.charset.CoderResult)30 Charset (java.nio.charset.Charset)27 InputStream (java.io.InputStream)9 Date (java.util.Date)9 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)8 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)6 Path (java.nio.file.Path)6 ParseException (java.text.ParseException)6 Test (org.junit.Test)6 CoreException (org.eclipse.core.runtime.CoreException)5 HumanReadableException (com.facebook.buck.util.HumanReadableException)4 OutputStream (java.io.OutputStream)4