Answer the question
In order to leave comments, you need to log in
How to implement an InputStream adapter to an Iterator?
They gave me the task to implement an InputStream adapter to an Iterator, but I can't figure out how to do it at all.
There is a blank: (You need to make a constructor and methods hasNext(), next())
public class ISToIteratorAdapter implements Iterator<Byte> {
public ISToIteratorAdapter(InputStream is) {
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
Answer the question
In order to leave comments, you need to log in
Probably something like this?
public class ISToIteratorAdapter implements Iterator<Byte> {
private final InputStream is;
public ISToIteratorAdapter(InputStream is) {
this.is = is;
}
@Override
public boolean hasNext() {
try {
return is.available() > 0;
} catch (IOException e) {
return false;
}
}
@Override
public Byte next() {
try {
return Integer.valueOf(is.read()).byteValue();
} catch (IOException e) {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question