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);
}
}
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;
}
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);
}
}
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
}
}
}
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;
}
Aggregations