Permalink
Browse files

DZUtil: Added FileAccess.

  • Loading branch information...
1 parent 4f05c35 commit ffdd2e74f55bb36029797c44ef86691a716b4b9e @esstrifork esstrifork committed Jun 22, 2011
Showing with 31 additions and 7 deletions.
  1. +28 −4 src/main/java/com/trifork/deltazip/DZUtil.java
  2. +3 −3 src/main/java/com/trifork/deltazip/DeltaZip.java
@@ -2,19 +2,25 @@
import java.io.IOException;
import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.RandomAccessFile;
+
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.nio.channels.Channels;
public abstract class DZUtil {
public static class ByteArrayAccess implements DeltaZip.Access {
- final byte[] data;
+ private final byte[] data;
+
public ByteArrayAccess(byte[] data) {this.data = data;}
public long getSize() {return data.length;}
- public ByteBuffer pread(long pos, long len) {
- System.err.println("DB| pread("+pos+","+len+") of "+data.length);
- return ByteBuffer.wrap(data, (int)pos, (int)len).slice().asReadOnlyBuffer();
+
+ public ByteBuffer pread(long pos, int len) throws IOException {
+ if (pos < 0 || pos > data.length) throw new IOException("Bad position");
+// System.err.println("DB| pread("+pos+","+len+") of "+data.length);
+ return ByteBuffer.wrap(data, (int)pos, len).slice().asReadOnlyBuffer();
}
public byte[] applyAppendSpec(DeltaZip.AppendSpecification spec) {
@@ -32,6 +38,24 @@ public ByteBuffer pread(long pos, long len) {
public static class FileAccess {
+ private final RandomAccessFile file;
+
+ public FileAccess(File f) throws IOException {
+ this.file = new RandomAccessFile(f, "r");
+ }
+
+ public long getSize() throws IOException {
+ return file.length();
+ }
+
+ public ByteBuffer pread(long pos, int len) throws IOException {
+// System.err.println("DB| pread("+pos+","+len+") of "+file);
+ byte[] buf = new byte [len];
+ file.seek(pos);
+ file.read(buf);
+
+ return ByteBuffer.wrap(buf).asReadOnlyBuffer();
+ }
}
}
@@ -95,7 +95,7 @@ protected void set_cursor_at_end() throws IOException {
if (hasPrevious()) previous();
}
- protected void set_initial_position() {
+ protected void set_initial_position() throws IOException {
current_pos = access.getSize();
}
@@ -190,8 +190,8 @@ public void writeBigEndianInteger(int value, int len) {
//==================== Interface types ==============================
public interface Access {
- long getSize();
- ByteBuffer pread(long offset, long size);
+ long getSize() throws IOException;
+ ByteBuffer pread(long offset, int size) throws IOException;
}
public final class AppendSpecification {

0 comments on commit ffdd2e7

Please sign in to comment.