use of com.intellij.openapi.util.io.ByteSequence in project intellij-community by JetBrains.
the class ExternalSystemTestCase method createProjectJarSubFile.
@NotNull
protected VirtualFile createProjectJarSubFile(String relativePath, Pair<ByteSequence, String>... contentEntries) throws IOException {
assertTrue("Use 'jar' extension for JAR files: '" + relativePath + "'", FileUtilRt.extensionEquals(relativePath, "jar"));
File f = new File(getProjectPath(), relativePath);
FileUtil.ensureExists(f.getParentFile());
FileUtil.ensureCanCreateFile(f);
final boolean created = f.createNewFile();
if (!created) {
throw new AssertionError("Unable to create the project sub file: " + f.getAbsolutePath());
}
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
JarOutputStream target = new JarOutputStream(new FileOutputStream(f), manifest);
for (Pair<ByteSequence, String> contentEntry : contentEntries) {
addJarEntry(contentEntry.first.getBytes(), contentEntry.second, target);
}
target.close();
final VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(f);
assertNotNull(virtualFile);
final VirtualFile jarFile = JarFileSystem.getInstance().getJarRootForLocalFile(virtualFile);
assertNotNull(jarFile);
return jarFile;
}
use of com.intellij.openapi.util.io.ByteSequence in project intellij-community by JetBrains.
the class SnapshotInputMappings method readPersistentDataOrMap.
@NotNull
Snapshot<Key, Value> readPersistentDataOrMap(@NotNull Input content) {
Map<Key, Value> data = null;
boolean havePersistentData = false;
int hashId;
boolean skippedReadingPersistentDataButMayHaveIt = false;
try {
FileContent fileContent = (FileContent) content;
hashId = getHashOfContent(fileContent);
if (doReadSavedPersistentData) {
if (!myContents.isBusyReading() || DebugAssertions.EXTRA_SANITY_CHECKS) {
// avoid blocking read, we can calculate index value
ByteSequence bytes = readContents(hashId);
if (bytes != null) {
data = deserializeSavedPersistentData(bytes);
havePersistentData = true;
if (DebugAssertions.EXTRA_SANITY_CHECKS) {
Map<Key, Value> contentData = myIndexer.map(content);
boolean sameValueForSavedIndexedResultAndCurrentOne = contentData.equals(data);
if (!sameValueForSavedIndexedResultAndCurrentOne) {
DebugAssertions.error("Unexpected difference in indexing of %s by index %s, file type %s, charset %s\ndiff %s\nprevious indexed info %s", fileContent.getFile(), myIndexId, fileContent.getFileType().getName(), ((FileContentImpl) fileContent).getCharset(), buildDiff(data, contentData), myIndexingTrace.get(hashId));
}
}
}
} else {
skippedReadingPersistentDataButMayHaveIt = true;
}
} else {
havePersistentData = myContents.containsMapping(hashId);
}
} catch (IOException ex) {
// todo:
throw new RuntimeException(ex);
}
if (data == null) {
data = myIndexer.map(content);
if (DebugAssertions.DEBUG) {
MapReduceIndex.checkValuesHaveProperEqualsAndHashCode(data, myIndexId, myValueExternalizer);
}
}
if (!havePersistentData) {
boolean saved = savePersistentData(data, hashId, skippedReadingPersistentDataButMayHaveIt);
if (DebugAssertions.EXTRA_SANITY_CHECKS) {
if (saved) {
FileContent fileContent = (FileContent) content;
try {
myIndexingTrace.put(hashId, ((FileContentImpl) fileContent).getCharset() + "," + fileContent.getFileType().getName() + "," + fileContent.getFile().getPath() + "," + ExceptionUtil.getThrowableText(new Throwable()));
} catch (IOException ex) {
LOG.error(ex);
}
}
}
}
return new Snapshot<>(data, hashId);
}
use of com.intellij.openapi.util.io.ByteSequence in project intellij-community by JetBrains.
the class StorageTest method testSmoke.
public void testSmoke() throws Exception {
final int record = myStorage.createNewRecord();
myStorage.writeBytes(record, new ByteSequence("Hello".getBytes()), false);
assertEquals("Hello", new String(myStorage.readBytes(record)));
}
use of com.intellij.openapi.util.io.ByteSequence in project intellij-community by JetBrains.
the class FileTypesTest method testReDetectOnContentsChange.
public void testReDetectOnContentsChange() throws IOException {
final FileTypeRegistry fileTypeManager = FileTypeRegistry.getInstance();
assertTrue(fileTypeManager.getClass().getName(), fileTypeManager instanceof FileTypeManagerImpl);
FileType fileType = fileTypeManager.getFileTypeByFileName("x" + ModuleFileType.DOT_DEFAULT_EXTENSION);
assertTrue(fileType.toString(), fileType instanceof ModuleFileType);
fileType = fileTypeManager.getFileTypeByFileName("x" + ProjectFileType.DOT_DEFAULT_EXTENSION);
assertTrue(fileType.toString(), fileType instanceof ProjectFileType);
FileType module = fileTypeManager.findFileTypeByName("IDEA_MODULE");
assertNotNull(module);
assertFalse(module.equals(PlainTextFileType.INSTANCE));
FileType project = fileTypeManager.findFileTypeByName("IDEA_PROJECT");
assertNotNull(project);
assertFalse(project.equals(PlainTextFileType.INSTANCE));
final Set<VirtualFile> detectorCalled = ContainerUtil.newConcurrentSet();
FileTypeRegistry.FileTypeDetector detector = new FileTypeRegistry.FileTypeDetector() {
@Nullable
@Override
public FileType detect(@NotNull VirtualFile file, @NotNull ByteSequence firstBytes, @Nullable CharSequence firstCharsIfText) {
detectorCalled.add(file);
String text = firstCharsIfText.toString();
FileType result = text.startsWith("TYPE:") ? fileTypeManager.findFileTypeByName(StringUtil.trimStart(text, "TYPE:")) : null;
log("T: my detector run for " + file.getName() + "; result: " + (result == null ? null : result.getName()));
return result;
}
@Override
public int getVersion() {
return 0;
}
};
Extensions.getRootArea().getExtensionPoint(FileTypeRegistry.FileTypeDetector.EP_NAME).registerExtension(detector);
myFileTypeManager.toLog = true;
try {
log("T: ------ akjdhfksdjgf");
File f = createTempFile("xx.asfdasdfas", "akjdhfksdjgf");
VirtualFile vFile = getVirtualFile(f);
ensureRedetected(vFile, detectorCalled);
assertTrue(vFile.getFileType().toString(), vFile.getFileType() instanceof PlainTextFileType);
log("T: ------ TYPE:IDEA_MODULE");
setFileText(vFile, "TYPE:IDEA_MODULE");
ensureRedetected(vFile, detectorCalled);
assertTrue(vFile.getFileType().toString(), vFile.getFileType() instanceof ModuleFileType);
log("T: ------ TYPE:IDEA_PROJECT");
setFileText(vFile, "TYPE:IDEA_PROJECT");
ensureRedetected(vFile, detectorCalled);
assertTrue(vFile.getFileType().toString(), vFile.getFileType() instanceof ProjectFileType);
log("T: ------");
} finally {
Extensions.getRootArea().getExtensionPoint(FileTypeRegistry.FileTypeDetector.EP_NAME).unregisterExtension(detector);
myFileTypeManager.toLog = false;
}
}
use of com.intellij.openapi.util.io.ByteSequence in project intellij-community by JetBrains.
the class FileTypesTest method testIfDetectorRanThenIdeaReopenedTheDetectorShouldBeReRun.
public void testIfDetectorRanThenIdeaReopenedTheDetectorShouldBeReRun() throws IOException {
final UserBinaryFileType stuffType = new UserBinaryFileType();
stuffType.setName("stuffType");
final Set<VirtualFile> detectorCalled = ContainerUtil.newConcurrentSet();
FileTypeRegistry.FileTypeDetector detector = new FileTypeRegistry.FileTypeDetector() {
@Nullable
@Override
public FileType detect(@NotNull VirtualFile file, @NotNull ByteSequence firstBytes, @Nullable CharSequence firstCharsIfText) {
detectorCalled.add(file);
FileType result = FileUtil.isHashBangLine(firstCharsIfText, "stuff") ? stuffType : null;
log("T: my detector for file " + file.getName() + " run. result=" + (result == null ? null : result.getName()));
return result;
}
@Override
public int getVersion() {
return 0;
}
};
Extensions.getRootArea().getExtensionPoint(FileTypeRegistry.FileTypeDetector.EP_NAME).registerExtension(detector);
myFileTypeManager.toLog = true;
try {
log("T: ------ akjdhfksdjgf");
File f = createTempFile("xx.asfdasdfas", "akjdhfksdjgf");
VirtualFile file = getVirtualFile(f);
ensureRedetected(file, detectorCalled);
assertTrue(file.getFileType().toString(), file.getFileType() instanceof PlainTextFileType);
log("T: ------ my");
setFileText(file, "#!stuff\nxx");
ensureRedetected(file, detectorCalled);
assertEquals(stuffType, file.getFileType());
log("T: ------ reload");
myFileTypeManager.drainReDetectQueue();
myFileTypeManager.clearCaches();
file.putUserData(FileTypeManagerImpl.DETECTED_FROM_CONTENT_FILE_TYPE_KEY, null);
ensureRedetected(file, detectorCalled);
assertSame(file.getFileType().toString(), file.getFileType(), stuffType);
log("T: ------");
} finally {
Extensions.getRootArea().getExtensionPoint(FileTypeRegistry.FileTypeDetector.EP_NAME).unregisterExtension(detector);
myFileTypeManager.toLog = false;
}
}
Aggregations