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

net.sf.sevenzipjbinding.simple.impl.SimpleInArchiveImpl Maven / Gradle / Ivy

The newest version!
package net.sf.sevenzipjbinding.simple.impl;

import net.sf.sevenzipjbinding.ISevenZipInArchive;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;

/**
 * Standard implementation of {@link ISimpleInArchive}, simplified 7-Zip-JBinding interface.
 * 
 * @author Boris Brodski
 * @version 4.65-1
 */
public class SimpleInArchiveImpl implements ISimpleInArchive {
	private final ISevenZipInArchive sevenZipInArchive;
	private boolean wasClosed = false;

	/**
	 * Constructing an instance of {@link SimpleInArchiveImpl} from a instance of {@link ISevenZipInArchive}.
	 * 
	 * @param sevenZipInArchive
	 *            a base instance of {@link ISevenZipInArchive}
	 */
	public SimpleInArchiveImpl(ISevenZipInArchive sevenZipInArchive) {
		this.sevenZipInArchive = sevenZipInArchive;
	}

	/**
	 * {@inheritDoc}
	 */

	public void close() throws SevenZipException {
		sevenZipInArchive.close();
		wasClosed = true;
	}

	/**
	 * {@inheritDoc}
	 */

	public ISimpleInArchiveItem[] getArchiveItems() throws SevenZipException {
		ISimpleInArchiveItem[] result = new ISimpleInArchiveItem[getNumberOfItems()];
		for (int i = 0; i < result.length; i++) {
			result[i] = new SimpleInArchiveItemImpl(this, i);
		}
		return result;
	}

	/**
	 * {@inheritDoc}
	 */

	public int getNumberOfItems() throws SevenZipException {
		return testAndGetSafeSevenZipInArchive().getNumberOfItems();
	}

	/**
	 * Tests, if 7-Zip In archive interface can be accessed safely.
	 * 
	 * @return 7-Zip In archive interface
	 * @throws SevenZipException
	 *             archive can't be accessed any more
	 */
	public ISevenZipInArchive testAndGetSafeSevenZipInArchive() throws SevenZipException {
		if (wasClosed) {
			throw new SevenZipException("Archive was closed");
		}
		return sevenZipInArchive;
	}

	/**
	 * ${@inheritDoc}
	 */
	public ISimpleInArchiveItem getArchiveItem(int index) throws SevenZipException {
		if (index < 0 || index >= sevenZipInArchive.getNumberOfItems()) {
			throw new SevenZipException("Index " + index + " is out of range. Number of items in archive: "
					+ sevenZipInArchive.getNumberOfItems());
		}
		return new SimpleInArchiveItemImpl(this, index);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy