use of java.nio.charset.IllegalCharsetNameException 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);
}
use of java.nio.charset.IllegalCharsetNameException 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
}
use of java.nio.charset.IllegalCharsetNameException in project hbase by apache.
the class RegexStringComparator method parseFrom.
/**
* @param pbBytes A pb serialized {@link RegexStringComparator} instance
* @return An instance of {@link RegexStringComparator} made from <code>bytes</code>
* @throws DeserializationException
* @see #toByteArray
*/
public static RegexStringComparator parseFrom(final byte[] pbBytes) throws DeserializationException {
ComparatorProtos.RegexStringComparator proto;
try {
proto = ComparatorProtos.RegexStringComparator.parseFrom(pbBytes);
} catch (InvalidProtocolBufferException e) {
throw new DeserializationException(e);
}
RegexStringComparator comparator;
if (proto.hasEngine()) {
EngineType engine = EngineType.valueOf(proto.getEngine());
comparator = new RegexStringComparator(proto.getPattern(), proto.getPatternFlags(), engine);
} else {
comparator = new RegexStringComparator(proto.getPattern(), proto.getPatternFlags());
}
String charset = proto.getCharset();
if (charset.length() > 0) {
try {
comparator.getEngine().setCharset(charset);
} catch (IllegalCharsetNameException e) {
LOG.error("invalid charset", e);
}
}
return comparator;
}
use of java.nio.charset.IllegalCharsetNameException 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;
}
use of java.nio.charset.IllegalCharsetNameException 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;
}
Aggregations