/*
 * Copyright 2015 Namihiko Matsumura (https://github.com/n-i-e/)
 *
 * Licensed 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 com.github.n_i_e.dirtreedb;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public abstract class AbstractArchiveLister implements IArchiveLister {
	protected PathEntry basepath;
	protected PathEntry next_entry;
	
	AbstractArchiveLister(PathEntry basepath) {
		this.basepath = basepath;
		next_entry = null;
	}

	protected abstract void getNext(boolean csum) throws IOException;
	
	public boolean hasNext(boolean csum) throws IOException {
		getNext(csum);
		if (next_entry == null) {
			return false;
		} else {
			return true;
		}
	}

	public PathEntry next(boolean csum) throws IOException {
		getNext(csum);

		PathEntry result = next_entry;
		next_entry = null;
		return result;
	}

	public boolean hasNext() throws IOException {
		return hasNext(false);
	}

	public PathEntry next() throws IOException {
		return next(false);
	}

	public InputStream getInputStream(PathEntry entry) throws IOException {
		while (hasNext(false)) {
			PathEntry p = next(false);
			if (p.getPath().equals(entry.getPath())) {
				return new BufferedInputStream(getInputStream());
				// BufferedInputStream here, because I want markSupported() stream at ApacheCompressArchiveLister constructor.
			}
		}
		throw new FileNotFoundException(String.format("!! Archive file not found for path %s at basepath %s",
				entry.getPath(), basepath.getPath()));
	}

	public void close() throws IOException {
		getInputStream().close();
	}

}

@@ -813,16 +813,66 @@
 Stream;%0D
+%0Aimport java.util.HashSet;%0D%0Aimport java.util.Set;%0D
 %0A%0D%0Apubli
@@ -1179,17 +1179,508 @@
 ption;%0D%0A
-%09
+%0D%0A%09private Set%3CString%3E pathnameUniquenessChecker = new HashSet%3CString%3E ();%0D%0A%09private void getNextWithIntegrityCheck(boolean csum) throws IOException %7B%0D%0A%09%09if (next_entry != null) %7B%0D%0A%09%09%09getNext(csum);%0D%0A%09%09%7D else %7B%0D%0A%09%09%09getNext(csum);%0D%0A%09%09%09if (next_entry != null) %7B%0D%0A%09%09%09%09Assertion.assertIOException(!pathnameUniquenessChecker.contains(next_entry.getPath()),%0D%0A%09%09%09%09%09%09%22!! duplicate pathname: %22 + next_entry.getPath()%0D%0A%09%09%09%09%09%09);%0D%0A%09%09%09%09pathnameUniquenessChecker.add(next_entry.getPath());%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A
 %0D%0A%09publi
@@ -1734,32 +1734,50 @@
 ion %7B%0D%0A%09%09getNext
+WithIntegrityCheck
 (csum);%0D%0A%09%09if (n
@@ -1907,32 +1907,32 @@
  IOException %7B%0D%0A
-
 %09%09getNext(csum);
@@ -1916,32 +1916,50 @@
 ion %7B%0D%0A%09%09getNext
+WithIntegrityCheck
 (csum);%0D%0A%0D%0A%09%09Pat

improve assertion: check duplicate file name in one archive