Search in sources :

Example 1 with LocalFile

use of org.apache.commons.vfs2.provider.local.LocalFile in project pentaho-kettle by pentaho.

the class FTPSConnection method downloadFile.

/**
 * this method is used to download a file from a remote host
 *
 * @param file          remote file to download
 * @param localFilename target filename
 * @throws KettleException
 */
public void downloadFile(FTPFile file, String localFilename) throws KettleException {
    try {
        FileObject localFile = KettleVFS.getFileObject(localFilename, nameSpace);
        writeToFile(connection.downloadStream(file), localFile.getContent().getOutputStream(), localFilename);
    } catch (Exception e) {
        throw new KettleException(e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) FileObject(org.apache.commons.vfs2.FileObject) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException)

Example 2 with LocalFile

use of org.apache.commons.vfs2.provider.local.LocalFile in project pentaho-kettle by pentaho.

the class ValueDataUtil method ChecksumAdler32.

public static Long ChecksumAdler32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;
    try {
        file = KettleVFS.getFileObject(dataA.toString());
        CheckedInputStream cis = null;
        // Computer Adler-32 checksum
        cis = new CheckedInputStream(((LocalFile) file).getInputStream(), new Adler32());
        byte[] buf = new byte[128];
        int readSize = 0;
        do {
            readSize = cis.read(buf);
        } while (readSize >= 0);
        checksum = cis.getChecksum().getValue();
    } catch (Exception e) {
    // throw new Exception(e);
    } finally {
        if (file != null) {
            try {
                file.close();
                file = null;
            } catch (Exception e) {
            // Ignore
            }
        }
    }
    return checksum;
}
Also used : LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) FileObject(org.apache.commons.vfs2.FileObject) CheckedInputStream(java.util.zip.CheckedInputStream) Adler32(java.util.zip.Adler32) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 3 with LocalFile

use of org.apache.commons.vfs2.provider.local.LocalFile in project pentaho-kettle by pentaho.

the class CsvInput method openNextFile.

private boolean openNextFile() throws KettleException {
    try {
        // Close the previous file...
        // 
        data.closeFile();
        if (data.filenr >= data.filenames.length) {
            return false;
        }
        // Open the next one...
        // 
        data.fieldsMapping = createFieldMapping(data.filenames[data.filenr], meta);
        FileObject fileObject = KettleVFS.getFileObject(data.filenames[data.filenr], getTransMeta());
        if (!(fileObject instanceof LocalFile)) {
            // 
            throw new KettleException(BaseMessages.getString(PKG, "CsvInput.Log.OnlyLocalFilesAreSupported"));
        }
        if (meta.isLazyConversionActive()) {
            data.binaryFilename = data.filenames[data.filenr].getBytes();
        }
        data.fis = new FileInputStream(KettleVFS.getFilename(fileObject));
        data.fc = data.fis.getChannel();
        data.bb = ByteBuffer.allocateDirect(data.preferredBufferSize);
        // 
        if (data.parallel) {
            if (data.bytesToSkipInFirstFile > 0) {
                data.fc.position(data.bytesToSkipInFirstFile);
                // evaluate whether there is a need to skip a row
                if (needToSkipRow()) {
                    // on windows systems, it's a sequence of '\r' and '\n'. finally we set the start of the buffer to the end buffer position.
                    while (!data.newLineFound()) {
                        data.moveEndBufferPointer();
                    }
                    data.moveEndBufferPointer();
                    if (data.newLineFound()) {
                        data.moveEndBufferPointer();
                    }
                }
                data.setStartBuffer(data.getEndBuffer());
            }
        }
        // Add filename to result filenames ?
        if (meta.isAddResultFile()) {
            ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject, getTransMeta().getName(), toString());
            resultFile.setComment("File was read by a Csv input step");
            addResultFile(resultFile);
        }
        // Move to the next filename
        // 
        data.filenr++;
        // 
        if (meta.isHeaderPresent()) {
            // Standard flat file : skip header
            if (!data.parallel || data.bytesToSkipInFirstFile <= 0) {
                // skip this row.
                readOneRow(true, false);
                logBasic(BaseMessages.getString(PKG, "CsvInput.Log.HeaderRowSkipped", data.filenames[data.filenr - 1]));
            }
        }
        // Reset the row number pointer...
        // 
        data.rowNumber = 1L;
        // Don't skip again in the next file...
        // 
        data.bytesToSkipInFirstFile = -1L;
        return true;
    } catch (KettleException e) {
        throw e;
    } catch (Exception e) {
        throw new KettleException(e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) FileObject(org.apache.commons.vfs2.FileObject) ResultFile(org.pentaho.di.core.ResultFile) FileInputStream(java.io.FileInputStream) KettleException(org.pentaho.di.core.exception.KettleException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) IOException(java.io.IOException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleConversionException(org.pentaho.di.core.exception.KettleConversionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with LocalFile

use of org.apache.commons.vfs2.provider.local.LocalFile in project pentaho-kettle by pentaho.

the class ValueDataUtil method loadFileContentInBinary.

public static Object loadFileContentInBinary(ValueMetaInterface metaA, Object dataA) throws KettleValueException {
    if (dataA == null) {
        return null;
    }
    FileObject file = null;
    FileInputStream fis = null;
    try {
        file = KettleVFS.getFileObject(dataA.toString());
        fis = (FileInputStream) ((LocalFile) file).getInputStream();
        int fileSize = (int) file.getContent().getSize();
        byte[] content = Const.createByteArray(fileSize);
        fis.read(content, 0, fileSize);
        return content;
    } catch (Exception e) {
        throw new KettleValueException(e);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            fis = null;
            if (file != null) {
                file.close();
            }
            file = null;
        } catch (Exception e) {
        // Ignore
        }
    }
}
Also used : LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) FileObject(org.apache.commons.vfs2.FileObject) KettleValueException(org.pentaho.di.core.exception.KettleValueException) FileInputStream(java.io.FileInputStream) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 5 with LocalFile

use of org.apache.commons.vfs2.provider.local.LocalFile in project pentaho-kettle by pentaho.

the class ValueDataUtil method ChecksumCRC32.

public static Long ChecksumCRC32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;
    try {
        file = KettleVFS.getFileObject(dataA.toString());
        CheckedInputStream cis = null;
        // Computer CRC32 checksum
        cis = new CheckedInputStream(((LocalFile) file).getInputStream(), new CRC32());
        byte[] buf = new byte[128];
        int readSize = 0;
        do {
            readSize = cis.read(buf);
        } while (readSize >= 0);
        checksum = cis.getChecksum().getValue();
    } catch (Exception e) {
    // ignore - should likely log the exception
    } finally {
        if (file != null) {
            try {
                file.close();
                file = null;
            } catch (Exception e) {
            // Ignore
            }
        }
    }
    return checksum;
}
Also used : LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) CRC32(java.util.zip.CRC32) FileObject(org.apache.commons.vfs2.FileObject) CheckedInputStream(java.util.zip.CheckedInputStream) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)16 LocalFile (org.apache.commons.vfs2.provider.local.LocalFile)10 File (java.io.File)9 IOException (java.io.IOException)6 KettleException (org.pentaho.di.core.exception.KettleException)6 KettleFileException (org.pentaho.di.core.exception.KettleFileException)4 KettleValueException (org.pentaho.di.core.exception.KettleValueException)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 FileContent (org.apache.commons.vfs2.FileContent)3 ResultFile (org.pentaho.di.core.ResultFile)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 DecimalFormat (java.text.DecimalFormat)2 CheckedInputStream (java.util.zip.CheckedInputStream)2 FileSystemException (org.apache.commons.vfs2.FileSystemException)2 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)2 TableItem (org.eclipse.swt.widgets.TableItem)2 Result (org.pentaho.di.core.Result)2 Database (org.pentaho.di.core.database.Database)2