1 23 package org.archive.io; 24 25 import java.io.IOException ; 26 27 28 33 public class ArraySeekInputStream extends SeekInputStream { 34 35 36 39 private byte[] array; 40 41 42 45 private int offset; 46 47 48 54 public ArraySeekInputStream(byte[] array) { 55 this.array = array; 56 this.offset = 0; 57 } 58 59 60 @Override 61 public int read() { 62 if (offset >= array.length) { 63 return -1; 64 } 65 int r = array[offset] & 0xFF; 66 offset++; 67 return r; 68 } 69 70 71 @Override 72 public int read(byte[] buf, int ofs, int len) { 73 if (offset >= array.length) { 74 return 0; 75 } 76 len = Math.min(len, array.length - offset); 77 System.arraycopy(array, offset, buf, ofs, len); 78 offset += len; 79 return len; 80 } 81 82 83 @Override 84 public int read(byte[] buf) { 85 return read(buf, 0, buf.length); 86 } 87 88 89 92 public long position() { 93 return offset; 94 } 95 96 97 103 public void position(long p) throws IOException { 104 if ((p < 0) || (p > array.length)) { 105 throw new IOException ("Invalid position: " + p); 106 } 107 offset = (int)p; 108 } 109 110 } 111 | Popular Tags |