Permalink
Browse files

DZUtil.FileAccess: Added applyAppendSpec().

  • Loading branch information...
esstrifork committed Jun 22, 2011
1 parent ffdd2e7 commit 9befc97a8bd3a83ab4b1cc6d70c8fbbb15f708a8
Showing with 29 additions and 8 deletions.
  1. +29 −8 src/main/java/com/trifork/deltazip/DZUtil.java
@@ -6,8 +6,9 @@
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
-import java.nio.channels.WritableByteChannel;
import java.nio.channels.Channels;
+import java.nio.channels.WritableByteChannel;
+import java.nio.channels.FileChannel;
public abstract class DZUtil {
public static class ByteArrayAccess implements DeltaZip.Access {
@@ -38,25 +39,45 @@ public ByteBuffer pread(long pos, int len) throws IOException {
public static class FileAccess {
- private final RandomAccessFile file;
+ private final FileChannel file;
public FileAccess(File f) throws IOException {
- this.file = new RandomAccessFile(f, "r");
+ this.file = new RandomAccessFile(f, "r").getChannel();
+ }
+
+ public void close() throws IOException {
+ file.close();
}
public long getSize() throws IOException {
- return file.length();
+ return file.size();
}
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);
+ ByteBuffer res = ByteBuffer.allocate(len);
+ while (res.hasRemaining()) {
+ int r = file.read(res, pos);
+ if (r<0) throw new IOException("End of file reached");
+ pos += r;
+ }
- return ByteBuffer.wrap(buf).asReadOnlyBuffer();
+ return res;
}
+ public void applyAppendSpec(DeltaZip.AppendSpecification spec) throws IOException {
+ long pos = spec.prefix_size;
+ ByteBuffer tail = spec.new_tail;
+ long total_length = (spec.prefix_size + tail.remaining());
+
+ while (tail.hasRemaining()) {
+ int w = file.write(tail, pos);
+ pos += w;
+ }
+ if (pos != total_length) throw new IOException("Internal error");
+ file.truncate(pos);
+ }
+
}
}

0 comments on commit 9befc97

Please sign in to comment.