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

com.emc.mongoose.base.item.io.CsvFileItemInput Maven / Gradle / Ivy

There is a newer version: 4.3.10
Show newest version
package com.emc.mongoose.base.item.io;

import static com.github.akurilov.commons.lang.Exceptions.throwUnchecked;

import com.emc.mongoose.base.item.DataItemFactory;
import com.emc.mongoose.base.item.Item;
import com.emc.mongoose.base.item.ItemFactory;
import com.github.akurilov.commons.io.file.FileInput;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

/** Created by kurila on 30.06.15. */
public class CsvFileItemInput extends CsvItemInput implements FileInput {
	//
	protected final Path itemsFilePath;

	/**
	* @param itemsFilePath the input stream to get the data item records from
	* @param itemFactory the concrete item factory
	* @throws IOException
	* @throws NoSuchMethodException
	*/
	public CsvFileItemInput(final Path itemsFilePath, final ItemFactory itemFactory)
					throws IOException, NoSuchMethodException {
		super(Files.newBufferedReader(itemsFilePath, StandardCharsets.UTF_8), itemFactory);
		this.itemsFilePath = itemsFilePath;
	}

	//
	@Override
	public String toString() {
		return (itemFactory instanceof DataItemFactory ? "Data" : "")
						+ "ItemsFromFile("
						+ itemsFilePath
						+ ")";
	}

	//
	@Override
	public final Path filePath() {
		return itemsFilePath;
	}

	//
	@Override
	public void reset() {
		try {
			if (itemsSrc != null) {
				itemsSrc.close();
			}
			setItemsSrc(Files.newBufferedReader(itemsFilePath, StandardCharsets.UTF_8));
		} catch (final IOException e) {
			throwUnchecked(e);
		}
	}
}