<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import java.io.InputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class IterableStream implements Iterable&lt;Byte&gt; {
	private InputStream stream;

	public IterableStream(InputStream s) {
		stream = s;
	}
	public Iterator&lt;Byte&gt; iterator() {
		return new Iterator&lt;Byte&gt;() {
		// XXX implement this?
			byte nxt;      // the next byte on the stream (if ready)
			boolean eos;   // whether the stream is at the end (if ready)
			boolean ready; // whether nxt and eos contains the next byte

			private void advance() {
				int b;
				try {
					b = stream.read();
				} catch (java.io.IOException e) {
					eos = true;
					ready = true;
					return;
				}
				ready = true;
				if (b == -1) {
					eos = true;
					return;
				}
				if (b &lt;= 127) nxt = (byte) b;
				else nxt = (byte) (b - 256);
			}
			public boolean hasNext() {
				if (!ready) advance();
				return !eos;
			}
			public Byte next() {
				if (!ready) advance();
				if (eos) throw new NoSuchElementException();
				ready = false;
				return nxt;
			}
			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}
}
</pre></body></html>