Permalink
Browse files

Minor reorganisation of helpers.

  • Loading branch information...
1 parent 9543ccf commit 4c6245ea50ba5ea17369624a8bdc307086720e7d @esstrifork esstrifork committed Jun 24, 2011
@@ -96,14 +96,30 @@ public void applyAppendSpec(DeltaZip.AppendSpecification spec) throws IOExceptio
}
- //====================
+ //==================== ByteBuffer utilities ====================
+
public static void writeBufferTo(ByteBuffer data, OutputStream out) throws IOException {
WritableByteChannel channel = Channels.newChannel(out);
while (data.hasRemaining()) channel.write(data);
}
+ public static byte[] allToByteArray(ByteBuffer org) {
+ int save_pos = org.position();
+ org.position(0);
+ byte[] buf = remainingToByteArray(org);
+ org.position(save_pos);
+
+ return buf;
+ }
+
+ public static byte[] remainingToByteArray(ByteBuffer org) {
+ byte[] buf = new byte[org.remaining()];
+ org.get(buf);
+ return buf;
+ }
+
public static int computeAdler32(ByteBuffer data) {
- return computeAdler32(DeltaZip.allToByteArray(data)); // Oh, the copying.
+ return computeAdler32(allToByteArray(data)); // Oh, the copying.
}
public static int computeAdler32(byte[] data) {
Adler32 acc = new Adler32();
@@ -98,7 +98,7 @@ public AppendSpecification add(ByteBuffer new_version) throws IOException {
ByteBuffer last_version = get();
if (last_version != null) {
- pack_delta(last_version, allToByteArray(new_version), baos);
+ pack_delta(last_version, DZUtil.allToByteArray(new_version), baos);
}
pack_snapshot(new_version, baos);
@@ -119,7 +119,9 @@ public AppendSpecification add(Iterator<ByteBuffer> versions_to_add) throws IOEx
while (versions_to_add.hasNext()) {
ByteBuffer cur = versions_to_add.next();
- if (prev_version != null) pack_delta(prev_version, allToByteArray(cur), baos);
+ if (prev_version != null) {
+ pack_delta(prev_version, DZUtil.allToByteArray(cur), baos);
+ }
prev_version = cur;
}
@@ -228,27 +230,8 @@ protected void pack_entry(ByteBuffer version, byte[] ref_version, CompressionMet
dst.writeBigEndianInteger(tag, 4);
}
- //==================== Compression methods =============================
- protected static abstract class CompressionMethod {
- public abstract int methodNumber();
- public abstract void compress(ByteBuffer org, byte[] ref_data, OutputStream dst) throws IOException;
- public abstract byte[] uncompress(ByteBuffer org, byte[] ref_data, Inflater inflater) throws IOException;
- }
-
- public static byte[] allToByteArray(ByteBuffer org) {
- int save_pos = org.position();
- org.position(0);
- byte[] buf = remainingToByteArray(org);
- org.position(save_pos);
-
- return buf;
- }
- public static byte[] remainingToByteArray(ByteBuffer org) {
- byte[] buf = new byte[org.remaining()];
- org.get(buf);
- return buf;
- }
+ //========== ByteArrayOutputStream with 'blanks' support ==========
protected static class ExtByteArrayOutputStream extends ByteArrayOutputStream {
public int insertBlank(int len) {
@@ -277,6 +260,13 @@ public void writeBigEndianInteger(int value, int len) {
}
}
+ //==================== Compression methods =============================
+ protected static abstract class CompressionMethod {
+ public abstract int methodNumber();
+ public abstract void compress(ByteBuffer org, byte[] ref_data, OutputStream dst) throws IOException;
+ public abstract byte[] uncompress(ByteBuffer org, byte[] ref_data, Inflater inflater) throws IOException;
+ }
+
//==================== Interface types ==============================
public interface Access {
@@ -73,7 +73,7 @@ public static void do_get(String[] args) throws IOException {
}
dz.previous();
}
- System.out.write(DeltaZip.allToByteArray(dz.get()));
+ System.out.write(DZUtil.allToByteArray(dz.get()));
}
fa.close();
}
@@ -23,7 +23,7 @@ public void compress(ByteBuffer org, byte[] ref_data, OutputStream dst) {
}
public byte[] uncompress(ByteBuffer org, byte[] ref_data, Inflater inflater) {
- return DeltaZip.remainingToByteArray(org);
+ return DZUtil.remainingToByteArray(org);
}
}
@@ -141,7 +141,7 @@ public void very_related_test() throws IOException {
ByteBuffer[] versions = new ByteBuffer[40];
versions[0] = createRandomBinary(100000, rnd);
for (int i=1; i<versions.length; i++) {
- byte[] tmp = DeltaZip.allToByteArray(versions[i-1]);
+ byte[] tmp = DZUtil.allToByteArray(versions[i-1]);
int nMutations = rnd.nextInt(20);
for (int k=0; k<nMutations; k++)
tmp[rnd.nextInt(tmp.length)] = (byte) rnd.nextInt(256);
@@ -160,7 +160,7 @@ public void somewhat_related_test() throws IOException {
ByteBuffer[] versions = new ByteBuffer[40];
versions[0] = createRandomBinary(100000, rnd);
for (int i=1; i<versions.length; i++) {
- byte[] tmp = DeltaZip.allToByteArray(versions[i-1]);
+ byte[] tmp = DZUtil.allToByteArray(versions[i-1]);
// Single-byte mutations:
int nMutations = rnd.nextInt(20);
@@ -230,12 +230,12 @@ public void series_test_with(ByteBuffer[] versions) throws IOException {
//======================================================================
public static String toString(ByteBuffer buf) {
- String r = new String(DeltaZip.allToByteArray(buf.duplicate()));
+ String r = new String(DZUtil.allToByteArray(buf.duplicate()));
return r;
}
public static void dump(String s, ByteBuffer buf) {
- dump(s, DeltaZip.allToByteArray(buf.duplicate()));
+ dump(s, DZUtil.allToByteArray(buf.duplicate()));
}
public static void dump(String s, byte[] buf) {

0 comments on commit 4c6245e

Please sign in to comment.