package org.eclipse.core.internal.resources;
import java.io.*;
import org.eclipse.core.internal.localstore.IHistoryStore;
import org.eclipse.core.internal.utils.Messages;
import org.eclipse.core.internal.utils.UniversalUniqueIdentifier;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.osgi.util.NLS;
public class FileState extends PlatformObject implements IFileState {
private static final IWorkspace workspace = ResourcesPlugin.getWorkspace();
protected long lastModified;
protected UniversalUniqueIdentifier uuid;
protected IHistoryStore store;
protected IPath fullPath;
public FileState(IHistoryStore store, IPath fullPath, long lastModified, UniversalUniqueIdentifier uuid) {
this.store = store;
this.lastModified = lastModified;
this.uuid = uuid;
this.fullPath = fullPath;
}
@Override
public boolean exists() {
return store.exists(this);
}
@Override
public String getCharset() throws CoreException {
IResource file = workspace.getRoot().findMember(fullPath);
if (file != null && file.getType() == IResource.FILE)
return ((IFile) file).getCharset();
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
try (InputStream contents = new BufferedInputStream(getContents())) {
IContentDescription description = contentTypeManager.getDescriptionFor(contents, getName(), new QualifiedName[] {IContentDescription.CHARSET});
return description == null ? null : description.getCharset();
} catch (IOException e) {
String message = NLS.bind(Messages.history_errorContentDescription, getFullPath());
throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, getFullPath(), message, e);
}
}
@Override
public InputStream getContents() throws CoreException {
return store.getContents(this);
}
@Override
public IPath getFullPath() {
return fullPath;
}
@Override
public long getModificationTime() {
return lastModified;
}
@Override
public String getName() {
return fullPath.lastSegment();
}
public UniversalUniqueIdentifier getUUID() {
return uuid;
}
@Override
public boolean isReadOnly() {
return true;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append("FileState(uuid: ");
s.append(uuid.toString());
s.append(", lastModified: ");
s.append(lastModified);
s.append(", path: ");
s.append(fullPath);
s.append(')');
return s.toString();
}
}