InputStreamEnumerator.java Source code

Java tutorial

Introduction

Here is the source code for InputStreamEnumerator.java

Source

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

class InputStreamEnumerator implements Enumeration<FileInputStream> {
    private Enumeration<String> files;

    public InputStreamEnumerator(Vector<String> files) {
        this.files = files.elements();
    }

    public boolean hasMoreElements() {
        return files.hasMoreElements();
    }

    public FileInputStream nextElement() {
        try {
            return new FileInputStream(files.nextElement().toString());
        } catch (IOException e) {
            return null;
        }
    }
}

class SequenceInputStreamDemo {
    public static void main(String args[]) throws IOException {

        int c;
        Vector<String> files = new Vector<String>();

        files.addElement("c:\\autoexec.bat");
        files.addElement("c:\\config.sys");
        InputStreamEnumerator e = new InputStreamEnumerator(files);
        InputStream input = new SequenceInputStream(e);

        while ((c = input.read()) != -1) {
            System.out.print((char) c);
        }

        input.close();
    }
}