예제 #1
0
 @Override
 @NotNull
 public byte[] contentsToByteArray(int contentId) throws IOException {
   final DataInputStream stream = readContentById(contentId);
   assert stream != null : contentId;
   return FileUtil.loadBytes(stream);
 }
 @Nullable
 private DataInputStream createFSDataStream(File dataStorageRoot) {
   if (myInitialFSDelta == null) {
     // this will force FS rescan
     return null;
   }
   try {
     final File file = new File(dataStorageRoot, FS_STATE_FILE);
     final InputStream fs = new FileInputStream(file);
     byte[] bytes;
     try {
       bytes = FileUtil.loadBytes(fs, (int) file.length());
     } finally {
       fs.close();
     }
     final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
     final int version = in.readInt();
     if (version != FSState.VERSION) {
       return null;
     }
     final long savedOrdinal = in.readLong();
     if (savedOrdinal + 1L != myInitialFSDelta.getOrdinal()) {
       return null;
     }
     return in;
   } catch (FileNotFoundException ignored) {
   } catch (Throwable e) {
     LOG.error(e);
   }
   return null;
 }
예제 #3
0
  @NotNull
  @Override
  public byte[] contentsToByteArray(@NotNull String relativePath) throws IOException {
    FileAccessorCache.Handle<ZipFile> zipRef;

    try {
      zipRef = getZipFileHandle();
    } catch (RuntimeException ex) {
      Throwable cause = ex.getCause();
      if (cause instanceof IOException) throw (IOException) cause;
      throw ex;
    }

    try {
      ZipFile zip = zipRef.get();
      ZipEntry entry = zip.getEntry(relativePath);
      if (entry != null) {
        InputStream stream = zip.getInputStream(entry);
        if (stream != null) {
          // ZipFile.c#Java_java_util_zip_ZipFile_read reads data in 8K (stack allocated) blocks -
          // no sense to create BufferedInputStream
          try {
            return FileUtil.loadBytes(stream, (int) entry.getSize());
          } finally {
            stream.close();
          }
        }
      }
    } finally {
      zipRef.release();
    }

    throw new FileNotFoundException(getFile() + "!/" + relativePath);
  }
  @NotNull
  private static File copyJarFileWithoutEntry(
      @NotNull File jarPath, @NotNull String... entriesToDelete) {
    try {
      File outputFile =
          new File(
              jarPath.getParentFile(), FileUtil.getNameWithoutExtension(jarPath) + "-after.jar");
      Set<String> toDelete = SetsKt.setOf(entriesToDelete);

      @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
      JarFile jar = new JarFile(jarPath);
      ZipOutputStream output =
          new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
      try {
        for (Enumeration<JarEntry> enumeration = jar.entries(); enumeration.hasMoreElements(); ) {
          JarEntry jarEntry = enumeration.nextElement();
          if (toDelete.contains(jarEntry.getName())) {
            continue;
          }
          output.putNextEntry(jarEntry);
          output.write(FileUtil.loadBytes(jar.getInputStream(jarEntry)));
          output.closeEntry();
        }
      } finally {
        output.close();
        jar.close();
      }

      return outputFile;
    } catch (IOException e) {
      throw ExceptionUtilsKt.rethrow(e);
    }
  }
예제 #5
0
  @Override
  @NotNull
  public byte[] contentsToByteArray(@NotNull final VirtualFile file, boolean cacheContent)
      throws IOException {
    InputStream contentStream = null;
    boolean reloadFromDelegate;
    boolean outdated;
    int fileId;
    synchronized (myInputLock) {
      fileId = getFileId(file);
      outdated = checkFlag(fileId, MUST_RELOAD_CONTENT) || FSRecords.getLength(fileId) == -1L;
      reloadFromDelegate = outdated || (contentStream = readContent(file)) == null;
    }

    if (reloadFromDelegate) {
      final NewVirtualFileSystem delegate = getDelegate(file);

      final byte[] content;
      if (outdated) {
        // in this case, file can have out-of-date length. so, update it first (it's needed for
        // correct contentsToByteArray() work)
        // see IDEA-90813 for possible bugs
        FSRecords.setLength(fileId, delegate.getLength(file));
        content = delegate.contentsToByteArray(file);
      } else {
        // a bit of optimization
        content = delegate.contentsToByteArray(file);
        FSRecords.setLength(fileId, content.length);
      }

      ApplicationEx application = (ApplicationEx) ApplicationManager.getApplication();
      // we should cache every local files content
      // because the local history feature is currently depends on this cache,
      // perforce offline mode as well
      if ((!delegate.isReadOnly()
              ||
              // do not cache archive content unless asked
              cacheContent && !application.isInternal() && !application.isUnitTestMode())
          && content.length <= PersistentFSConstants.FILE_LENGTH_TO_CACHE_THRESHOLD) {
        synchronized (myInputLock) {
          writeContent(file, new ByteSequence(content), delegate.isReadOnly());
          setFlag(file, MUST_RELOAD_CONTENT, false);
        }
      }

      return content;
    } else {
      try {
        final int length = (int) file.getLength();
        assert length >= 0 : file;
        return FileUtil.loadBytes(contentStream, length);
      } catch (IOException e) {
        throw FSRecords.handleError(e);
      }
    }
  }
  @Nullable
  static IdeaPluginDescriptorImpl loadDescriptorFromJar(File file, @NonNls String fileName) {
    try {
      URI fileURL = file.toURI();
      URL jarURL =
          new URL(
              "jar:"
                  + StringUtil.replace(fileURL.toASCIIString(), "!", "%21")
                  + "!/META-INF/"
                  + fileName);

      IdeaPluginDescriptorImpl descriptor = new IdeaPluginDescriptorImpl(file);
      FileInputStream in = new FileInputStream(file);
      ZipInputStream zipStream = new ZipInputStream(in);
      try {
        ZipEntry entry = zipStream.getNextEntry();
        if (entry.getName().equals(JarMemoryLoader.SIZE_ENTRY)) {
          entry = zipStream.getNextEntry();
          if (entry.getName().equals("META-INF/" + fileName)) {
            byte[] content = FileUtil.loadBytes(zipStream, (int) entry.getSize());
            Document document = JDOMUtil.loadDocument(new ByteArrayInputStream(content));
            descriptor.readExternal(document, jarURL);
            return descriptor;
          }
        }
      } finally {
        zipStream.close();
        in.close();
      }

      descriptor.readExternal(jarURL);
      return descriptor;
    } catch (XmlSerializationException e) {
      getLogger().info("Cannot load " + file, e);
      prepareLoadingPluginsErrorMessage(
          "Plugin file " + file.getName() + " contains invalid plugin descriptor file.");
    } catch (FileNotFoundException e) {
      return null;
    } catch (Exception e) {
      getLogger().info("Cannot load " + file, e);
    } catch (Throwable e) {
      getLogger().info("Cannot load " + file, e);
    }

    return null;
  }
예제 #7
0
  // todo[r.sh] drop after migration on Java 7
  private static void loadSnappyForJRockit() throws Exception {
    String vmName = System.getProperty("java.vm.name");
    if (vmName == null || !vmName.toLowerCase().contains("jrockit")) {
      return;
    }

    byte[] bytes;
    InputStream in =
        StartupUtil.class.getResourceAsStream("/org/xerial/snappy/SnappyNativeLoader.bytecode");
    try {
      bytes = FileUtil.loadBytes(in);
    } finally {
      in.close();
    }

    ClassLoader classLoader = StartupUtil.class.getClassLoader();

    Method defineClass =
        ClassLoader.class.getDeclaredMethod(
            "defineClass", String.class, byte[].class, int.class, int.class);
    defineClass.setAccessible(true);
    Class<?> loaderClass =
        (Class<?>)
            defineClass.invoke(
                classLoader, "org.xerial.snappy.SnappyNativeLoader", bytes, 0, bytes.length);
    loaderClass = classLoader.loadClass(loaderClass.getName());

    String[] classes = {
      "org.xerial.snappy.SnappyNativeAPI",
      "org.xerial.snappy.SnappyNative",
      "org.xerial.snappy.SnappyErrorCode"
    };
    for (String aClass : classes) {
      classLoader.loadClass(aClass);
    }

    Method loadNativeLibrary =
        SnappyLoader.class.getDeclaredMethod("loadNativeLibrary", Class.class);
    loadNativeLibrary.setAccessible(true);
    loadNativeLibrary.invoke(null, loaderClass);
  }