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

io.cucumber.messages.BinaryToMessageIterable Maven / Gradle / Ivy

There is a newer version: 26.0.1
Show newest version
package io.cucumber.messages;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

/**
 * Iterates over messages read from a stream. Client code should not depend on this class
 * directly, but rather on a {@code Iterable} object.
 * Tests can then use a {@code new ArrayList} which implements the same interface.
 */
public class BinaryToMessageIterable implements Iterable {
    private final InputStream input;
    private Messages.Envelope next;

    public BinaryToMessageIterable(InputStream input) {
        this.input = input;
    }

    @Override
    public Iterator iterator() {
        return new Iterator() {
            @Override
            public boolean hasNext() {
                try {
                    next = Messages.Envelope.parseDelimitedFrom(input);
                    return next != null;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public Messages.Envelope next() {
                if (next == null) {
                    throw new IllegalStateException("next() should only be called after a call to hasNext() that returns true");
                }
                return next;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy