Search in sources :

Example 6 with ByteSequence

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) ByteSequence(com.intellij.openapi.util.io.ByteSequence) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with ByteSequence

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);
}
Also used : ByteSequence(com.intellij.openapi.util.io.ByteSequence) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with ByteSequence

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)));
}
Also used : ByteSequence(com.intellij.openapi.util.io.ByteSequence)

Example 9 with ByteSequence

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;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleFileType(com.intellij.ide.highlighter.ModuleFileType) NotNull(org.jetbrains.annotations.NotNull) ProjectFileType(com.intellij.ide.highlighter.ProjectFileType) ArchiveFileType(com.intellij.ide.highlighter.ArchiveFileType) ModuleFileType(com.intellij.ide.highlighter.ModuleFileType) ProjectFileType(com.intellij.ide.highlighter.ProjectFileType) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiPlainTextFile(com.intellij.psi.PsiPlainTextFile) PsiFile(com.intellij.psi.PsiFile) PsiBinaryFile(com.intellij.psi.PsiBinaryFile) File(java.io.File) ByteSequence(com.intellij.openapi.util.io.ByteSequence) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with ByteSequence

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;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NotNull(org.jetbrains.annotations.NotNull) ProjectFileType(com.intellij.ide.highlighter.ProjectFileType) ArchiveFileType(com.intellij.ide.highlighter.ArchiveFileType) ModuleFileType(com.intellij.ide.highlighter.ModuleFileType) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiPlainTextFile(com.intellij.psi.PsiPlainTextFile) PsiFile(com.intellij.psi.PsiFile) PsiBinaryFile(com.intellij.psi.PsiBinaryFile) File(java.io.File) ByteSequence(com.intellij.openapi.util.io.ByteSequence) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ByteSequence (com.intellij.openapi.util.io.ByteSequence)11 NotNull (org.jetbrains.annotations.NotNull)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 File (java.io.File)3 ArchiveFileType (com.intellij.ide.highlighter.ArchiveFileType)2 ModuleFileType (com.intellij.ide.highlighter.ModuleFileType)2 ProjectFileType (com.intellij.ide.highlighter.ProjectFileType)2 PsiBinaryFile (com.intellij.psi.PsiBinaryFile)2 PsiFile (com.intellij.psi.PsiFile)2 PsiPlainTextFile (com.intellij.psi.PsiPlainTextFile)2 Nullable (org.jetbrains.annotations.Nullable)2 BufferExposingByteArrayOutputStream (com.intellij.openapi.util.io.BufferExposingByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 JarOutputStream (java.util.jar.JarOutputStream)1 Manifest (java.util.jar.Manifest)1