diff --git a/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java b/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java deleted file mode 100644 index b8af7b26..00000000 --- a/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.archetype.common.util; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Locale; - -import com.ibm.icu.text.CharsetDetector; -import com.ibm.icu.text.CharsetMatch; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * @author rafale - */ -public class FileCharsetDetector { - private static final Logger LOGGER = LoggerFactory.getLogger(FileCharsetDetector.class); - private final String charset; - - public FileCharsetDetector(File detectedFile) throws IOException { - try (FileInputStream fileInputStream = new FileInputStream(detectedFile); - BufferedInputStream is = new BufferedInputStream(fileInputStream)) { - CharsetDetector detector = new CharsetDetector(); - detector.setText(is); - CharsetMatch match = detector.detect(); - - charset = match.getName().toUpperCase(Locale.ENGLISH); - } - } - - public FileCharsetDetector(InputStream detectedStream) throws IOException { - CharsetDetector detector = new CharsetDetector(); - detector.setText(detectedStream); - CharsetMatch match = detector.detect(); - - charset = match.getName().toUpperCase(Locale.ENGLISH); - } - - public String getCharset() { - return charset; - } - - public boolean isFound() { - return true; - } -} diff --git a/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java b/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java index 62c92fc7..0dc39c25 100644 --- a/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java +++ b/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java @@ -22,6 +22,7 @@ import javax.inject.Named; import javax.inject.Singleton; +import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -36,17 +37,19 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Properties; import java.util.Set; +import com.ibm.icu.text.CharsetDetector; +import com.ibm.icu.text.CharsetMatch; import org.apache.commons.collections.CollectionUtils; import org.apache.maven.archetype.ArchetypeCreationRequest; import org.apache.maven.archetype.ArchetypeCreationResult; import org.apache.maven.archetype.common.ArchetypeFilesResolver; import org.apache.maven.archetype.common.Constants; import org.apache.maven.archetype.common.PomManager; -import org.apache.maven.archetype.common.util.FileCharsetDetector; import org.apache.maven.archetype.common.util.ListScanner; import org.apache.maven.archetype.common.util.PathUtils; import org.apache.maven.archetype.metadata.ArchetypeDescriptor; @@ -1260,9 +1263,7 @@ private void processFileSet( File inputFile = new File(basedir, inputFileName); - FileCharsetDetector detector = new FileCharsetDetector(inputFile); - - String fileEncoding = detector.isFound() ? detector.getCharset() : defaultEncoding; + String fileEncoding = getFileCharsetEncoding(inputFile, defaultEncoding); String initialcontent = IOUtil.toString(Files.newInputStream(inputFile.toPath()), fileEncoding); @@ -1632,6 +1633,17 @@ private FileSet getUnpackagedFileSet( return createFileSet(excludes, false, filtered, group, includes, defaultEncoding); } + private String getFileCharsetEncoding(File detectedFile, String defaultEncoding) { + try (InputStream in = new BufferedInputStream(Files.newInputStream(detectedFile.toPath()))) { + CharsetDetector detector = new CharsetDetector(); + detector.setText(in); + CharsetMatch match = detector.detect(); + return match.getName().toUpperCase(Locale.ENGLISH); + } catch (IOException e) { + return defaultEncoding; + } + } + private FileSet getUnpackagedFileSet( final boolean filtered, final Set unpackagedExtensions,