Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 46 additions & 78 deletions core/src/main/java/org/apache/hop/core/row/ValueDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.commons.codec.language.RefinedSoundex;
import org.apache.commons.codec.language.Soundex;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -335,35 +334,29 @@ public static String createChecksum(Object dataA, String type, boolean failIfNoF
}

String md5Hash = null;
FileObject file = null;
InputStream in = null;
try {
file = HopVfs.getFileObject(dataA.toString());
try (FileObject file = HopVfs.getFileObject(dataA.toString())) {
// verify file exists before opening the stream
throwsErrorOnFileNotFound(file);
in = HopVfs.getInputStream(file);
int bytes = in.available();
byte[] buffer = new byte[bytes];
in.read(buffer);

StringBuffer md5HashBuff = new StringBuffer(32);
byte[] b = MessageDigest.getInstance(type).digest(buffer);
int len = b.length;
for (byte value : b) {
md5HashBuff.append(String.format("%02x", value));
}

md5Hash = md5HashBuff.toString();
try (InputStream in = HopVfs.getInputStream(file)) {
int bytes = in.available();
byte[] buffer = new byte[bytes];
in.read(buffer);

StringBuffer md5HashBuff = new StringBuffer(32);
byte[] b = MessageDigest.getInstance(type).digest(buffer);
for (byte value : b) {
md5HashBuff.append(String.format("%02x", value));
}

md5Hash = md5HashBuff.toString();
}
} catch (HopFileNotFoundException e) {
if (failIfNoFile) {
throw e;
}
log.debug(e.getMessage());
} catch (Exception e) {
log.debug(e.getMessage());
} finally {
IOUtils.closeQuietly(file);
IOUtils.closeQuietly(in);
}
return md5Hash;
}
Expand All @@ -382,32 +375,27 @@ public static Long checksumCRC32(Object dataA, boolean failIfNoFile)
return checksum;
}

FileObject file = null;
CheckedInputStream cis = null;
try {
file = HopVfs.getFileObject(dataA.toString());
try (FileObject file = HopVfs.getFileObject(dataA.toString())) {
throwsErrorOnFileNotFound(file);

// Computer CRC32 checksum
cis = new CheckedInputStream(HopVfs.getInputStream(file), new CRC32());
byte[] buf = new byte[128];
int readSize = 0;
do {
readSize = cis.read(buf);
} while (readSize >= 0);

checksum = cis.getChecksum().getValue();
// Compute CRC32 checksum
try (CheckedInputStream cis =
new CheckedInputStream(HopVfs.getInputStream(file), new CRC32())) {
byte[] buf = new byte[128];
int readSize = 0;
do {
readSize = cis.read(buf);
} while (readSize >= 0);

checksum = cis.getChecksum().getValue();
}
} catch (HopFileNotFoundException e) {
if (failIfNoFile) {
throw e;
}
log.debug(e.getMessage());
} catch (Exception e) {
log.debug(e.getMessage());
} finally {
IOUtils.closeQuietly(file);
IOUtils.closeQuietly(cis);
}
return checksum;
}
Expand All @@ -426,32 +414,26 @@ public static Long checksumAdler32(Object dataA, boolean failIfNoFile)
return checksum;
}

FileObject file = null;
CheckedInputStream cis = null;
try {
file = HopVfs.getFileObject(dataA.toString());
try (FileObject file = HopVfs.getFileObject(dataA.toString())) {
throwsErrorOnFileNotFound(file);

// Computer Adler-32 checksum
cis = new CheckedInputStream(HopVfs.getInputStream(file), new Adler32());

byte[] buf = new byte[128];
int readSize = 0;
do {
readSize = cis.read(buf);
} while (readSize >= 0);
checksum = cis.getChecksum().getValue();

// Compute Adler-32 checksum
try (CheckedInputStream cis =
new CheckedInputStream(HopVfs.getInputStream(file), new Adler32())) {
byte[] buf = new byte[128];
int readSize = 0;
do {
readSize = cis.read(buf);
} while (readSize >= 0);
checksum = cis.getChecksum().getValue();
}
} catch (HopFileNotFoundException e) {
if (failIfNoFile) {
throw e;
}
log.debug(e.getMessage());
} catch (Exception e) {
log.debug(e.getMessage());
} finally {
IOUtils.closeQuietly(file);
IOUtils.closeQuietly(cis);
}
return checksum;
}
Expand Down Expand Up @@ -552,12 +534,12 @@ public static Object sum(IValueMeta metaA, Object dataA, IValueMeta metaB, Objec
if (dataA == null && dataB == null) {
return null;
}
if (dataA == null && dataB != null) {
if (dataA == null) {
Object value = metaA.convertData(metaB, dataB);
metaA.setStorageType(IValueMeta.STORAGE_TYPE_NORMAL);
return value;
}
if (dataA != null && dataB == null) {
if (dataB == null) {
return dataA;
}

Expand All @@ -578,26 +560,20 @@ public static byte[] loadFileContentInBinary(Object dataA, boolean failIfNoFile)
}

byte[] content = null;
FileObject file = null;
InputStream is = null;

try {
file = HopVfs.getFileObject(dataA.toString());
try (FileObject file = HopVfs.getFileObject(dataA.toString())) {
throwsErrorOnFileNotFound(file);
is = HopVfs.getInputStream(file);
int fileSize = (int) file.getContent().getSize();
content = new byte[fileSize];
is.read(content, 0, fileSize);
try (InputStream is = HopVfs.getInputStream(file)) {
int fileSize = (int) file.getContent().getSize();
content = new byte[fileSize];
is.read(content, 0, fileSize);
}
} catch (HopFileNotFoundException e) {
if (failIfNoFile) {
throw e;
}
log.debug(e.getMessage());
} catch (Exception e) {
throw new HopValueException(e);
} finally {
IOUtils.closeQuietly(file);
IOUtils.closeQuietly(is);
}
return content;
}
Expand Down Expand Up @@ -1830,9 +1806,7 @@ public static boolean isXmlFileWellFormed(Object dataA, boolean failIfNoFile)
}

String filename = dataA.toString();
FileObject file = null;
try {
file = HopVfs.getFileObject(filename);
try (FileObject file = HopVfs.getFileObject(filename)) {
throwsErrorOnFileNotFound(file);
return XmlCheck.isXmlFileWellFormed(file);
} catch (HopFileNotFoundException e) {
Expand All @@ -1842,8 +1816,6 @@ public static boolean isXmlFileWellFormed(Object dataA, boolean failIfNoFile)
log.debug(e.getMessage());
} catch (Exception e) {
log.debug(e.getMessage());
} finally {
IOUtils.closeQuietly(file);
}
return false;
}
Expand Down Expand Up @@ -1884,9 +1856,7 @@ public static String getFileEncoding(IValueMeta metaA, Object dataA, boolean fai
}

String encoding = null;
FileObject file = null;
try {
file = HopVfs.getFileObject(metaA.getString(dataA));
try (FileObject file = HopVfs.getFileObject(metaA.getString(dataA))) {
throwsErrorOnFileNotFound(file);
encoding = CharsetToolkit.guessEncodingName(file);
} catch (HopFileNotFoundException e) {
Expand All @@ -1896,8 +1866,6 @@ public static String getFileEncoding(IValueMeta metaA, Object dataA, boolean fai
log.debug(e.getMessage());
} catch (Exception e) {
throw new HopValueException(e);
} finally {
IOUtils.closeQuietly(file);
}
return encoding;
}
Expand Down
Loading