/** * Opens an output stream on the specified file using the specified backup file. * @param file file on which to open the backup output stream. * @param save file that will be used for backup. * @exception IOException thrown if any IO error occurs. */ private BackupOutputStream(AbstractFile file, AbstractFile save) throws IOException { out = save.getOutputStream(); target = file; backup = save; }
@Override public OutputStream getOutputStream() throws IOException, UnsupportedFileOperationException { return file.getOutputStream(); }
@Override public OutputStream getOutputStream() throws IOException { checkConnected(); return file.getOutputStream(); }
/** * Creates this file as an empty, non-directory file. This method will fail (throw an <code>IOException</code>) * if this file already exists. Note that this method may not always yield a zero-byte file (see below). * * <p>This generic implementation simply creates a zero-byte file. {@link AbstractRWArchiveFile} implementations * may want to override this method so that it creates a valid archive with no entry. To illustrate, an empty Zip * file with proper headers is 22-byte long.</p> * * @throws IOException if the file could not be created, either because it already exists or because of an I/O error * @throws UnsupportedFileOperationException if this method relies on a file operation that is not supported * or not implemented by the underlying filesystem. */ public void mkfile() throws IOException, UnsupportedFileOperationException { if(exists()) throw new IOException(); if(isFileOperationSupported(FileOperation.WRITE_FILE)) getOutputStream().close(); else copyStream(new ByteArrayInputStream(new byte[]{}), false, 0); }
@Override protected void saveAs(AbstractFile destFile) throws IOException { OutputStream out = null; try { out = destFile.getOutputStream(); write(out); } finally { if(out != null) { try {out.close();} catch(IOException e) { // Ignored } } } // We get here only if the destination file was updated successfully // so we can set that no further save is needed at this stage setSaveNeeded(false); // Change the parent folder's date to now, so that changes are picked up by folder auto-refresh (see ticket #258) if(destFile.isFileOperationSupported(FileOperation.CHANGE_DATE)) { try { destFile.getParent().changeDate(System.currentTimeMillis()); } catch (IOException e) { LOGGER.debug("failed to change the date of "+destFile, e); // Fail silently } } }
infoWriter = new OutputStreamWriter(infoFile.getOutputStream()); infoWriter.write(fileInfoContent); } catch (IOException ex) {
infoWriter = new OutputStreamWriter(infoFile.getOutputStream()); infoWriter.write(fileInfoContent); } catch (IOException ex) {
tmpOut = new OutputStreamWriter(tmpFile.getOutputStream(), "utf-8");
OutputStream crcStream = crcFile.getOutputStream(); String line = sourceFile.getName() + " " + sourceChecksum; crcStream.write(line.getBytes("utf-8"));
out = append?getAppendOutputStream():getOutputStream();
out = new BufferedOutputStream(file.getOutputStream());
/** * Creates the destination (merged) file. * @param file first part */ protected void createDestFile(AbstractFile file) { destFile = baseDestFolder; baseDestFolder = baseDestFolder.getParent(); destFile = checkForCollision(file, baseDestFolder, destFile, false); if (destFile == null) { interrupt(); return; } try { out = destFile.getOutputStream(); } catch(IOException e) { LOGGER.debug("Caught exception", e); showErrorDialog(errorDialogTitle, Translator.get("error_while_transferring", destFile.getName()), new String[]{FileJobAction.CANCEL_TEXT}, new int[]{FileJobAction.CANCEL} ); interrupt(); } }
/** * Tests {@link AbstractFile#getOutputStream()} when the operation is supported. * * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ protected void testGetOutputStreamSupported() throws IOException, NoSuchAlgorithmException { // Assert that: // - getOutputStream does not throw an IOException // - returns a non-null value // - the file exists after OutputStream out = tempFile.getOutputStream(); assert out != null; assert tempFile.exists(); assert 0 == tempFile.getSize(); out.close(); // Assert that getOutputStream() overwrites the existing file contents (resets the file size to 0) createFile(tempFile, 1); out = tempFile.getOutputStream(); out.close(); assert 0 == tempFile.getSize(); // Test the integrity of the OutputStream after writing a somewhat large amount of random data ChecksumOutputStream md5Out = getMd5OutputStream(tempFile.getOutputStream()); writeRandomData(md5Out, 100000, 1000); md5Out.close(); assert md5Out.getChecksumString().equals(calculateMd5(tempFile)); }
/** * Tests {@link AbstractFile#getOutputStream()} when the operation is not supported. * * @throws IOException should not happen */ protected void testGetOutputStreamUnsupported() throws IOException { // Assert that #getOutputStream throws a proper UnsupportedFileOperationException when called UnsupportedFileOperationException e = null; try { tempFile.getOutputStream(); } catch(UnsupportedFileOperationException ex) { e = ex; } assertUnsupportedFileOperationException(e, FileOperation.WRITE_FILE); assert !tempFile.exists(); assert 0 == tempFile.getSize(); }
try { tempFile = FileFactory.getTemporaryFile(false); tempOut = tempFile.getOutputStream();
/** * Tests {@link AbstractFile#getAppendOutputStream()} when the operation is supported. * * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ protected void testGetAppendOutputStreamSupported() throws IOException, NoSuchAlgorithmException { // Assert that: // - getAppendOutputStream does not throw an IOException // - returns a non-null value // - the file exists after OutputStream out = tempFile.getAppendOutputStream(); assert out != null; assert tempFile.exists(); assert 0 == tempFile.getSize(); out.close(); // Assert that getAppendOutputStream() does not overwrite the existing file contents. // Appending to the file may not be supported, catch IOException thrown by getAppendOutputStream() and only those createFile(tempFile, 1); out = tempFile.getAppendOutputStream(); out.write('a'); out.close(); assert 2 == tempFile.getSize(); // Test the integrity of the OutputStream after writing a somewhat large amount of random data ChecksumOutputStream md5Out = getMd5OutputStream(tempFile.getOutputStream()); writeRandomData(md5Out, 100000, 1000); md5Out.close(); assert md5Out.getChecksumString().equals(calculateMd5(tempFile)); }
out = destFile.getOutputStream();
mkfileOut = file.getOutputStream();
try { this.checksumFileOut = checksumFile.getOutputStream();