コード例 #1
0
  public static boolean isSubtitlesExists(File file, DLNAMediaInfo media, boolean usecache) {
    boolean found = false;
    if (file.exists()) {
      found = browseFolderForSubtitles(file.getParentFile(), file, media, usecache);
    }
    String alternate = PMS.getConfiguration().getAlternateSubtitlesFolder();

    if (isNotBlank(alternate)) { // https://code.google.com/p/ps3mediaserver/issues/detail?id=737#c5
      File subFolder = new File(alternate);

      if (!subFolder.isAbsolute()) {
        subFolder = new File(file.getParent() + "/" + alternate);
        try {
          subFolder = subFolder.getCanonicalFile();
        } catch (IOException e) {
          LOGGER.debug("Caught exception", e);
        }
      }

      if (subFolder.exists()) {
        found = found || browseFolderForSubtitles(subFolder, file, media, usecache);
      }
    }

    return found;
  }
コード例 #2
0
  // Loosely based on the workaround posted here:
  // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4993360
  // XXX why isn't this in Apache Commons?
  public static boolean isFileWritable(File file) {
    boolean isWritable = false;

    if (file != null) {
      boolean fileAlreadyExists = file.isFile(); // i.e. exists and is a File

      if (fileAlreadyExists || !file.exists()) {
        try {
          // true: open for append: make sure the open
          // doesn't clobber the file
          new FileOutputStream(file, true).close();
          isWritable = true;

          if (!fileAlreadyExists) { // a new file has been "touch"ed; try to remove it
            try {
              if (!file.delete()) {
                LOGGER.warn("Can't delete temporary test file: {}", file.getAbsolutePath());
              }
            } catch (SecurityException se) {
              LOGGER.error("Error deleting temporary test file: " + file.getAbsolutePath(), se);
            }
          }
        } catch (IOException | SecurityException ioe) {
        }
      }
    }

    return isWritable;
  }
コード例 #3
0
  // this is called from a static initialiser, where errors aren't clearly reported,
  // so do everything possible to return a valid reponse, even if the parameters
  // aren't sane
  public static FileLocation getFileLocation(
      String customPath, String defaultDirectory, String defaultBasename) {
    File customFile = null;
    File directory = null;
    File file = null;

    if (isBlank(defaultBasename)) {
      // shouldn't get here
      defaultBasename = DEFAULT_BASENAME;
    }

    if (defaultDirectory == null) {
      defaultDirectory = ""; // current directory
    }

    if (customPath != null) {
      customFile = new File(customPath).getAbsoluteFile();
    }

    if (customFile != null) {
      if (customFile.exists()) {
        if (customFile.isDirectory()) {
          directory = customFile;
          file = new File(customFile, defaultBasename).getAbsoluteFile();
        } else {
          directory = customFile.getParentFile();
          file = customFile;
        }
      } else {
        File parentDirectoryFile = customFile.getParentFile();
        if (parentDirectoryFile != null && parentDirectoryFile.exists()) {
          // parent directory exists: the file can be created
          directory = parentDirectoryFile;
          file = customFile;
        }
      }
    }

    if (directory == null || file == null) {
      directory = new File(defaultDirectory).getAbsoluteFile();
      file = new File(directory, defaultBasename).getAbsoluteFile();
    }

    return new FileLocation(directory, file);
  }
コード例 #4
0
  public static File getFileNameWithAddedExtension(File parent, File f, String ext) {
    File ff = new File(parent, f.getName() + ext);

    if (ff.exists()) {
      return ff;
    }

    return null;
  }
コード例 #5
0
  public static File isFileExists(File f, String ext) {
    int point = f.getName().lastIndexOf('.');

    if (point == -1) {
      point = f.getName().length();
    }

    File lowerCasedFile =
        new File(f.getParentFile(), f.getName().substring(0, point) + "." + ext.toLowerCase());
    if (lowerCasedFile.exists()) {
      return lowerCasedFile;
    }

    File upperCasedFile =
        new File(f.getParentFile(), f.getName().substring(0, point) + "." + ext.toUpperCase());
    if (upperCasedFile.exists()) {
      return upperCasedFile;
    }

    return null;
  }
コード例 #6
0
  /**
   * Converts UTF-16 inputFile to UTF-8 outputFile. Does not overwrite existing outputFile file.
   *
   * @param inputFile UTF-16 file
   * @param outputFile UTF-8 file after conversion
   * @throws IOException
   */
  public static void convertFileFromUtf16ToUtf8(File inputFile, File outputFile)
      throws IOException {
    String charset;
    if (inputFile == null || !inputFile.canRead()) {
      throw new FileNotFoundException("Can't read inputFile.");
    }

    try {
      charset = getFileCharset(inputFile);
    } catch (IOException ex) {
      LOGGER.debug("Exception during charset detection.", ex);
      throw new IllegalArgumentException("Can't confirm inputFile is UTF-16.");
    }

    if (isCharsetUTF16(charset)) {
      if (!outputFile.exists()) {
        BufferedReader reader = null;

        try {
          if (equalsIgnoreCase(charset, CHARSET_UTF_16LE)) {
            reader =
                new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "UTF-16"));
          } else {
            reader =
                new BufferedReader(
                    new InputStreamReader(new FileInputStream(inputFile), "UTF-16BE"));
          }
        } catch (UnsupportedEncodingException ex) {
          LOGGER.warn("Unsupported exception.", ex);
          throw ex;
        }

        BufferedWriter writer =
            new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
        int c;

        while ((c = reader.read()) != -1) {
          writer.write(c);
        }

        writer.close();
        reader.close();
      }
    } else {
      throw new IllegalArgumentException("File is not UTF-16");
    }
  }