Search in sources :

Example 61 with ReadableByteChannel

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

the class IngestServiceBean method fixMissingOriginalType.

// This method fixes a datatable object that's missing the format type of
// the ingested original. It will check the saved original file to
// determine the type.
private void fixMissingOriginalType(long fileId) {
    DataFile dataFile = fileService.find(fileId);
    if (dataFile != null && dataFile.isTabularData()) {
        String originalFormat = dataFile.getDataTable().getOriginalFileFormat();
        Long datatableId = dataFile.getDataTable().getId();
        if (StringUtil.isEmpty(originalFormat) || originalFormat.equals(FileUtil.MIME_TYPE_TAB)) {
            // We need to determine the mime type of the saved original
            // and save it in the database.
            // 
            // First, we need access to the file. Note that the code below
            // works with any supported StorageIO driver (although, as of now
            // all the production installations out there are only using filesystem
            // access; but just in case)
            // The FileUtil method that determines the type takes java.io.File
            // as an argument. So for StorageIO drivers that provide local
            // file access, we'll just go directly to the stored file. For
            // swift and similar implementations, we'll read the saved aux
            // channel and save it as a local temp file.
            StorageIO<DataFile> storageIO;
            File savedOriginalFile = null;
            boolean tempFileRequired = false;
            try {
                storageIO = dataFile.getStorageIO();
                storageIO.open();
                if (storageIO.isLocalFile()) {
                    try {
                        savedOriginalFile = storageIO.getAuxObjectAsPath(FileUtil.SAVED_ORIGINAL_FILENAME_EXTENSION).toFile();
                    } catch (IOException ioex) {
                        // do nothing, just make sure savedOriginalFile is still null:
                        savedOriginalFile = null;
                    }
                }
                if (savedOriginalFile == null) {
                    tempFileRequired = true;
                    ReadableByteChannel savedOriginalChannel = (ReadableByteChannel) storageIO.openAuxChannel(FileUtil.SAVED_ORIGINAL_FILENAME_EXTENSION);
                    savedOriginalFile = File.createTempFile("tempSavedOriginal", ".tmp");
                    FileChannel tempSavedOriginalChannel = new FileOutputStream(savedOriginalFile).getChannel();
                    tempSavedOriginalChannel.transferFrom(savedOriginalChannel, 0, storageIO.getAuxObjectSize(FileUtil.SAVED_ORIGINAL_FILENAME_EXTENSION));
                }
            } catch (Exception ex) {
                logger.warning("Exception " + ex.getClass() + " caught trying to open StorageIO channel for the saved original; (datafile id=" + fileId + ", datatable id=" + datatableId + "): " + ex.getMessage());
                savedOriginalFile = null;
            }
            if (savedOriginalFile == null) {
                logger.warning("Could not obtain the saved original file as a java.io.File! (datafile id=" + fileId + ", datatable id=" + datatableId + ")");
                return;
            }
            String fileTypeDetermined = null;
            try {
                fileTypeDetermined = FileUtil.determineFileType(savedOriginalFile, "");
            } catch (IOException ioex) {
                logger.warning("Caught exception trying to determine original file type (datafile id=" + fileId + ", datatable id=" + datatableId + "): " + ioex.getMessage());
            }
            // If we had to create a temp file, delete it now:
            if (tempFileRequired) {
                savedOriginalFile.delete();
            }
            if (fileTypeDetermined == null) {
                logger.warning("Failed to determine preserved original file type. (datafile id=" + fileId + ", datatable id=" + datatableId + ")");
                return;
            }
            // it really means it must be a CSV file.
            if (fileTypeDetermined.startsWith("text/plain")) {
                fileTypeDetermined = FileUtil.MIME_TYPE_CSV;
            }
            // and, finally, if it is still "application/octet-stream", it must be Excel:
            if (FileUtil.MIME_TYPE_UNDETERMINED_DEFAULT.equals(fileTypeDetermined)) {
                fileTypeDetermined = FileUtil.MIME_TYPE_XLSX;
            }
            logger.info("Original file type determined: " + fileTypeDetermined + " (file id=" + fileId + ", datatable id=" + datatableId + "; file path: " + savedOriginalFile.getAbsolutePath() + ")");
            // save permanently in the database:
            dataFile.getDataTable().setOriginalFileFormat(fileTypeDetermined);
            fileService.saveDataTable(dataFile.getDataTable());
        } else {
            logger.info("DataFile id=" + fileId + "; original type already present: " + originalFormat);
        }
    } else {
        logger.warning("DataFile id=" + fileId + ": No such DataFile!");
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) 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) FileExceedsMaxSizeException(edu.harvard.iq.dataverse.datasetutility.FileExceedsMaxSizeException) UnsupportedDataAccessOperationException(edu.harvard.iq.dataverse.dataaccess.UnsupportedDataAccessOperationException) ParseException(java.text.ParseException) JMSException(javax.jms.JMSException) FileNotFoundException(java.io.FileNotFoundException) EJBException(javax.ejb.EJBException) IOException(java.io.IOException)

Example 62 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project Pasta-Launcher by puruscor.

the class Utils method updateFile.

public static void updateFile(String name, String link, String destfile) {
    MainFrame.log("Updating " + name);
    MainFrame.status("Downloading: " + name);
    try {
        URL obj = new URL(link);
        URLConnection conn = obj.openConnection();
        ReadableByteChannel rbc = Channels.newChannel(new URL(link).openStream());
        File file = new File(".//" + destfile);
        FileOutputStream fos = new FileOutputStream(file);
        long position = 0;
        int step = 20480;
        int size = conn.getContentLength();
        MainFrame.setProgress(position, size);
        while (position < size) {
            position += fos.getChannel().transferFrom(rbc, position, step);
            MainFrame.setProgress(position, size);
        }
        MainFrame.setProgress(0, size);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileOutputStream(java.io.FileOutputStream) File(java.io.File) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 63 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project vespa by vespa-engine.

the class ApplicationFileManager method download.

void download(String uri, String relativePath) {
    File file = new File(applicationDir, relativePath);
    FileOutputStream fos = null;
    ReadableByteChannel rbc = null;
    try {
        Files.createDirectories(file.toPath().getParent());
        URL website = new URL(uri);
        rbc = Channels.newChannel(website.openStream());
        fos = new FileOutputStream(file.getAbsolutePath());
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed creating directory " + file.getParent(), e);
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
            if (rbc != null) {
                rbc.close();
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed closing down after downloading " + uri + " to " + file.getAbsolutePath());
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL)

Example 64 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project jbosstools-openshift by jbosstools.

the class OpenShiftCommandLineToolsRequirement method downloadArchive.

private String downloadArchive(String downloadLink) {
    if (StringUtils.isEmpty(downloadLink)) {
        throw new OpenShiftToolsException("Cannot download OpenShift binary. No download known\n");
    }
    String fileName = null;
    try {
        URL downloadUrl = new URL(downloadLink);
        fileName = getFileName(downloadUrl.getPath());
        if (new File(fileName).exists()) {
            LOGGER.info(fileName + " already exists, it will not be downloaded");
            return fileName;
        }
        try (FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            ReadableByteChannel readableByteChannel = Channels.newChannel(downloadUrl.openStream())) {
            LOGGER.info("Downloading OpenShift binary");
            fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
        } catch (IOException ex) {
            // clean up after unsuccessful downloading
            deleteOnPathIfExists(fileName);
            throw new OpenShiftToolsException("Cannot download OpenShift binary\n" + ex.getMessage());
        }
    } catch (MalformedURLException e) {
        throw new OpenShiftToolsException(NLS.bind("Could not download \"{0}\". Invalid url", downloadLink));
    }
    return fileName;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) MalformedURLException(java.net.MalformedURLException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) OpenShiftToolsException(org.jboss.tools.openshift.reddeer.exception.OpenShiftToolsException) URL(java.net.URL)

Example 65 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project tink by google.

the class StreamingTestUtil method testFileEncryptionWithChannel.

// Methods for testFileEncryption.
/**
 * Encrypt some plaintext to a file, then decrypt from the file
 */
private static void testFileEncryptionWithChannel(StreamingAead ags, File tmpFile) throws Exception {
    byte[] aad = TestUtil.hexDecode("aabbccddeeff");
    int plaintextSize = 1 << 18;
    SeekableByteBufferChannel plaintext = new SeekableByteBufferChannel(generatePlaintext(plaintextSize));
    // Encrypt to file
    WritableByteChannel bc = ags.newEncryptingChannel(new FileOutputStream(tmpFile).getChannel(), aad);
    int chunkSize = 1000;
    ByteBuffer chunk = ByteBuffer.allocate(chunkSize);
    int read;
    do {
        chunk.clear();
        read = plaintext.read(chunk);
        if (read > 0) {
            chunk.flip();
            bc.write(chunk);
        }
    } while (read != -1);
    bc.close();
    // Decrypt the whole file and compare to plaintext
    plaintext.rewind();
    ReadableByteChannel ptStream = ags.newDecryptingChannel(new FileInputStream(tmpFile).getChannel(), aad);
    int decryptedSize = 0;
    do {
        ByteBuffer decrypted = ByteBuffer.allocate(512);
        read = ptStream.read(decrypted);
        if (read > 0) {
            ByteBuffer expected = ByteBuffer.allocate(read);
            plaintext.read(expected);
            decrypted.flip();
            TestUtil.assertByteBufferContains(expected.array(), decrypted);
            decryptedSize += read;
        }
    } while (read != -1);
    assertEquals(plaintextSize, decryptedSize);
    // Decrypt file partially using FileChannel and compare to plaintext
    plaintext.rewind();
    SeekableByteChannel ptChannel = ags.newSeekableDecryptingChannel(new FileInputStream(tmpFile).getChannel(), aad);
    SecureRandom random = new SecureRandom();
    for (int samples = 0; samples < 100; samples++) {
        int start = random.nextInt(plaintextSize);
        int length = random.nextInt(plaintextSize / 100 + 1);
        ByteBuffer decrypted = ByteBuffer.allocate(length);
        ptChannel.position(start);
        read = ptChannel.read(decrypted);
        // Hence we also expect that ptChannel returns the maximal number of bytes.
        if (read < length && read + start < plaintextSize) {
            fail("Plaintext size is smaller than expected; read:" + read + " position:" + start + " length:" + length);
        }
        byte[] expected = new byte[read];
        plaintext.position(start);
        plaintext.read(ByteBuffer.wrap(expected));
        decrypted.flip();
        TestUtil.assertByteBufferContains(expected, decrypted);
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) SecureRandom(java.security.SecureRandom) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

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