use of java.nio.charset.UnmappableCharacterException in project che by eclipse.
the class FileStoreTextFileBuffer method commitFileBufferContent.
/*
* @see org.eclipse.core.internal.filebuffers.FileBuffer#commitFileBufferContent(org.eclipse.core.runtime.IProgressMonitor, boolean)
*/
protected void commitFileBufferContent(IProgressMonitor monitor, boolean overwrite) throws CoreException {
// if (!isSynchronized() && !overwrite)
// throw new CoreException(new Status(IStatus.WARNING, FileBuffersPlugin.PLUGIN_ID, IResourceStatus.OUT_OF_SYNC_LOCAL, FileBuffersMessages.FileBuffer_error_outOfSync, null));
String encoding = computeEncoding();
Charset charset;
try {
charset = Charset.forName(encoding);
} catch (UnsupportedCharsetException ex) {
String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_unsupported_encoding_message_arg, encoding);
IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, message, ex);
throw new CoreException(s);
} catch (IllegalCharsetNameException ex) {
String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_illegal_encoding_message_arg, encoding);
IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, message, ex);
throw new CoreException(s);
}
CharsetEncoder encoder = charset.newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
byte[] bytes;
int bytesLength;
try {
ByteBuffer byteBuffer = encoder.encode(CharBuffer.wrap(fDocument.get()));
bytesLength = byteBuffer.limit();
if (byteBuffer.hasArray())
bytes = byteBuffer.array();
else {
bytes = new byte[bytesLength];
byteBuffer.get(bytes);
}
} catch (CharacterCodingException ex) {
Assert.isTrue(ex instanceof UnmappableCharacterException);
String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_charset_mapping_failed_message_arg, encoding);
IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CHARSET_MAPPING_FAILED, message, null);
throw new CoreException(s);
}
IFileInfo fileInfo = fFileStore.fetchInfo();
if (fileInfo != null && fileInfo.exists()) {
if (!overwrite)
checkSynchronizationState();
InputStream stream = new ByteArrayInputStream(bytes, 0, bytesLength);
/*
* 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 (fHasBOM && CHARSET_UTF_8.equals(encoding))
stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_8), stream);
// here the file synchronizer should actually be removed and afterwards added again. However,
// we are already inside an operation, so the delta is sent AFTER we have added the listener
setFileContents(stream, monitor);
// set synchronization stamp to know whether the file synchronizer must become active
fSynchronizationStamp = fFileStore.fetchInfo().getLastModified();
// if (fAnnotationModel instanceof IPersistableAnnotationModel) {
// IPersistableAnnotationModel persistableModel= (IPersistableAnnotationModel) fAnnotationModel;
// persistableModel.commit(fDocument);
// }
} else {
fFileStore.getParent().mkdir(EFS.NONE, null);
OutputStream out = fFileStore.openOutputStream(EFS.NONE, null);
try {
/*
* 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 (fHasBOM && CHARSET_UTF_8.equals(encoding))
out.write(IContentDescription.BOM_UTF_8);
out.write(bytes, 0, bytesLength);
out.flush();
out.close();
} catch (IOException x) {
IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, x.getLocalizedMessage(), x);
throw new CoreException(s);
} finally {
try {
out.close();
} catch (IOException x) {
}
}
// set synchronization stamp to know whether the file synchronizer must become active
fSynchronizationStamp = fFileStore.fetchInfo().getLastModified();
}
}
use of java.nio.charset.UnmappableCharacterException in project robovm by robovm.
the class CharsetDecoderTest method testDecodeByteBufferException.
public void testDecodeByteBufferException() throws CharacterCodingException, UnsupportedEncodingException {
CharBuffer out;
ByteBuffer in;
String replaceStr = decoder.replacement() + getString();
// MalformedException:
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
in = getMalformedByteBuffer();
if (in != null) {
try {
CharBuffer buffer = decoder.decode(in);
assertTrue(buffer.remaining() > 0);
fail("should throw MalformedInputException");
} catch (MalformedInputException e) {
}
decoder.reset();
in.rewind();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
out = decoder.decode(in);
assertCharBufferValue(getString(), out);
decoder.reset();
in.rewind();
decoder.onMalformedInput(CodingErrorAction.REPLACE);
out = decoder.decode(in);
assertCharBufferValue(replaceStr, out);
}
// Unmapped Exception:
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
in = getUnmappedByteBuffer();
if (in != null) {
try {
decoder.decode(in);
fail("should throw UnmappableCharacterException");
} catch (UnmappableCharacterException e) {
}
decoder.reset();
in.rewind();
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
out = decoder.decode(in);
assertCharBufferValue(getString(), out);
decoder.reset();
in.rewind();
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
out = decoder.decode(in);
assertCharBufferValue(replaceStr, out);
}
// RuntimeException
try {
decoder.decode(getExceptionByteArray());
fail("should throw runtime exception");
} catch (RuntimeException e) {
}
}
use of java.nio.charset.UnmappableCharacterException in project robovm by robovm.
the class ASCIICharsetEncoderTest method testMultiStepEncode.
public void testMultiStepEncode() throws CharacterCodingException {
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
try {
encoder.encode(CharBuffer.wrap("𐀀"));
fail("should unmappable");
} catch (UnmappableCharacterException e) {
}
encoder.reset();
ByteBuffer out = ByteBuffer.allocate(10);
assertTrue(encoder.encode(CharBuffer.wrap("�"), out, true).isMalformed());
encoder.flush(out);
encoder.reset();
out = ByteBuffer.allocate(10);
assertSame(CoderResult.UNDERFLOW, encoder.encode(CharBuffer.wrap("�"), out, false));
assertTrue(encoder.encode(CharBuffer.wrap("�"), out, true).isMalformed());
}
use of java.nio.charset.UnmappableCharacterException in project robovm by robovm.
the class ASCIICharsetEncoderTest method testEncodeMapping.
public void testEncodeMapping() throws CharacterCodingException {
encoder.reset();
for (int i = 0; i <= MAXCODEPOINT; i++) {
char[] chars = Character.toChars(i);
CharBuffer cb = CharBuffer.wrap(chars);
ByteBuffer bb = encoder.encode(cb);
assertEquals(i, bb.get(0));
}
CharBuffer cb = CharBuffer.wrap("");
try {
encoder.encode(cb);
} catch (UnmappableCharacterException e) {
//expected
}
cb = CharBuffer.wrap("�");
try {
encoder.encode(cb);
} catch (MalformedInputException e) {
//expected
}
ByteBuffer bb = ByteBuffer.allocate(0x10);
cb = CharBuffer.wrap("A");
encoder.reset();
encoder.encode(cb, bb, false);
try {
encoder.encode(cb);
} catch (IllegalStateException e) {
//expected
}
}
use of java.nio.charset.UnmappableCharacterException in project robovm by robovm.
the class UnmappableCharacterExceptionTest method testConstructor.
public void testConstructor() {
UnmappableCharacterException ex = new UnmappableCharacterException(3);
assertTrue(ex instanceof CharacterCodingException);
assertNull(ex.getCause());
assertEquals(ex.getInputLength(), 3);
assertTrue(ex.getMessage().indexOf("3") != -1);
ex = new UnmappableCharacterException(-3);
assertNull(ex.getCause());
assertEquals(ex.getInputLength(), -3);
assertTrue(ex.getMessage().indexOf("-3") != -1);
ex = new UnmappableCharacterException(0);
assertNull(ex.getCause());
assertEquals(ex.getInputLength(), 0);
assertTrue(ex.getMessage().indexOf("0") != -1);
}
Aggregations