Search in sources :

Example 1 with UnmappableCharacterException

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();
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) IFileInfo(org.eclipse.core.filesystem.IFileInfo) CoreException(org.eclipse.core.runtime.CoreException) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException)

Example 2 with UnmappableCharacterException

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) {
    }
}
Also used : UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) CharBuffer(java.nio.CharBuffer) MalformedInputException(java.nio.charset.MalformedInputException) ByteBuffer(java.nio.ByteBuffer)

Example 3 with UnmappableCharacterException

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());
}
Also used : UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) ByteBuffer(java.nio.ByteBuffer)

Example 4 with UnmappableCharacterException

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
    }
}
Also used : UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) CharBuffer(java.nio.CharBuffer) MalformedInputException(java.nio.charset.MalformedInputException) ByteBuffer(java.nio.ByteBuffer)

Example 5 with UnmappableCharacterException

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);
}
Also used : UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) CharacterCodingException(java.nio.charset.CharacterCodingException)

Aggregations

UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)8 ByteBuffer (java.nio.ByteBuffer)5 CharBuffer (java.nio.CharBuffer)3 MalformedInputException (java.nio.charset.MalformedInputException)3 CharacterCodingException (java.nio.charset.CharacterCodingException)2 ArrayList (java.util.ArrayList)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 Charset (java.nio.charset.Charset)1 CharsetEncoder (java.nio.charset.CharsetEncoder)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 IFileInfo (org.eclipse.core.filesystem.IFileInfo)1