1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
5 package org.ibex.nestedvm.util;
9 public abstract class Seekable {
10 public abstract int read(byte[] buf, int offset, int length) throws IOException;
11 public abstract int write(byte[] buf, int offset, int length) throws IOException;
12 public abstract int length() throws IOException;
13 public abstract void seek(int pos) throws IOException;
14 public abstract void close() throws IOException;
15 public abstract int pos() throws IOException;
17 public void sync() throws IOException {
18 throw new IOException("sync not implemented for " + getClass());
20 public void resize(long length) throws IOException {
21 throw new IOException("resize not implemented for " + getClass());
23 /** If pos == 0 and size == 0 lock covers whole file. */
24 public Lock lock(long pos, long size, boolean shared) throws IOException {
25 throw new IOException("lock not implemented for " + getClass());
28 public int read() throws IOException {
29 byte[] buf = new byte[1];
30 int n = read(buf,0,1);
31 return n == -1 ? -1 : buf[0]&0xff;
34 public int tryReadFully(byte[] buf, int off, int len) throws IOException {
37 int n = read(buf,off,len);
43 return total == 0 ? -1 : total;
46 public static class ByteArray extends Seekable {
47 protected byte[] data;
49 private final boolean writable;
51 public ByteArray(byte[] data, boolean writable) {
54 this.writable = writable;
57 public int read(byte[] buf, int off, int len) {
58 len = Math.min(len,data.length-pos);
59 if(len <= 0) return -1;
60 System.arraycopy(data,pos,buf,off,len);
65 public int write(byte[] buf, int off, int len) throws IOException {
66 if(!writable) throw new IOException("read-only data");
67 len = Math.min(len,data.length-pos);
68 if(len <= 0) throw new IOException("no space");
69 System.arraycopy(buf,off,data,pos,len);
74 public int length() { return data.length; }
75 public int pos() { return pos; }
76 public void seek(int pos) { this.pos = pos; }
77 public void close() { /*noop*/ }
80 public static class File extends Seekable {
81 private final java.io.File file;
82 private final RandomAccessFile raf;
84 public File(String fileName) throws IOException { this(fileName,false); }
85 public File(String fileName, boolean writable) throws IOException { this(new java.io.File(fileName),writable,false); }
87 public File(java.io.File file, boolean writable, boolean truncate) throws IOException {
89 String mode = writable ? "rw" : "r";
90 raf = new RandomAccessFile(file,mode);
91 if (truncate) Platform.setFileLength(raf, 0);
94 public int read(byte[] buf, int offset, int length) throws IOException { return raf.read(buf,offset,length); }
95 public int write(byte[] buf, int offset, int length) throws IOException { raf.write(buf,offset,length); return length; }
96 public void sync() throws IOException { raf.getFD().sync(); }
97 public void seek(int pos) throws IOException{ raf.seek(pos); }
98 public int pos() throws IOException { return (int) raf.getFilePointer(); }
99 public int length() throws IOException { return (int)raf.length(); }
100 public void close() throws IOException { raf.close(); }
101 public void resize(long length) throws IOException { Platform.setFileLength(raf, (int)length); }
102 public boolean equals(Object o) {
103 return o != null && o instanceof File
104 && file.equals(((File)o).file);
106 public Lock lock(long pos, long size, boolean shared)
108 return Platform.lockFile(this, raf, pos, size, shared);
112 public static class InputStream extends Seekable {
113 private byte[] buffer = new byte[4096];
114 private int bytesRead = 0;
115 private boolean eof = false;
117 private java.io.InputStream is;
119 public InputStream(java.io.InputStream is) { this.is = is; }
121 public int read(byte[] outbuf, int off, int len) throws IOException {
122 if(pos >= bytesRead && !eof) readTo(pos + 1);
123 len = Math.min(len,bytesRead-pos);
124 if(len <= 0) return -1;
125 System.arraycopy(buffer,pos,outbuf,off,len);
130 private void readTo(int target) throws IOException {
131 if(target >= buffer.length) {
132 byte[] buf2 = new byte[Math.max(buffer.length+Math.min(buffer.length,65536),target)];
133 System.arraycopy(buffer,0,buf2,0,bytesRead);
136 while(bytesRead < target) {
137 int n = is.read(buffer,bytesRead,buffer.length-bytesRead);
146 public int length() throws IOException {
147 while(!eof) readTo(bytesRead+4096);
151 public int write(byte[] buf, int off, int len) throws IOException { throw new IOException("read-only"); }
152 public void seek(int pos) { this.pos = pos; }
153 public int pos() { return pos; }
154 public void close() throws IOException { is.close(); }
157 public abstract static class Lock {
158 private Object owner = null;
160 public abstract Seekable seekable();
161 public abstract boolean isShared();
162 public abstract boolean isValid();
163 public abstract void release() throws IOException;
164 public abstract long position();
165 public abstract long size();
167 public void setOwner(Object o) { owner = o; }
168 public Object getOwner() { return owner; }
170 public final boolean contains(int start, int len) {
171 return start >= position() && position() + size() >= start + len;
174 public final boolean contained(int start, int len) {
175 return start < position() && position() + size() < start + len;
178 public final boolean overlaps(int start, int len) {
179 return contains(start, len) || contained(start, len);