Search in sources :

Example 1 with RandomAccessOutputStream

use of com.mucommander.commons.io.RandomAccessOutputStream in project mucommander by mucommander.

the class MkdirJob method processFile.

// //////////////////////////
// FileJob implementation //
// //////////////////////////
/**
 * Creates the new directory in the destination folder.
 */
@Override
protected boolean processFile(AbstractFile file, Object recurseParams) {
    // Stop if interrupted (although there is no way to stop the job at this time)
    if (getState() == FileJobState.INTERRUPTED)
        return false;
    do {
        try {
            LOGGER.debug("Creating: {}", file);
            // Check for file collisions, i.e. if the file already exists in the destination
            int collision = FileCollisionChecker.checkForCollision(null, file);
            if (collision != FileCollisionChecker.NO_COLLISION) {
                // File already exists in destination, ask the user what to do (cancel, overwrite,...) but
                // do not offer the multiple files' mode options such as 'skip' and 'apply to all'.
                DialogAction choice = waitForUserResponse(new FileCollisionDialog(getMainFrame(), getMainFrame(), collision, null, file, false, false));
                // Overwrite file
                if (choice == FileCollisionDialog.FileCollisionAction.OVERWRITE) {
                    // Delete the file
                    file.delete();
                } else // Cancel or dialog close (return)
                // else if (choice==-1 || choice==FileCollisionDialog.OverwriteAction.CANCEL) {
                {
                    interrupt();
                    return false;
                }
            }
            // Create file
            if (mkfileMode) {
                // Use mkfile
                if (allocateSpace == -1) {
                    file.mkfile();
                } else // Allocate the requested number of bytes
                {
                    OutputStream mkfileOut = null;
                    try {
                        // using RandomAccessOutputStream if we can have one
                        if (file.isFileOperationSupported(FileOperation.RANDOM_WRITE_FILE)) {
                            mkfileOut = file.getRandomAccessOutputStream();
                            ((RandomAccessOutputStream) mkfileOut).setLength(allocateSpace);
                        } else // manually otherwise
                        {
                            mkfileOut = file.getOutputStream();
                            // Use BufferPool to avoid excessive memory allocation and garbage collection
                            byte[] buffer = BufferPool.getByteArray();
                            int bufferSize = buffer.length;
                            try {
                                long remaining = allocateSpace;
                                int nbWrite;
                                while (remaining > 0 && getState() != FileJobState.INTERRUPTED) {
                                    nbWrite = (int) (remaining > bufferSize ? bufferSize : remaining);
                                    mkfileOut.write(buffer, 0, nbWrite);
                                    remaining -= nbWrite;
                                }
                            } finally {
                                BufferPool.releaseByteArray(buffer);
                            }
                        }
                    } finally {
                        if (mkfileOut != null)
                            try {
                                mkfileOut.close();
                            } catch (IOException e) {
                            }
                    }
                }
            } else // Create directory
            {
                file.mkdir();
            }
            // Resolve new file instance now that it exists: remote files do not update file attributes after
            // creation, we need to get an instance that reflects the newly created file attributes
            file = FileFactory.getFile(file.getURL());
            // Select newly created file when job is finished
            selectFileWhenFinished(file);
            // Return Success
            return true;
        } catch (IOException e) {
            // thrown, this is normal behavior
            if (mkfileMode && getState() == FileJobState.INTERRUPTED)
                return false;
            LOGGER.debug("IOException caught", e);
            DialogAction action = showErrorDialog(Translator.get("error"), Translator.get(mkfileMode ? "cannot_write_file" : "cannot_create_folder", file.getAbsolutePath()), Arrays.asList(FileJobAction.RETRY, FileJobAction.CANCEL));
            // Retry (loop)
            if (action == FileJobAction.RETRY)
                continue;
            // Return Failure
            return false;
        }
    } while (true);
}
Also used : FileCollisionDialog(com.mucommander.ui.dialog.file.FileCollisionDialog) DialogAction(com.mucommander.ui.dialog.DialogAction) OutputStream(java.io.OutputStream) RandomAccessOutputStream(com.mucommander.commons.io.RandomAccessOutputStream) RandomAccessOutputStream(com.mucommander.commons.io.RandomAccessOutputStream) IOException(java.io.IOException)

Example 2 with RandomAccessOutputStream

use of com.mucommander.commons.io.RandomAccessOutputStream in project mucommander by mucommander.

the class FileMonitorTest method testSizeAttribute.

/**
 * Validates that FileMonitor properly reports {@link FileMonitor#SIZE_ATTRIBUTE} changes when a file's size changes.
 *
 * @throws IOException should not normally happen
 */
@Test
public void testSizeAttribute() throws IOException {
    setUp(SIZE_ATTRIBUTE);
    RandomAccessOutputStream raos = file.getRandomAccessOutputStream();
    try {
        raos.setLength(10);
        assert hasAttributeChanged(SIZE_ATTRIBUTE);
        raos.setLength(0);
    } finally {
        if (raos != null)
            raos.close();
    }
}
Also used : RandomAccessOutputStream(com.mucommander.commons.io.RandomAccessOutputStream) Test(org.testng.annotations.Test)

Example 3 with RandomAccessOutputStream

use of com.mucommander.commons.io.RandomAccessOutputStream in project mucommander by mucommander.

the class ZipOutputStream method finalizeEntryData.

/**
 * Writes the size and CRC information of an entry. This method is to be called right after a file entry's data
 * has been written.
 *
 * <p>The size and CRC information is written to the given <code>OutputStream</code>, either as a data descriptor or
 * in the entry's local file header, and is set in the given {@link ZipEntry} instance.
 *
 * @param entry the entry
 * @param zeos the Zip entry's output stream
 * @param out the
 * @param useDataDescriptor if true, a data descriptor will be written to out. If false, size and CRC information
 * will be written in the local file header (requires out to be a RandomAccessOutputStream).
 * @param zipBuffer a ZipBuffer instance used to convert integer values to Zip variants
 * @throws IOException if an I/O error occurred
 */
protected static void finalizeEntryData(ZipEntry entry, ZipEntryOutputStream zeos, OutputStream out, boolean useDataDescriptor, ZipBuffer zipBuffer) throws IOException {
    long crc = zeos.getCrc();
    if (entry.getMethod() == DEFLATED) {
        ((DeflatedOutputStream) zeos).finishDeflate();
        entry.setSize(adjustToLong(zeos.getTotalIn()));
        long compressedSize = adjustToLong(zeos.getTotalOut());
        entry.setCompressedSize(compressedSize);
        entry.setCrc(crc);
    } else {
        // Method is STORED
        long size = zeos.getTotalOut();
        entry.setSize(size);
        entry.setCompressedSize(size);
        entry.setCrc(crc);
    }
    // the correct CRC and compressed/uncompressed sizes
    if (!useDataDescriptor) {
        RandomAccessOutputStream raos = (RandomAccessOutputStream) out;
        long save = raos.getOffset();
        raos.seek(entry.getEntryInfo().headerOffset + 14);
        raos.write(ZipLong.getBytes(entry.getCrc(), zipBuffer.longBuffer));
        raos.write(ZipLong.getBytes(entry.getCompressedSize(), zipBuffer.longBuffer));
        raos.write(ZipLong.getBytes(entry.getSize(), zipBuffer.longBuffer));
        raos.seek(save);
    }
}
Also used : RandomAccessOutputStream(com.mucommander.commons.io.RandomAccessOutputStream)

Aggregations

RandomAccessOutputStream (com.mucommander.commons.io.RandomAccessOutputStream)3 DialogAction (com.mucommander.ui.dialog.DialogAction)1 FileCollisionDialog (com.mucommander.ui.dialog.file.FileCollisionDialog)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Test (org.testng.annotations.Test)1