@Override public InputStream getInputStream() throws IOException, UnsupportedFileOperationException { return file.getInputStream(); }
@Override public InputStream getInputStream(long offset) throws IOException, UnsupportedFileOperationException { return file.getInputStream(offset); }
public MuRandomAccessFile(AbstractFile file) throws UnsupportedFileOperationException { super(); position = 0; this.file = file; try { stream = file.getInputStream(); } catch (IOException e) { LOGGER.trace("Error", e); } }
@Override public long Seek(long offset, int seekOrigin) throws IOException { if (seekOrigin == STREAM_SEEK_SET) { stream.close(); stream = file.getInputStream(); stream.skip(offset); position = offset; } else if (seekOrigin == STREAM_SEEK_CUR) { stream.skip(offset); position += offset; } return position; }
@Override public InputStream getInputStream() throws IOException { checkConnected(); return file.getInputStream(); }
public RarFile(AbstractFile file) throws IOException, UnsupportedFileOperationException, RarException { InputStream fileIn = file.getInputStream(); try { archive = new Archive(new File(file.getPath())); } finally { fileIn.close(); } }
/** * Returns a checksum of this file (also referred to as <i>hash</i> or <i>digest</i>) calculated by reading this * file's contents and feeding the bytes to the given <code>MessageDigest</code>, until EOF is reached. * * <p>The checksum is returned as an hexadecimal string, such as "6d75636f0a". The length of this string depends on * the kind of <code>MessageDigest</code>.</p> * * <p>Note: this method does not reset the <code>MessageDigest</code> after the checksum has been calculated.</p> * * @param messageDigest the MessageDigest to use for calculating the checksum * @return this file's checksum, as an hexadecimal string * @throws IOException if an I/O error occurred while calculating the checksum * @throws UnsupportedFileOperationException if this method relies on a file operation that is not supported * or not implemented by the underlying filesystem. */ public final String calculateChecksum(MessageDigest messageDigest) throws IOException, UnsupportedFileOperationException { InputStream in = getInputStream(); try { return calculateChecksum(in, messageDigest); } finally { in.close(); } }
/** * Returns an input stream on the requested resource. * @param name name of the resource to open. * @return an input stream on the requested resource, <code>null</code> if not found. */ @Override public InputStream getResourceAsStream(String name) { AbstractFile file; // File representing the resource. InputStream in; // Input stream on the resource. // Tries the parent first, to respect the delegation model. if((in = getParent().getResourceAsStream(name)) != null) return in; // Tries to locate the resource in the extended classpath if it wasn't found // in the parent. if((file = findResourceAsFile(name)) != null) { try {return file.getInputStream();} catch(Exception e) {} } // Couldn't find the resource. return null; }
private String calculateChecksum(AbstractFile file) throws IOException, NoSuchAlgorithmException { currentFileByteCounter.reset(); InputStream in = setCurrentInputStream(file.getInputStream()); try { return AbstractFile.calculateChecksum(in, MessageDigest.getInstance(CHECKSUM_VERIFICATION_ALGORITHM)); } finally { closeCurrentInputStream(); } }
@Override public InputStream getEntryInputStream(ArchiveEntry entry, ArchiveEntryIterator entryIterator) throws IOException, UnsupportedFileOperationException { // Will throw an IOException if the file designated by the entry doesn't exist return FileFactory.getFile(((LstArchiveEntry)entry).getBaseFolder()+entry.getPath(), true).getInputStream(); } }
/** * Asserts that both files contain the same data, by calculating their checksum and comparing them. * * @param file1 the first file to compare * @param file2 the second file to compare * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ protected void assertContentsEquals(AbstractFile file1, AbstractFile file2) throws IOException, NoSuchAlgorithmException { InputStream in1 = null; InputStream in2 = null; try { in1 = file1.getInputStream(); in2 = file2.getInputStream(); assertInputStreamEquals(in1, in2); } finally { if(in1!=null) try { in1.close(); } catch(IOException e) {} if(in2!=null) try { in2.close(); } catch(IOException e) {} } }
@Override public synchronized ArchiveEntryIterator getEntryIterator() throws IOException, UnsupportedFileOperationException { // If the underlying AbstractFile has random read access, use our own ZipFile implementation to read entries if (file.isFileOperationSupported(FileOperation.RANDOM_READ_FILE)) { checkZipFile(); final Iterator<ZipEntry> iterator = zipFile.getEntries(); return new ArchiveEntryIterator() { public ArchiveEntry nextEntry() throws IOException { ZipEntry entry; if(!iterator.hasNext() || (entry = iterator.next())==null) return null; return createArchiveEntry(entry); } public void close() throws IOException { } }; } // If the underlying AbstractFile doesn't have random read access, use java.util.zip.ZipInputStream to // read the entries. This is much slower than the former method as the file cannot be sought through and needs // to be traversed. else { return new JavaUtilZipEntryIterator(new ZipInputStream(file.getInputStream())); } }
/** * Calculates and returns the md5 checksum of the given <code>AbstractFile</code>'s contents. * * @param file the file to digest * @return the md5 checksum of the given InputStream's contents * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ protected String calculateMd5(AbstractFile file) throws IOException, NoSuchAlgorithmException { InputStream in = file.getInputStream(); try { return calculateMd5(in); } finally { in.close(); } }
/** * Checks the specified XML file that are embedded with muCommander for well-formedness. The test fails * whenever an error is encountered in one of the XML files, even a recoverable one. * * @param file the file to parse and check for well-formedness. * @throws IOException if there was an error reading the file * @throws SAXException if an error was found in XML document * @throws ParserConfigurationException 'a serious configuration error' occurred in the XML parser */ private void testXMLDocument(AbstractFile file) throws SAXException, IOException, ParserConfigurationException { System.out.println("Parsing "+file.getAbsolutePath()); SAXParserFactory.newInstance().newSAXParser().parse(file.getInputStream(), new SAXErrorHandler()); }
/** * Tests {@link AbstractFile#getInputStream()} when the operation is not supported. * * @throws IOException should not happen */ protected void testGetInputStreamUnsupported() throws IOException { // Assert that #getInputStream throws a proper UnsupportedFileOperationException when called UnsupportedFileOperationException e = null; try { tempFile.getInputStream(); } catch(UnsupportedFileOperationException ex) { e = ex; } assertUnsupportedFileOperationException(e, FileOperation.READ_FILE); // And again with #getInputStream(long) try { tempFile.getInputStream(27); } catch(UnsupportedFileOperationException ex) { e = ex; } assertUnsupportedFileOperationException(e, FileOperation.READ_FILE); }
/** * Opens a stream on the right file. * <p> * If a backup file is found, and is bigger than the target file, then it will be used. * </p> * @param file file on which to open an input stream. * @return a stream on the right file. * @exception IOException thrown if any IO related error occurs. */ private static InputStream getInputStream(AbstractFile file) throws IOException { AbstractFile backup; FileURL test; test = (FileURL)file.getURL().clone(); test.setPath(test.getPath() + BACKUP_SUFFIX); // Checks whether the backup file is a better choice than the target one. backup = FileFactory.getFile(test); if(backup != null && backup.exists() && (file.getSize() < backup.getSize())) return backup.getInputStream(); // Opens a stream on the target file. return file.getInputStream(); } }
private void assertReadable(AbstractFile file) throws IOException { assert file != null; assertReadable(file.getInputStream()); assert file.exists(); }
private boolean doGenericChecks(AbstractFile file) throws WarnUserException { // Do not allow directories if(file.isDirectory()) return false; // Warn the user if the file is large that a certain size as the whole file is loaded into memory // (in a JTextArea) if(file.getSize()>1048576) throw new WarnUserException(Translator.get("file_viewer.large_file_warning")); // Warn the user if the file looks like a binary file InputStream in = null; try { in = file.getInputStream(); if(BinaryDetector.guessBinary(in)) return false; } catch(IOException e) { // Not much too do } finally { if(in!=null) { try { in.close(); } catch(IOException e2) {} } } return true; } }
public void encodingChanged(Object source, String oldEncoding, String newEncoding) { try { // Reload the file using the new encoding // Note: loadDocument closes the InputStream loadDocument(getCurrentFile().getInputStream(), newEncoding, null); } catch(IOException ex) { InformationDialog.showErrorDialog(getFrame(), Translator.get("read_error"), Translator.get("file_editor.cannot_read_file", getCurrentFile().getName())); } } }
public void encodingChanged(Object source, String oldEncoding, String newEncoding) { if(!askSave()) return; // Abort if the file could not be saved try { // Reload the file using the new encoding // Note: loadDocument closes the InputStream loadDocument(getCurrentFile().getInputStream(), newEncoding, null); } catch(IOException ex) { InformationDialog.showErrorDialog(getFrame(), Translator.get("read_error"), Translator.get("file_editor.cannot_read_file", getCurrentFile().getName())); } } }