Compare commits

...

6 Commits

Author SHA1 Message Date
Christophe Le Saec
451f25526a TDI-42230 remove SNAPSHOT 2019-07-04 09:58:46 +02:00
Christophe Le Saec
b4c77184f3 fix(TDI-42230) : custom lib on copy with version 2019-07-03 10:28:44 +02:00
Christophe Le Saec
4efe837b40 fix(TDI-42230) - put filecopy librarie directly in studio 2019-07-02 10:17:28 +02:00
Christophe Le Saec
2164c692d2 fix(TDI-42230) : use filecopy from nexus in studio 2019-06-28 16:56:38 +02:00
Christophe Le Saec
c1de5f5fc0 fix(TDI-42230) filecopy.jar removed from git 2019-06-28 09:53:57 +02:00
Christophe Le Saec
bd31878bcd fix(TDI-42230) : utilisation de la classe standard java.nio.Files 2019-06-27 17:17:23 +02:00
12 changed files with 265 additions and 281 deletions

View File

@@ -2,10 +2,9 @@
<project name="org.talend.designer.components.libs" default="buildall" basedir=".">
<target name="buildall">
<ant antfile="filecopy/build.xml" target="process" inheritall="no" />
<ant antfile="talend_file_enhanced_20070724/build.xml" target="process" inheritall="no" />
<ant antfile="sugarCRMManagement/build.xml" target="process" inheritall="no" />
<ant antfile="TalendSAX/build.xml" target="process" inheritall="no" />
</target>
</project>
</project>

View File

@@ -0,0 +1,3 @@
.classpath
.project
target/

View File

@@ -1,88 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="org.talend.designer.components.libs" default="process" basedir=".">
<property name="component.plugin.home" value="../../../org.talend.designer.components.localprovider/components" />
<!-- #################################################### -->
<!-- modification 1: config -->
<property name="jar.name" value="filecopy.jar" />
<property name="component.name" value="tFileCopy" />
<property name="author.name" value="wyang" />
<!-- modification 2: compile classpath -->
<path id="compile.classpath">
</path>
<!-- #################################################### -->
<!-- sourcecode and final jar path -->
<property name="source.home" value="." />
<property name="jar.home" value="${component.plugin.home}/${component.name}/${jar.name}" />
<!-- temp dir for clasee files -->
<property name="build.dir" value="../../build" />
<!-- compile option -->
<property name="compile.debug" value="true" />
<property name="compile.deprecation" value="false" />
<property name="compile.optimize" value="true" />
<target name="process" description="prepare a temp dir">
<antcall target="prepare" />
<antcall target="compile" />
<antcall target="clean" />
</target>
<target name="prepare" description="prepare a temp dir">
<delete dir="${build.dir}" />
<mkdir dir="${build.dir}" />
<mkdir dir="${build.dir}/classes" />
</target>
<target name="compile" description="Compile Java sources">
<!-- compile -->
<javac srcdir="${source.home}" destdir="${build.dir}/classes" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}">
<classpath refid="compile.classpath" />
</javac>
<!-- include source code -->
<copy todir="${build.dir}/classes">
<fileset dir="${source.home}">
<exclude name="build.xml" />
</fileset>
</copy>
<!-- make jar -->
<tstamp>
<format property="date" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<jar destfile="${build.dir}/${jar.name}" basedir="${build.dir}/classes">
<manifest>
<!-- who -->
<attribute name="Built-By" value="${author.name}" />
<!-- when -->
<attribute name="Built-Date" value="${date}"/>
<!-- JDK version -->
<attribute name="Created-By" value="${java.version} (${java.vendor})" />
<!-- Information about the program itself -->
<attribute name="Implementation-Vendor" value="Talend SA" />
<attribute name="Implementation-Title" value="${jar.name}" />
<attribute name="Implementation-Version" value="1.0" />
</manifest>
</jar>
<!-- move jar -->
<move file="${build.dir}/${jar.name}" tofile="${jar.home}" />
</target>
<target name="clean" description="clean the temp dir">
<delete dir="${build.dir}" />
<mkdir dir="${build.dir}" />
</target>
</project>

View File

@@ -1,190 +0,0 @@
// ============================================================================
//
// Copyright (C) 2006-2019 Talend Inc. - www.talend.com
//
// This source code is available under agreement available at
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
//
// You should have received a copy of the agreement
// along with this program; if not, write to Talend SA
// 9 rue Pages 92150 Suresnes, France
//
// ============================================================================
package org.talend;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* DOC Administrator class global comment. Detailled comment
*/
public class FileCopy {
private final static long L_SIZE = 100 * 1024 * 1024; // 100M
private final static long M_SIZE = 10 * 1024 * 1024; // 10M
private final static long S_SIZE = 0; // 0M
public static void copyFile(String srcFileName, String desFileName, boolean delSrc) throws Exception {
FileInputStream srcInputStream = null;
try{
srcInputStream = new FileInputStream(srcFileName);
long lastModified = new File(srcFileName).lastModified();
int available = srcInputStream.available();
if (available > L_SIZE) {// X > 100M
copyFileL(srcFileName, srcInputStream, desFileName, delSrc);
} else if (available > M_SIZE) {// 10M < X <100M
copyFileM(srcFileName, srcInputStream, desFileName, delSrc);
} else { // X < 10M
copyFileS(srcFileName, srcInputStream, desFileName, delSrc);
}
// keep modification_time
new File(desFileName).setLastModified(lastModified);
}finally{
if(srcInputStream!=null){
srcInputStream.close();
}
}
}
private static void copyFileS(String srcFileName, FileInputStream srcInputStream, String desFileName, boolean delSrc)
throws IOException {
File source = new File(srcFileName);
File dest = new File(desFileName);
FileInputStream in = null;
FileOutputStream out = null;
try {
in = srcInputStream;
out = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
if (delSrc) {
source.delete();
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
private static void copyFileM(String srcFileName, FileInputStream srcInputStream, String desFileName, boolean delSrc)
throws IOException {
File source = new File(srcFileName);
File dest = new File(desFileName);
FileChannel in = null;
FileChannel out = null;
try {
in = srcInputStream.getChannel();
out = new FileOutputStream(dest).getChannel();
int maxCount = (32 * 1024 * 1024) - (28 * 1024);
long size = in.size();
long position = 0;
while (position < size) {
position += in.transferTo(position, maxCount, out);
}
in.close();
out.close();
if (delSrc) {
source.delete();
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
private static void copyFileL(String srcFileName, FileInputStream srcInputStream, String desFileName, boolean delSrc)
throws Exception {
File source = new File(srcFileName);
File dest = new File(desFileName);
FileChannel in = null, out = null;
try {
in = srcInputStream.getChannel();
out = new FileOutputStream(dest).getChannel();
long size = in.size();
long position = 0;
final long MAP_SIZE = 33525760;
MappedByteBuffer buf = null;
while (true) {
if (position + MAP_SIZE >= size) {
buf = in.map(FileChannel.MapMode.READ_ONLY, position, size - position);
out.write(buf);
//For But TDI-26493, here must clean first, or it can't delete
clean(buf);
break;
} else {
buf = in.map(FileChannel.MapMode.READ_ONLY, position, MAP_SIZE);
out.write(buf);
// here must clean first, or it can't delete
clean(buf);
position += MAP_SIZE;
}
}
in.close();
out.close();
if (delSrc) {
source.delete();
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
@SuppressWarnings("unchecked")
private static void clean(final Object buffer) throws Exception {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
Method getCleanerMethod = buffer.getClass().getMethod("cleaner", new Class[0]);
getCleanerMethod.setAccessible(true);
sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(buffer, new Object[0]);
cleaner.clean();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
});
}
}

View File

@@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.talend.libraries</groupId>
<artifactId>filecopy</artifactId>
<version>2.0.0</version>
<packaging>jar</packaging>
<name>talend-copy</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<talend.nexus.url>https://artifacts-oss.talend.com</talend.nexus.url>
<java.source.version>1.8</java.source.version>
<junit5.version>5.4.2</junit5.version>
</properties>
<distributionManagement>
<snapshotRepository>
<id>talend_nexus_deployment</id>
<url>${talend.nexus.url}/nexus/content/repositories/TalendOpenSourceSnapshot/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</snapshotRepository>
<repository>
<id>talend_nexus_deployment</id>
<url>${talend.nexus.url}/nexus/content/repositories/TalendOpenSourceRelease/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.source.version}</source>
<target>${java.source.version}</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,49 @@
// ============================================================================
//
// Copyright (C) 2006-2019 Talend Inc. - www.talend.com
//
// This source code is available under agreement available at
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
//
// You should have received a copy of the agreement
// along with this program; if not, write to Talend SA
// 9 rue Pages 92150 Suresnes, France
//
// ============================================================================
package org.talend;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
/**
* DOC Administrator class global comment. Detailled comment
*/
public class FileCopy {
/** Private constructor, only static methods */
private FileCopy() {
}
/**
* Copy files.
*
* @param srcFileName : file name for source file.
* @param desFileName : file name for destination file.
* @param delSrc : true if delete source.
* @throws IOException : if IO pb.
*/
public static void copyFile(String srcFileName, String desFileName, boolean delSrc) throws IOException {
final File source = new File(srcFileName);
final File destination = new File(desFileName);
if (delSrc) {
// move : more efficient if in same FS and mustr delete existing file.
Files.move(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
} else {
Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
}

View File

@@ -0,0 +1,128 @@
package org.talend;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* Test for FileCopy.class with diffents file size.
*
* @author clesaec
*
*/
class FileCopyTest {
@Test
void testCopyFile() throws Exception {
final URL repCopy = Thread.currentThread().getContextClassLoader().getResource("copy");
File small = this.buildFile("small.txt", 10L * 1024L);
small.deleteOnExit();
File smallCopy = new File(repCopy.getPath(), "small.txt");
smallCopy.deleteOnExit();
FileCopy.copyFile(small.getPath(), smallCopy.getPath(), false);
Assertions.assertTrue(smallCopy.exists(), "small file fail to copy (not created)");
Assertions.assertTrue(small.exists(), "small file : original file deleted");
Assertions.assertEquals(smallCopy.length(), small.length(), "Size error");
File medium = this.buildFile("medium.txt", 30L * 1024L * 1024L);
medium.deleteOnExit();
File mediumCopy = new File(repCopy.getPath(), "medium.txt");
mediumCopy.deleteOnExit();
FileCopy.copyFile(medium.getPath(), mediumCopy.getPath(), false);
Assertions.assertTrue(mediumCopy.exists(), "medium file fail to copy (not created)");
Assertions.assertTrue(medium.exists(), "medium file : original file deleted");
Assertions.assertEquals(mediumCopy.length(), medium.length(), "Size error");
File large = this.buildFile("large.txt", 110L * 1024L * 1024L);
large.deleteOnExit();
long startTime = System.nanoTime();
File largeCopy = new File(repCopy.getPath(), "large.txt");
long duration = System.nanoTime() - startTime;
System.out.println("Duration for 110 Mo file : " + TimeUnit.NANOSECONDS.toMicros(duration) + " µs");
largeCopy.deleteOnExit();
FileCopy.copyFile(large.getPath(), largeCopy.getPath(), false);
Assertions.assertTrue(largeCopy.exists(), "small file fail to copy (not created)");
Assertions.assertTrue(large.exists(), "small file : original file deleted");
Assertions.assertEquals(largeCopy.length(), large.length(), "Size error");
}
@Test
void testCopyMv() throws Exception {
final URL repCopy = Thread.currentThread().getContextClassLoader().getResource("copy");
File file = this.buildFile("fileToMove.txt", 10L * 1024L);
file.deleteOnExit();
File copy = new File(repCopy.getPath(), "fileToMove.txt");
long referenceSize = file.length();
if (copy.exists()) {
copy.delete();
}
copy.deleteOnExit();
FileCopy.copyFile(file.getPath(), copy.getPath(), true);
Assertions.assertFalse(file.exists(), "file not delete");
Assertions.assertTrue(copy.exists(), "small file : original file deleted");
Assertions.assertEquals(referenceSize, copy.length(), "Size error");
}
@Test
void testCopyWithDelete() throws Exception {
final URL repCopy = Thread.currentThread().getContextClassLoader().getResource("copy");
File file = this.buildFile("fileToDelete.txt", 10L * 1024L);
file.deleteOnExit();
File copy = new File(repCopy.getPath(), "fileToDelete.txt");
long referenceSize = file.length();
if (!copy.exists()) {
copy.createNewFile();
}
copy.deleteOnExit();
FileCopy.copyFile(file.getPath(), copy.getPath(), true);
Assertions.assertFalse(file.exists(), "file not delete");
Assertions.assertTrue(copy.exists(), "small file : original file deleted");
Assertions.assertEquals(referenceSize, copy.length(), "Size error");
}
/**
* Generate a new file for testing.
*
* @param name : name of file.
* @param minSize : minimum size.
* @return the new file.
* @throws IOException : on IO pb.
*/
private File buildFile(String name, long minSize) throws IOException {
final URL repGenerated = Thread.currentThread().getContextClassLoader().getResource("generated");
final File generatedFile = new File(repGenerated.getPath(), name);
if (generatedFile.exists()) {
generatedFile.delete();
}
final String data = "{ data to put in generated file for it have the desired sized }" + System.lineSeparator();
long nbeIteration = (minSize / data.length()) + 1;
try (BufferedWriter writer = Files.newBufferedWriter(generatedFile.toPath(), StandardOpenOption.CREATE)) {
for (long i = 0; i < nbeIteration; i++) {
writer.append(data);
}
}
return generatedFile;
}
}

View File

@@ -0,0 +1 @@
Just here to not have an empty directory.

View File

@@ -0,0 +1 @@
Just here to not have an empty directory.

View File

@@ -143,7 +143,10 @@
<CODEGENERATION>
<IMPORTS>
<IMPORT NAME="filecopy" MODULE="filecopy.jar" MVN="mvn:org.talend.libraries/filecopy/6.0.0" REQUIRED="true" />
<IMPORT NAME="filecopy" MODULE="filecopy-2.0.0.jar"
MVN="mvn:org.talend.libraries/filecopy/2.0.0"
UrlPath="platform:/plugin/org.talend.libraries.custom/lib/filecopy-2.0.0.jar"
REQUIRED="true" />
</IMPORTS>
</CODEGENERATION>

View File

@@ -58,6 +58,11 @@
<artifactId>talendzip</artifactId>
<version>1.0-20190527</version>
</artifactItem>
<artifactItem>
<groupId>org.talend.libraries</groupId>
<artifactId>filecopy</artifactId>
<version>2.0.0</version>
</artifactItem>
</artifactItems>
</configuration>
</execution>