commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [3/4] commons-compress git commit: byte[] output version
Date Tue, 24 Jan 2017 13:15:01 GMT
byte[] output version


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/0eef29b3
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/0eef29b3
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/0eef29b3

Branch: refs/heads/master
Commit: 0eef29b38c209b7af55d9d867adb280ef079a15d
Parents: 22252ae
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Tue Jan 24 13:46:43 2017 +0100
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Tue Jan 24 13:46:43 2017 +0100

----------------------------------------------------------------------
 .../apache/commons/compress/utils/ByteUtils.java    | 16 ++++++++++++++++
 .../commons/compress/utils/ByteUtilsTest.java       | 15 +++++++++++++++
 2 files changed, 31 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/0eef29b3/src/main/java/org/apache/commons/compress/utils/ByteUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/utils/ByteUtils.java b/src/main/java/org/apache/commons/compress/utils/ByteUtils.java
index 579b0bf..b6d08f7 100644
--- a/src/main/java/org/apache/commons/compress/utils/ByteUtils.java
+++ b/src/main/java/org/apache/commons/compress/utils/ByteUtils.java
@@ -146,6 +146,22 @@ public final class ByteUtils {
     }
 
     /**
+     * Inserts the given value into the array as a little endian
+     * sequence of the given length starting at the given offset.
+     * @param array the array to write into
+     * @param value the value to insert
+     * @param offset the offset into the array that receives the first byte
+     * @param length the number of bytes to use to represent the value
+     */
+    public static void toLittleEndian(final byte[] b, final long value, final int off, final
int length) {
+        long num = value;
+        for (int i = 0; i < length; i++) {
+            b[off + i] = (byte) (num & 0xff);
+            num >>= 8;
+        }
+    }
+
+    /**
      * Writes the given value to the given stream as a little endian
      * array of the given length.
      * @param out the stream to write to

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/0eef29b3/src/test/java/org/apache/commons/compress/utils/ByteUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress/utils/ByteUtilsTest.java b/src/test/java/org/apache/commons/compress/utils/ByteUtilsTest.java
index 416ac00..1671ec0 100644
--- a/src/test/java/org/apache/commons/compress/utils/ByteUtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/ByteUtilsTest.java
@@ -25,6 +25,7 @@ import java.io.DataInputStream;
 import java.io.DataOutput;
 import java.io.DataOutputStream;
 import java.io.IOException;
+import java.util.Arrays;
 
 import org.junit.Test;
 
@@ -189,4 +190,18 @@ public class ByteUtilsTest {
         assertArrayEquals(new byte[] { 2, 3, 4, (byte) 128 }, bos.toByteArray());
     }
 
+
+    @Test
+    public void toLittleEndianToByteArray() throws IOException {
+        byte[] b = new byte[4];
+        toLittleEndian(b, 2 + 3 * 256 + 4 * 256 * 256, 1, 3);
+        assertArrayEquals(new byte[] { 2, 3, 4 }, Arrays.copyOfRange(b, 1, 4));
+    }
+
+    @Test
+    public void toLittleEndianToByteArrayUnsignedInt32() throws IOException {
+        byte[] b = new byte[4];
+        toLittleEndian(b, 2 + 3 * 256 + 4 * 256 * 256 + 128l * 256 * 256 * 256, 0, 4);
+        assertArrayEquals(new byte[] { 2, 3, 4, (byte) 128 }, b);
+    }
 }


Mime
View raw message