Skip to content

Commit 799c844

Browse files
committed
Clean up FeatureExport.copyFileToDirectory()
Use Files.copy() instead of open-coding the copy operation. Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
1 parent d881043 commit 799c844

1 file changed

Lines changed: 12 additions & 22 deletions

File tree

features/command/src/main/java/org/apache/karaf/features/command/FeatureExport.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
package org.apache.karaf.features.command;
1818

1919
import java.io.File;
20-
import java.io.FileInputStream;
2120
import java.io.FileNotFoundException;
22-
import java.io.FileOutputStream;
2321
import java.io.IOException;
22+
import java.nio.file.Files;
2423

2524
import org.apache.karaf.features.BundleInfo;
2625
import org.apache.karaf.features.Dependency;
@@ -42,7 +41,6 @@
4241
* file system. This is useful for several use cases, such as in the event you
4342
* need to deploy the functionality offered by a particular feature to an OBR
4443
* repository.
45-
*
4644
*/
4745
@Service
4846
@Command(scope = "feature", name = "export-bundles", description = "Export all of the bundles that make up a specified feature to a directory on the file system.")
@@ -111,7 +109,7 @@ public void doExecute(final FeaturesService featuresService) throws Exception {
111109

112110
/**
113111
* Prepare the target destination directory.
114-
*
112+
*
115113
* @param destination
116114
* Where we'll save the bundles
117115
* @return true if it is valid, false otherwise
@@ -122,7 +120,7 @@ private boolean prepareDestination(final File destination) {
122120

123121
/**
124122
* Save the feature bundles, and all of its transitive dependency bundles.
125-
*
123+
*
126124
* @param dest
127125
* The target directory where we'll save the feature bundles
128126
* @param feature
@@ -158,7 +156,7 @@ private void saveBundles(final File dest, final Feature feature, final FeaturesS
158156

159157
/**
160158
* Simple method to copy a file to a target destination directory.
161-
*
159+
*
162160
* @param file
163161
* The file to copy
164162
* @param directory
@@ -172,23 +170,15 @@ private void saveBundles(final File dest, final Feature feature, final FeaturesS
172170
private static boolean copyFileToDirectory(final File file, final File directory) throws IOException {
173171
if (!directory.isDirectory()) {
174172
throw new IOException("Can't copy to non-directory specified: " + directory.getAbsolutePath());
175-
} else {
176-
boolean copied = false;
177-
final File newFile = new File(directory.getAbsolutePath() + "/" + file.getName());
178-
if (!newFile.isFile()) {
179-
try (final FileInputStream fis = new FileInputStream(file)) {
180-
try (final FileOutputStream fos = new FileOutputStream(newFile)) {
181-
byte[] buffer = new byte[1024 * 8];
182-
int read = -1;
183-
while ((read = fis.read(buffer)) >= 0) {
184-
fos.write(buffer, 0, read);
185-
}
186-
}
187-
}
188-
copied = true;
189-
}
190-
return copied;
191173
}
174+
175+
final var newFile = directory.toPath().resolve(file.getName());
176+
if (Files.isRegularFile(newFile)) {
177+
return false;
178+
}
179+
180+
Files.copy(file.toPath(), newFile);
181+
return true;
192182
}
193183

194184
}

0 commit comments

Comments
 (0)