Buggy code:
public class CommitWorkingCopyOperation extends JavaModelOperation { public CommitWorkingCopyOperation ( ICompilationUnit element , boolean force ) { } protected void executeOperation ( ) throws JavaModelException { try { beginTask ( Util . bind ( "workingCopy.commit" ) , 2 ) ; CompilationUnit workingCopy = getCompilationUnit ( ) ; IFile resource = ( ( IFile ) ( workingCopy . getResource ( ) ) ) ; ICompilationUnit primary = workingCopy . getPrimary ( ) ; boolean isPrimary = workingCopy . isPrimary ( ) ; JavaElementDeltaBuilder deltaBuilder = null ; PackageFragmentRoot root = ( ( PackageFragmentRoot ) ( workingCopy . getAncestor ( IJavaElement . PACKAGE_FRAGMENT_ROOT ) ) ) ; boolean isIncluded = ! ( Util . isExcluded ( workingCopy ) ) ; if ( isPrimary || ( ( ( ( root . isOnClasspath ( ) ) && isIncluded ) && ( resource . isAccessible ( ) ) ) && ( Util . isValidCompilationUnitName ( workingCopy . getElementName ( ) ) ) ) ) { if ( ( ! isPrimary ) && ( ! ( primary . isOpen ( ) ) ) ) { primary . open ( null ) ; } if ( isIncluded && ( ( ! isPrimary ) || ( ! ( workingCopy . isConsistent ( ) ) ) ) ) { deltaBuilder = new JavaElementDeltaBuilder ( primary ) ; } IBuffer primaryBuffer = primary . getBuffer ( ) ; if ( ! isPrimary ) { if ( primaryBuffer == null ) return ; char [ ] primaryContents = primaryBuffer . getCharacters ( ) ; boolean hasSaved = false ; try { IBuffer workingCopyBuffer = workingCopy . getBuffer ( ) ; if ( workingCopyBuffer == null ) return ; primaryBuffer . setContents ( workingCopyBuffer . getCharacters ( ) ) ; primaryBuffer . save ( this . progressMonitor , this . force ) ; primary . makeConsistent ( this ) ; hasSaved = true ; } finally { if ( ! hasSaved ) { primaryBuffer . setContents ( primaryContents ) ; } } } else { primaryBuffer . save ( this . progressMonitor , this . force ) ; primary . makeConsistent ( this ) ; } } else { String encoding = null ; try { <START_BUG> resource . getCharset ( ) ; <END_BUG> } catch ( CoreException ce ) { } String contents = workingCopy . getSource ( ) ; if ( contents == null ) return ; try { byte [ ] bytes = ( encoding == null ) ? contents . getBytes ( ) : contents . getBytes ( encoding ) ; ByteArrayInputStream stream = new ByteArrayInputStream ( bytes ) ; if ( resource . exists ( ) ) { resource . setContents ( stream , ( this . force ? ( IResource . FORCE ) | ( IResource . KEEP_HISTORY ) : IResource . KEEP_HISTORY ) , null ) ; } else { resource . create ( stream , this . force , this . progressMonitor ) ; } } catch ( CoreException e ) { throw new JavaModelException ( e ) ; } catch ( UnsupportedEncodingException e ) { throw new JavaModelException ( e , IJavaModelStatusConstants . IO_EXCEPTION ) ; } } JavaModelOperation . setAttribute ( JavaModelOperation . HAS_MODIFIED_RESOURCE_ATTR , JavaModelOperation . TRUE ) ; workingCopy . updateTimeStamp ( ( ( CompilationUnit ) ( primary ) ) ) ; workingCopy . makeConsistent ( this ) ; worked ( 1 ) ; if ( deltaBuilder != null ) { deltaBuilder . buildDeltas ( ) ; if ( ( deltaBuilder . delta ) != null ) { addDelta ( deltaBuilder . delta ) ; } } worked ( 1 ) ; } finally { done ( ) ; } } protected CompilationUnit getCompilationUnit ( ) { } protected ISchedulingRule getSchedulingRule ( ) { } public IJavaModelStatus verify ( ) { } }

Fix:
encoding = resource . getCharset ( ) ;