imported brians code
[nestedvm.git] / src / org / xwt / mips / util / SeekableByteArray.java
1 package org.xwt.mips.util;
2
3 import java.io.IOException;
4
5 public class SeekableByteArray implements SeekableData {
6     protected byte[] data;
7     protected int pos;
8     private final boolean writable;
9     
10     public SeekableByteArray(byte[] data, boolean writable) {
11         this.data = data;
12         this.pos = 0;
13         this.writable = writable;
14     }
15     
16     public int read(byte[] buf, int off, int len) {
17         len = Math.min(len,data.length-pos);
18         if(len <= 0) return -1;
19         System.arraycopy(data,pos,buf,off,len);
20         pos += len;
21         return len;
22     }
23     
24     public int write(byte[] buf, int off, int len) throws IOException {
25         if(!writable) throw new IOException("read-only data");
26         len = Math.min(len,data.length-pos);
27         if(len <= 0) throw new IOException("no space");
28         System.arraycopy(buf,off,data,pos,len);        
29         pos += len;
30         return len;
31     }
32     
33     public int length() { return data.length; }
34     public int pos() { return pos; }
35     public void seek(int pos) { this.pos = pos; }
36     public void close() { /*noop*/ }
37 }