use of org.eclipse.core.filesystem.IFileInfo in project eclipse.platform.text by eclipse.
the class FileBuffersForWorkspaceFiles method modifyUnderlyingFile.
@Override
protected boolean modifyUnderlyingFile() throws Exception {
IFileStore fileStore = FileBuffers.getFileStoreAtLocation(getPath());
assertTrue(fileStore.fetchInfo().exists());
OutputStream out = fileStore.openOutputStream(EFS.NONE, null);
try {
out.write("Changed content of workspace file".getBytes());
out.flush();
} catch (IOException x) {
fail();
} finally {
out.close();
}
IFileInfo fileInfo = fileStore.fetchInfo();
fileInfo.setLastModified(1000);
fileStore.putInfo(fileInfo, EFS.SET_LAST_MODIFIED, null);
IFile iFile = FileBuffers.getWorkspaceFileAtLocation(getPath());
assertTrue(iFile.exists() && iFile.getFullPath().equals(getPath()));
iFile.refreshLocal(IResource.DEPTH_INFINITE, null);
return true;
}
use of org.eclipse.core.filesystem.IFileInfo in project eclipse.platform.text by eclipse.
the class FileStoreFileBufferFunctions method test6.
/*
* Test revert.
*/
@Test
public void test6() throws Exception {
fManager.connectFileStore(fFileStore, null);
try {
ITextFileBuffer fileBuffer = fManager.getFileStoreTextFileBuffer(fFileStore);
// set dirty bit
IDocument document = fileBuffer.getDocument();
String originalContent = document.get();
document.replace(document.getLength(), 0, "appendix");
// invalidate synchronization state
IFileInfo fileInfo = fFileStore.fetchInfo();
fileInfo.setLastModified(1000);
if (fileInfo.exists())
fFileStore.putInfo(fileInfo, EFS.SET_LAST_MODIFIED, null);
// revert
fileBuffer.revert(null);
// check assertions
assertEquals(originalContent, document.get());
assertFalse(fileBuffer.isDirty());
assertTrue(fileBuffer.isSynchronized());
} finally {
fManager.disconnectFileStore(fFileStore, null);
}
}
use of org.eclipse.core.filesystem.IFileInfo in project eclipse.platform.text by eclipse.
the class FileStoreTextFileBuffer method revert.
@Override
public void revert(IProgressMonitor monitor) throws CoreException {
if (isDisconnected())
return;
IDocument original = null;
fStatus = null;
try {
original = fManager.createEmptyDocument(getLocationOrName(), LocationKind.LOCATION);
cacheEncodingState();
setDocumentContent(original, fFileStore, fEncoding, fHasBOM, monitor);
} catch (CoreException x) {
fStatus = x.getStatus();
}
if (original == null)
return;
String originalContents = original.get();
boolean replaceContents = !originalContents.equals(fDocument.get());
if (!replaceContents && !fCanBeSaved)
return;
fManager.fireStateChanging(this);
try {
if (replaceContents) {
fManager.fireBufferContentAboutToBeReplaced(this);
fDocument.set(original.get());
}
boolean fireDirtyStateChanged = fCanBeSaved;
if (fCanBeSaved) {
fCanBeSaved = false;
addFileBufferContentListeners();
}
if (replaceContents)
fManager.fireBufferContentReplaced(this);
IFileInfo info = fFileStore.fetchInfo();
if (info.exists())
fSynchronizationStamp = fFileStore.fetchInfo().getLastModified();
if (fAnnotationModel instanceof IPersistableAnnotationModel) {
IPersistableAnnotationModel persistableModel = (IPersistableAnnotationModel) fAnnotationModel;
try {
persistableModel.revert(fDocument);
} catch (CoreException x) {
fStatus = x.getStatus();
}
}
if (fireDirtyStateChanged)
fManager.fireDirtyStateChanged(this, fCanBeSaved);
} catch (RuntimeException x) {
fManager.fireStateChangeFailed(this);
throw x;
}
}
use of org.eclipse.core.filesystem.IFileInfo in project eclipse.platform.text by eclipse.
the class FileStoreTextFileBuffer method commitFileBufferContent.
@Override
protected void commitFileBufferContent(IProgressMonitor monitor, boolean overwrite) throws CoreException {
if (!isSynchronized() && !overwrite) {
String message = NLSUtility.format(FileBuffersMessages.FileBuffer_error_outOfSync, getFileStore().toURI());
throw new CoreException(new Status(IStatus.WARNING, FileBuffersPlugin.PLUGIN_ID, IResourceStatus.OUT_OF_SYNC_LOCAL, message, 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);
try (OutputStream out = fFileStore.openOutputStream(EFS.NONE, null)) {
/*
* 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);
}
// set synchronization stamp to know whether the file synchronizer must become active
fSynchronizationStamp = fFileStore.fetchInfo().getLastModified();
}
}
use of org.eclipse.core.filesystem.IFileInfo in project eclipse.platform.text by eclipse.
the class FileStoreFileBuffer method create.
public void create(IFileStore fileStore, IProgressMonitor monitor) throws CoreException {
IFileInfo info = fileStore.fetchInfo();
fFileStore = fileStore;
if (fLocation == null)
fLocation = URIUtil.toPath(fileStore.toURI());
initializeFileBufferContent(monitor);
if (info.exists())
fSynchronizationStamp = info.getLastModified();
addFileBufferContentListeners();
}
Aggregations