X-Git-Url: http://git.megacz.com/?p=nestedvm.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fnestedvm%2Futil%2FSeekable.java;fp=src%2Forg%2Fibex%2Fnestedvm%2Futil%2FSeekable.java;h=98f2c8e413dc0bebf6eeb53f9db205d2d4135472;hp=85a48cc0d4cac58bcd2bc86e739c8473ed0bd00c;hb=034a42fa65955289442614ef9914e5474fac62aa;hpb=40de2c62cb907622ff6f5fcdbeccc8773a1d7b2d diff --git a/src/org/ibex/nestedvm/util/Seekable.java b/src/org/ibex/nestedvm/util/Seekable.java index 85a48cc..98f2c8e 100644 --- a/src/org/ibex/nestedvm/util/Seekable.java +++ b/src/org/ibex/nestedvm/util/Seekable.java @@ -2,15 +2,21 @@ package org.ibex.nestedvm.util; import java.io.*; -public interface Seekable { - public int read(byte[] buf, int offset, int length) throws IOException; - public int write(byte[] buf, int offset, int length) throws IOException; - public int length() throws IOException; - public void seek(int pos) throws IOException; - public void close() throws IOException; - public int pos() throws IOException; +public abstract class Seekable { + public abstract int read(byte[] buf, int offset, int length) throws IOException; + public abstract int write(byte[] buf, int offset, int length) throws IOException; + public abstract int length() throws IOException; + public abstract void seek(int pos) throws IOException; + public abstract void close() throws IOException; + public abstract int pos() throws IOException; - public class ByteArray implements Seekable { + public int read() throws IOException { + byte[] buf = new byte[1]; + int n = read(buf,0,1); + return n == -1 ? -1 : buf[0]&0xff; + } + + public static class ByteArray extends Seekable { protected byte[] data; protected int pos; private final boolean writable; @@ -28,7 +34,7 @@ public interface Seekable { pos += len; return len; } - + public int write(byte[] buf, int off, int len) throws IOException { if(!writable) throw new IOException("read-only data"); len = Math.min(len,data.length-pos); @@ -44,7 +50,7 @@ public interface Seekable { public void close() { /*noop*/ } } - public class File implements Seekable { + public static class File extends Seekable { private final RandomAccessFile raf; public File(String fileName) throws IOException { this(fileName,false); } @@ -65,7 +71,7 @@ public interface Seekable { public void close() throws IOException { raf.close(); } } - public class InputStream implements Seekable { + public static class InputStream extends Seekable { private byte[] buffer = new byte[4096]; private int bytesRead = 0; private boolean eof = false;