1 17 package org.apache.commons.collections.primitives.adapters.io; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.util.NoSuchElementException ; 22 23 import org.apache.commons.collections.primitives.ByteIterator; 24 25 31 public class InputStreamByteIterator implements ByteIterator { 32 33 public InputStreamByteIterator(InputStream in) { 34 this.stream = in; 35 } 36 37 public boolean hasNext() { 38 ensureNextAvailable(); 39 return (-1 != next); 40 } 41 42 public byte next() { 43 if(!hasNext()) { 44 throw new NoSuchElementException ("No next element"); 45 } else { 46 nextAvailable = false; 47 return (byte)next; 48 } 49 } 50 51 55 public void remove() throws UnsupportedOperationException { 56 throw new UnsupportedOperationException ("remove() is not supported here"); 57 } 58 59 public static ByteIterator adapt(InputStream in) { 60 return null == in ? null : new InputStreamByteIterator(in); 61 } 62 63 private void ensureNextAvailable() { 64 if(!nextAvailable) { 65 readNext(); 66 } 67 } 68 69 private void readNext() { 70 try { 71 next = stream.read(); 72 nextAvailable = true; 73 } catch(IOException e) { 74 throw new RuntimeException (e.toString()); 77 } 78 } 79 80 private InputStream stream = null; 81 private boolean nextAvailable = false; 82 private int next; 83 } 84 | Popular Tags |