Search in sources :

Example 56 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project sis by apache.

the class TestUtilities method createTemporaryFile.

/**
 * Copies the full content of the given test resource in a temporary file and returns the channel for that file.
 * The file is opened with {@link StandardOpenOption#DELETE_ON_CLOSE}, together with read and write options.
 *
 * @param  caller    defines the root from which to search for the {@code resource}.
 * @param  resource  path (relative to the {@code caller}) of the test file to copy.
 * @return a channel opened on a copy of the content of the given test resource.
 * @throws IOException if an error occurred while copying the data.
 *
 * @since 0.8
 */
public static SeekableByteChannel createTemporaryFile(final Class<?> caller, final String resource) throws IOException {
    final SeekableByteChannel channel;
    try (ReadableByteChannel in = Channels.newChannel(caller.getResourceAsStream(resource))) {
        final int s = resource.lastIndexOf('.');
        final Path file = Files.createTempFile("SIS", (s >= 0) ? resource.substring(s) : null);
        channel = Files.newByteChannel(file, StandardOpenOption.DELETE_ON_CLOSE, StandardOpenOption.READ, StandardOpenOption.WRITE);
        final ByteBuffer buffer = ByteBuffer.allocate(4000);
        while (in.read(buffer) >= 0) {
            buffer.flip();
            channel.write(buffer);
            buffer.clear();
        }
    }
    return channel.position(0);
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) Path(java.nio.file.Path) ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 57 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project dataverse by IQSS.

the class DataConverter method performFormatConversion.

public static StorageIO<DataFile> performFormatConversion(DataFile file, StorageIO<DataFile> storageIO, String formatRequested, String formatType) {
    if (!file.isTabularData()) {
        return null;
    }
    // we don't need to do anything:
    if (formatRequested.equals(FILE_TYPE_TAB) && file.getContentType().equals("text/tab-separated-values")) {
        return storageIO;
    }
    InputStream convertedFileStream = null;
    long convertedFileSize = 0;
    // format:
    try {
        convertedFileStream = Channels.newInputStream((ReadableByteChannel) storageIO.openAuxChannel(formatRequested));
        convertedFileSize = storageIO.getAuxObjectSize(formatRequested);
    } catch (IOException ioex) {
        logger.fine("No cached copy for file format " + formatRequested + ", file " + file.getStorageIdentifier());
        convertedFileStream = null;
    }
    // If not cached, run the conversion:
    if (convertedFileStream == null) {
        File tabFile = null;
        boolean tempFilesRequired = false;
        try {
            Path tabFilePath = storageIO.getFileSystemPath();
            tabFile = tabFilePath.toFile();
        } catch (UnsupportedDataAccessOperationException uoex) {
            // this means there is no direct filesystem path for this object; it's ok!
            logger.fine("Could not open source file as a local Path - will go the temp file route.");
            tempFilesRequired = true;
        } catch (IOException ioex) {
            // this is likely a fatal condition, as in, the file is unaccessible:
            return null;
        }
        if (tempFilesRequired) {
            ReadableByteChannel tabFileChannel = null;
            try {
                logger.fine("opening datafFileIO for the source tabular file...");
                storageIO.open();
                tabFileChannel = storageIO.getReadChannel();
                FileChannel tempFileChannel;
                tabFile = File.createTempFile("tempTabFile", ".tmp");
                tempFileChannel = new FileOutputStream(tabFile).getChannel();
                tempFileChannel.transferFrom(tabFileChannel, 0, storageIO.getSize());
            } catch (IOException ioex) {
                logger.warning("caught IOException trying to store tabular file " + storageIO.getDataFile().getStorageIdentifier() + " as a temp file.");
                return null;
            }
        }
        if (tabFile == null) {
            return null;
        }
        if (tabFile.length() > 0) {
            File formatConvertedFile = runFormatConversion(file, tabFile, formatRequested);
            // cache the result for future use:
            if (formatConvertedFile != null && formatConvertedFile.exists()) {
                try {
                    storageIO.savePathAsAux(Paths.get(formatConvertedFile.getAbsolutePath()), formatRequested);
                } catch (IOException ex) {
                    logger.warning("failed to save cached format " + formatRequested + " for " + file.getStorageIdentifier());
                // We'll assume that this is a non-fatal condition.
                }
                // re-open the generated file:
                try {
                    convertedFileStream = new FileInputStream(formatConvertedFile);
                    convertedFileSize = formatConvertedFile.length();
                } catch (FileNotFoundException ioex) {
                    logger.warning("Failed to open generated format " + formatRequested + " for " + file.getStorageIdentifier());
                    return null;
                }
            }
        }
    }
    // download API instance writer:
    if (convertedFileStream != null && convertedFileSize > 0) {
        InputStreamIO inputStreamIO = null;
        try {
            inputStreamIO = new InputStreamIO(convertedFileStream, convertedFileSize);
        } catch (IOException ioex) {
            return null;
        }
        inputStreamIO.setMimeType(formatType);
        String fileName = storageIO.getFileName();
        if (fileName == null || fileName.isEmpty()) {
            fileName = "f" + file.getId().toString();
        }
        inputStreamIO.setFileName(generateAltFileName(formatRequested, fileName));
        return inputStreamIO;
    }
    return null;
}
Also used : Path(java.nio.file.Path) ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) DataFile(edu.harvard.iq.dataverse.DataFile) File(java.io.File)

Example 58 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project dataverse by IQSS.

the class StoredOriginalFile method retreive.

public static StorageIO<DataFile> retreive(StorageIO<DataFile> storageIO) {
    String originalMimeType;
    DataFile dataFile = storageIO.getDataFile();
    if (dataFile == null) {
        return null;
    }
    if (dataFile.getDataTable() != null) {
        originalMimeType = dataFile.getDataTable().getOriginalFileFormat();
    } else {
        return null;
    }
    long storedOriginalSize;
    InputStreamIO inputStreamIO;
    try {
        storageIO.open();
        Channel storedOriginalChannel = storageIO.openAuxChannel(SAVED_ORIGINAL_FILENAME_EXTENSION);
        storedOriginalSize = storageIO.getAuxObjectSize(SAVED_ORIGINAL_FILENAME_EXTENSION);
        inputStreamIO = new InputStreamIO(Channels.newInputStream((ReadableByteChannel) storedOriginalChannel), storedOriginalSize);
        logger.fine("Opened stored original file as Aux " + SAVED_ORIGINAL_FILENAME_EXTENSION);
    } catch (IOException ioEx) {
        // The original file not saved, or could not be opened.
        logger.fine("Failed to open stored original file as Aux " + SAVED_ORIGINAL_FILENAME_EXTENSION + "!");
        return null;
    }
    if (originalMimeType != null && !originalMimeType.isEmpty()) {
        if (originalMimeType.matches("application/x-dvn-.*-zip")) {
            inputStreamIO.setMimeType("application/zip");
        } else {
            inputStreamIO.setMimeType(originalMimeType);
        }
    } else {
        inputStreamIO.setMimeType("application/x-unknown");
    }
    String fileName = storageIO.getFileName();
    if (fileName != null) {
        if (originalMimeType != null) {
            String origFileExtension = generateOriginalExtension(originalMimeType);
            inputStreamIO.setFileName(fileName.replaceAll(".tab$", origFileExtension));
        } else {
            inputStreamIO.setFileName(fileName.replaceAll(".tab$", ""));
        }
    }
    return inputStreamIO;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Channel(java.nio.channels.Channel) IOException(java.io.IOException)

Example 59 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project epp.mpc by eclipse.

the class UnmarshallerTest method readResource.

private ByteBuffer readResource(String resource, ByteBuffer buffer) throws IOException {
    ReadableByteChannel in = Channels.newChannel(getResourceAsStream(resource));
    if (buffer == null) {
        buffer = ByteBuffer.allocate(32768);
    }
    while (true) {
        int read = in.read(buffer);
        if (read == -1) {
            break;
        }
        if (buffer.remaining() < read) {
            ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity() + Math.max(8192, 2 * read));
            buffer.flip();
            newBuffer.put(buffer);
            buffer = newBuffer;
        }
    }
    return buffer;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 60 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project dataverse by IQSS.

the class ImageThumbConverter method generatePDFThumbnail.

private static boolean generatePDFThumbnail(StorageIO<DataFile> storageIO, int size) {
    if (isPdfFileOverSizeLimit(storageIO.getDataFile().getFilesize())) {
        logger.fine("Image file too large (" + storageIO.getDataFile().getFilesize() + " bytes) - skipping");
        return false;
    }
    // better give up right away:
    if (!isImageMagickInstalled()) {
        return false;
    }
    File sourcePdfFile = null;
    // We'll to get a local Path for this file - but if that is not available
    // (i.e., if it's a file that's stored by a driver that does not provide
    // direct file access - e.g., swift), we'll save this PDF in a temp file,
    // will run the ImageMagick on it, and will save its output in another temp
    // file, and will save it as an "auxiliary" file via the driver.
    boolean tempFilesRequired = false;
    try {
        Path pdfFilePath = storageIO.getFileSystemPath();
        sourcePdfFile = pdfFilePath.toFile();
        logger.fine("Opened the source pdf file as a local File.");
    } catch (UnsupportedDataAccessOperationException uoex) {
        // this means there is no direct filesystem path for this object;
        logger.fine("Could not open source pdf file as a local file - will go the temp file route.");
        tempFilesRequired = true;
    } catch (IOException ioex) {
        // this on the other hand is likely a fatal condition :(
        return false;
    }
    if (tempFilesRequired) {
        ReadableByteChannel pdfFileChannel;
        try {
            storageIO.open();
            // inputStream = storageIO.getInputStream();
            pdfFileChannel = storageIO.getReadChannel();
        } catch (Exception ioex) {
            logger.warning("caught Exception trying to open an input stream for " + storageIO.getDataFile().getStorageIdentifier());
            return false;
        }
        File tempFile;
        FileChannel tempFileChannel;
        try {
            tempFile = File.createTempFile("tempFileToRescale", ".tmp");
            tempFileChannel = new FileOutputStream(tempFile).getChannel();
            tempFileChannel.transferFrom(pdfFileChannel, 0, storageIO.getSize());
        } catch (IOException ioex) {
            logger.warning("GenerateImageThumb: failed to save pdf bytes in a temporary file.");
            return false;
        }
        sourcePdfFile = tempFile;
    }
    String imageThumbFileName = generatePDFThumbnailFromFile(sourcePdfFile.getAbsolutePath(), size);
    if (imageThumbFileName == null) {
        return false;
    }
    // generated thumbnail with via the storage driver:
    if (tempFilesRequired) {
        try {
            logger.fine("attempting to save generated pdf thumbnail, as AUX file " + THUMBNAIL_SUFFIX + size);
            storageIO.savePathAsAux(Paths.get(imageThumbFileName), THUMBNAIL_SUFFIX + size);
        } catch (IOException ioex) {
            logger.warning("failed to save generated pdf thumbnail, as AUX file " + THUMBNAIL_SUFFIX + size + "!");
            return false;
        }
    }
    return true;
}
Also used : Path(java.nio.file.Path) ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) DataFile(edu.harvard.iq.dataverse.DataFile) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

ReadableByteChannel (java.nio.channels.ReadableByteChannel)307 ByteBuffer (java.nio.ByteBuffer)111 IOException (java.io.IOException)84 FileOutputStream (java.io.FileOutputStream)62 WritableByteChannel (java.nio.channels.WritableByteChannel)62 Test (org.junit.Test)52 File (java.io.File)50 FileChannel (java.nio.channels.FileChannel)49 FileInputStream (java.io.FileInputStream)43 ByteArrayInputStream (java.io.ByteArrayInputStream)38 InputStream (java.io.InputStream)36 URL (java.net.URL)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 Path (java.nio.file.Path)18 Test (org.testng.annotations.Test)14 FileNotFoundException (java.io.FileNotFoundException)13 ArrayList (java.util.ArrayList)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)11 MalformedURLException (java.net.MalformedURLException)11 Vector (java.util.Vector)11