All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jasonmfehr.combiner.file.MultiFileReader Maven / Gradle / Ivy

package com.github.jasonmfehr.combiner.file;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;

import com.google.common.io.Files;
import com.github.jasonmfehr.combiner.logging.ParameterizedLogger;
import com.github.jasonmfehr.tojs.exception.FileReadException;

@Component(role=MultiFileReader.class)
public class MultiFileReader {

	@Requirement
	private FileValidator fileValidator;
	
	@Requirement
	private ParameterizedLogger logger;
	
	public Map readInputFiles(final Charset charSet, final List inputFiles) {
		final Map filesContents = new HashMap();
		String content;
		File tmpFileObj;
		
		for(final String filePath : inputFiles){
			this.logger.debugWithParams("MultiFileReader reading file {0}", filePath);
			fileValidator.existsAndReadable(filePath);
			tmpFileObj = new File(filePath);
			
			try {
				content = Files.toString(tmpFileObj, charSet);
				this.logger.debugWithParams("contents of file has length of {0}", content.length());
				filesContents.put(tmpFileObj.getAbsolutePath(), content);
			} catch (IOException e) {
				throw new FileReadException(filePath, e);
			}
		}
		
		return filesContents;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy