Search in sources :

Example 26 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project stanbol by apache.

the class AnalyzedTextWriter method writeTo.

@Override
public void writeTo(AnalysedText at, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    String charsetName = mediaType.getParameters().get("charset");
    Charset charset = null;
    if (charsetName != null) {
        try {
            charset = Charset.forName(charsetName);
        } catch (IllegalCharsetNameException e) {
            log.warn("Unable to use charset defined by the parsed MediaType '" + mediaType + "! Fallback to default (UTF-8).", e);
        } catch (UnsupportedCharsetException e) {
            log.warn("Charset defined by the parsed MediaType '" + mediaType + " is not supported! Fallback to default (UTF-8).", e);
        }
    }
    getSerializer().serialize(at, entityStream, charset);
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset)

Example 27 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project ceylon-compiler by ceylon.

the class BaseFileManager method decode.

public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
    String encodingName = getEncodingName();
    CharsetDecoder decoder;
    try {
        decoder = getDecoder(encodingName, ignoreEncodingErrors);
    } catch (IllegalCharsetNameException e) {
        log.error("unsupported.encoding", encodingName);
        return (CharBuffer) CharBuffer.allocate(1).flip();
    } catch (UnsupportedCharsetException e) {
        log.error("unsupported.encoding", encodingName);
        return (CharBuffer) CharBuffer.allocate(1).flip();
    }
    // slightly overestimate the buffer size to avoid reallocation.
    float factor = decoder.averageCharsPerByte() * 0.8f + decoder.maxCharsPerByte() * 0.2f;
    CharBuffer dest = CharBuffer.allocate(10 + (int) (inbuf.remaining() * factor));
    while (true) {
        CoderResult result = decoder.decode(inbuf, dest, true);
        dest.flip();
        if (result.isUnderflow()) {
            // make sure there is at least one extra character
            if (dest.limit() == dest.capacity()) {
                dest = CharBuffer.allocate(dest.capacity() + 1).put(dest);
                dest.flip();
            }
            return dest;
        } else if (result.isOverflow()) {
            // buffer too small; expand
            int newCapacity = 10 + dest.capacity() + (int) (inbuf.remaining() * decoder.maxCharsPerByte());
            dest = CharBuffer.allocate(newCapacity).put(dest);
        } else if (result.isMalformed() || result.isUnmappable()) {
            // report coding error (warn only pre 1.5)
            if (!getSource().allowEncodingErrors()) {
                log.error(new SimpleDiagnosticPosition(dest.limit()), "illegal.char.for.encoding", charset == null ? encodingName : charset.name());
            } else {
                log.warning(new SimpleDiagnosticPosition(dest.limit()), "illegal.char.for.encoding", charset == null ? encodingName : charset.name());
            }
            // skip past the coding error
            inbuf.position(inbuf.position() + result.length());
            // undo the flip() to prepare the output buffer
            // for more translation
            dest.position(dest.limit());
            dest.limit(dest.capacity());
            // backward compatible
            dest.put((char) 0xfffd);
        } else {
            throw new AssertionError(result);
        }
    }
// unreached
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) CharsetDecoder(java.nio.charset.CharsetDecoder) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) CharBuffer(java.nio.CharBuffer) SimpleDiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition) CoderResult(java.nio.charset.CoderResult)

Example 28 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project che by eclipse.

the class TextSearchVisitor method processFile.

/**
//	 * @return returns a map from IFile to IDocument for all open, dirty editors
//	 */
//	private Map evalNonFileBufferDocuments() {
//		Map result = new HashMap();
//		IWorkbench workbench = SearchPlugin.getDefault().getWorkbench();
//		IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
//		for (int i = 0; i < windows.length; i++) {
//			IWorkbenchPage[] pages = windows[i].getPages();
//			for (int x = 0; x < pages.length; x++) {
//				IEditorReference[] editorRefs = pages[x].getEditorReferences();
//				for (int z = 0; z < editorRefs.length; z++) {
//					IEditorPart ep = editorRefs[z].getEditor(false);
//					if (ep instanceof ITextEditor && ep.isDirty()) { // only dirty editors
//						evaluateTextEditor(result, ep);
//					}
//				}
//			}
//		}
//		return result;
//	}
//	private void evaluateTextEditor(Map result, IEditorPart ep) {
//		IEditorInput input= ep.getEditorInput();
//		if (input instanceof IFileEditorInput) {
//			IFile file= ((IFileEditorInput) input).getFile();
//			if (!result.containsKey(file)) { // take the first editor found
//				ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager();
//				ITextFileBuffer textFileBuffer= bufferManager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
//				if (textFileBuffer != null) {
//					// file buffer has precedence
//					result.put(file, textFileBuffer.getDocument());
//				} else {
//					// use document provider
//					IDocument document= ((ITextEditor) ep).getDocumentProvider().getDocument(input);
//					if (document != null) {
//						result.put(file, document);
//					}
//				}
//			}
//		}
//	}
public boolean processFile(IFile file, Map documentsInEditors) {
    try {
        if (!fCollector.acceptFile(file) || fMatcher == null) {
            return true;
        }
        IDocument document = getOpenDocument(file, documentsInEditors);
        if (document != null) {
            DocumentCharSequence documentCharSequence = new DocumentCharSequence(document);
            // assume all documents are non-binary
            locateMatches(file, documentCharSequence);
        } else {
            CharSequence seq = null;
            try {
                seq = fFileCharSequenceProvider.newCharSequence(file);
                if (hasBinaryContent(seq, file) && !fCollector.reportBinaryFile(file)) {
                    return true;
                }
                locateMatches(file, seq);
            } catch (FileCharSequenceProvider.FileCharSequenceException e) {
                e.throwWrappedException();
            } finally {
                if (seq != null) {
                    try {
                        fFileCharSequenceProvider.releaseCharSequence(seq);
                    } catch (IOException e) {
                        SearchPlugin.log(e);
                    }
                }
            }
        }
    } catch (UnsupportedCharsetException e) {
        String[] args = { getCharSetName(file), file.getFullPath().makeRelative().toString() };
        String message = Messages.format(SearchMessages.TextSearchVisitor_unsupportedcharset, args);
        fStatus.add(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, e));
    } catch (IllegalCharsetNameException e) {
        String[] args = { getCharSetName(file), file.getFullPath().makeRelative().toString() };
        String message = Messages.format(SearchMessages.TextSearchVisitor_illegalcharset, args);
        fStatus.add(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, e));
    } catch (IOException e) {
        String[] args = { getExceptionMessage(e), file.getFullPath().makeRelative().toString() };
        String message = Messages.format(SearchMessages.TextSearchVisitor_error, args);
        fStatus.add(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, e));
    } catch (CoreException e) {
        String[] args = { getExceptionMessage(e), file.getFullPath().makeRelative().toString() };
        String message = Messages.format(SearchMessages.TextSearchVisitor_error, args);
        fStatus.add(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, e));
    } catch (StackOverflowError e) {
        String message = SearchMessages.TextSearchVisitor_patterntoocomplex0;
        fStatus.add(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, e));
        return false;
    } finally {
        fNumberOfScannedFiles++;
    }
    if (fProgressMonitor.isCanceled())
        throw new OperationCanceledException(SearchMessages.TextSearchVisitor_canceled);
    return true;
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) FileCharSequenceException(org.eclipse.search.internal.core.text.FileCharSequenceProvider.FileCharSequenceException) CoreException(org.eclipse.core.runtime.CoreException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) IDocument(org.eclipse.jface.text.IDocument)

Example 29 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project gitblit by gitblit.

the class StringUtils method decodeString.

/**
	 * Decodes a string by trying several charsets until one does not throw a
	 * coding exception.  Last resort is to interpret as UTF-8 with illegal
	 * character substitution.
	 *
	 * @param content
	 * @param charsets optional
	 * @return a string
	 */
public static String decodeString(byte[] content, String... charsets) {
    Set<String> sets = new LinkedHashSet<String>();
    if (!ArrayUtils.isEmpty(charsets)) {
        sets.addAll(Arrays.asList(charsets));
    }
    String value = null;
    sets.addAll(Arrays.asList("UTF-8", "ISO-8859-1", Charset.defaultCharset().name()));
    for (String charset : sets) {
        try {
            Charset cs = Charset.forName(charset);
            CharsetDecoder decoder = cs.newDecoder();
            CharBuffer buffer = decoder.decode(ByteBuffer.wrap(content));
            value = buffer.toString();
            break;
        } catch (CharacterCodingException e) {
        // ignore and advance to the next charset
        } catch (IllegalCharsetNameException e) {
        // ignore illegal charset names
        } catch (UnsupportedCharsetException e) {
        // ignore unsupported charsets
        }
    }
    if (value != null && value.startsWith("")) {
        // strip UTF-8 BOM
        return value.substring(1);
    }
    return value;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) CharsetDecoder(java.nio.charset.CharsetDecoder) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 30 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project j2objc by google.

the class IOSCharsetDecoder method decodeImpl.

private String decodeImpl(ByteBuffer in) {
    Charset cs = charset();
    if (!(cs instanceof IOSCharset)) {
        throw new UnsupportedCharsetException(cs.name());
    }
    byte[] bytes;
    int i;
    if (inBuffer != null) {
        i = inBuffer.length;
        bytes = new byte[i + in.remaining()];
        System.arraycopy(inBuffer, 0, bytes, 0, inBuffer.length);
        inBuffer = null;
    } else {
        i = 0;
        bytes = new byte[in.remaining()];
    }
    in.get(bytes, i, bytes.length - i);
    String s = decode(bytes, nsEncoding);
    if (s.isEmpty()) {
        inBuffer = bytes;
    } else {
        inBuffer = null;
    }
    return s;
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset)

Aggregations

UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)46 Charset (java.nio.charset.Charset)18 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)13 IOException (java.io.IOException)10 File (java.io.File)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 FileOutputStream (java.io.FileOutputStream)4 PrintStream (java.io.PrintStream)4 HashMap (java.util.HashMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 FormBuilder (com.jgoodies.forms.builder.FormBuilder)2 FormLayout (com.jgoodies.forms.layout.FormLayout)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 ReadableByteChannel (java.nio.channels.ReadableByteChannel)2 WritableByteChannel (java.nio.channels.WritableByteChannel)2