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

com.emc.mongoose.base.item.io.CsvFileItemOutput 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 static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.WRITE;

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

/** Created by kurila on 30.06.15. */
public class CsvFileItemOutput extends CsvItemOutput implements FileOutput {

	protected Path itemsFilePath;

	public CsvFileItemOutput(final Path itemsFilePath, final ItemFactory itemFactory)
					throws IOException {
		super(Files.newOutputStream(itemsFilePath, WRITE, CREATE), itemFactory);
		this.itemsFilePath = itemsFilePath;
	}

	public CsvFileItemOutput(final ItemFactory itemFactory) throws IOException {
		this(Files.createTempFile(null, ".csv"), itemFactory);
		this.itemsFilePath.toFile().deleteOnExit();
	}

	@Override
	public CsvFileItemInput getInput() {
		try {
			return new CsvFileItemInput<>(itemsFilePath, itemFactory);
		} catch (final NoSuchMethodException | IOException e) {
			throwUnchecked(e);
		}
		return null;
	}

	@Override
	public String toString() {
		return "csvFileItemOutput<" + itemsFilePath.getFileName() + ">";
	}

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