use of java.nio.charset.CharsetEncoder 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);
}
}
use of java.nio.charset.CharsetEncoder in project jdk8u_jdk by JetBrains.
the class ZipCoder method getBytes.
byte[] getBytes(String s) {
CharsetEncoder ce = encoder().reset();
char[] ca = s.toCharArray();
int len = (int) (ca.length * ce.maxBytesPerChar());
byte[] ba = new byte[len];
if (len == 0)
return ba;
// CodingErrorAction.REPLACE mode.
if (isUTF8 && ce instanceof ArrayEncoder) {
int blen = ((ArrayEncoder) ce).encode(ca, 0, ca.length, ba);
if (// malformed
blen == -1)
throw new IllegalArgumentException("MALFORMED");
return Arrays.copyOf(ba, blen);
}
ByteBuffer bb = ByteBuffer.wrap(ba);
CharBuffer cb = CharBuffer.wrap(ca);
CoderResult cr = ce.encode(cb, bb, true);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
cr = ce.flush(bb);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
if (// defensive copy?
bb.position() == ba.length)
return ba;
else
return Arrays.copyOf(ba, bb.position());
}
use of java.nio.charset.CharsetEncoder 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);
}
}
use of java.nio.charset.CharsetEncoder in project eclipse.platform.text by eclipse.
the class AbstractDecoratedTextEditor method openSaveErrorDialog.
/**
* Presents an error dialog to the user when a problem happens during save.
* <p>
* Overrides the default behavior by showing a more advanced error dialog in case of encoding
* problems.
* </p>
*
* @param title the dialog title
* @param message the message to display
* @param exception the exception to handle
* @since 3.6
*/
@Override
protected void openSaveErrorDialog(String title, String message, CoreException exception) {
IStatus status = exception.getStatus();
final IDocumentProvider documentProvider = getDocumentProvider();
if (!(status.getCode() == IFileBufferStatusCodes.CHARSET_MAPPING_FAILED && documentProvider instanceof IStorageDocumentProvider)) {
super.openSaveErrorDialog(title, message, exception);
return;
}
final int saveAsUTF8ButtonId = IDialogConstants.OK_ID + IDialogConstants.CANCEL_ID + 1;
final int selectUnmappableCharButtonId = saveAsUTF8ButtonId + 1;
final Charset charset = getCharset();
ErrorDialog errorDialog = new ErrorDialog(getSite().getShell(), title, message, status, IStatus.ERROR) {
@Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
createButton(parent, saveAsUTF8ButtonId, TextEditorMessages.AbstractDecoratedTextEditor_save_error_Dialog_button_saveAsUTF8, false);
if (charset != null)
createButton(parent, selectUnmappableCharButtonId, TextEditorMessages.AbstractDecoratedTextEditor_save_error_Dialog_button_selectUnmappable, false);
}
@Override
protected void buttonPressed(int id) {
if (id == saveAsUTF8ButtonId || id == selectUnmappableCharButtonId) {
setReturnCode(id);
close();
} else
super.buttonPressed(id);
}
@Override
protected boolean shouldShowDetailsButton() {
return false;
}
};
int returnCode = errorDialog.open();
if (returnCode == saveAsUTF8ButtonId) {
// $NON-NLS-1$
((IStorageDocumentProvider) documentProvider).setEncoding(getEditorInput(), "UTF-8");
IProgressMonitor monitor = getProgressMonitor();
try {
doSave(monitor);
} finally {
monitor.done();
}
} else if (returnCode == selectUnmappableCharButtonId) {
CharsetEncoder encoder = charset.newEncoder();
IDocument document = getDocumentProvider().getDocument(getEditorInput());
int documentLength = document.getLength();
int offset = 0;
BreakIterator charBreakIterator = BreakIterator.getCharacterInstance();
charBreakIterator.setText(document.get());
while (offset < documentLength) {
try {
int next = charBreakIterator.next();
String ch = document.get(offset, next - offset);
if (!encoder.canEncode(ch)) {
selectAndReveal(offset, next - offset);
return;
}
offset = next;
} catch (BadLocationException ex) {
EditorsPlugin.log(ex);
// Skip this character. Showing yet another dialog here is overkill
}
}
}
}
use of java.nio.charset.CharsetEncoder 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;
}
Aggregations