Author: ruschein
Date: 2010-02-02 09:48:03 -0800 (Tue, 02 Feb 2010)
New Revision: 19120
Added:
csplugins/trunk/util.compression/src/org/cytoscape/util/compression/Pack.java
Log:
Conversion from byte[] to int[] and back.
Added:
csplugins/trunk/util.compression/src/org/cytoscape/util/compression/Pack.java
===================================================================
---
csplugins/trunk/util.compression/src/org/cytoscape/util/compression/Pack.java
(rev 0)
+++
csplugins/trunk/util.compression/src/org/cytoscape/util/compression/Pack.java
2010-02-02 17:48:03 UTC (rev 19120)
@@ -0,0 +1,43 @@
+package org.cytoscape.util.compression;
+
+
+/**
+ * Class to convert between int and byte arrays.
+ */
+public class Pack {
+ /**
+ * Converts a byte array to an int array. This requires that the byte
array has a size that
+ * is a multiple of 4! The byte order in the ints will be the first
original byte being the
+ * most-significant byte in the first int and so on...
+ */
+ static public int[] pack(final byte[] data) throws
IllegalStateException {
+ if ((data.length % 4) != 0)
+ throw new IllegalStateException("data size must be a
multiple of 4!");
+
+ final int[] retval = new int[data.length >> 2];
+
+ for (int i = 0; i < retval.length; ++i) {
+ retval[i] = (int)data[i << 4] | (int) data[(i << 4) + 1]
+ | (int) data[(i << 4) + 2] |(int) data[(i
<< 4) + 3];
+ }
+
+ return retval;
+ }
+
+ /**
+ * Converts an int array to a byte array. The bytes will be in
most-significant to least significant order.
+ */
+ static public byte[] unpack(final int[] data) {
+ final byte[] retval = new byte[data.length << 2];
+
+ for (int i = 0; i < data.length; ++i) {
+ int value = data[i];
+ retval[i << 2] = (byte)(value >> 24);
+ retval[(i << 2) + 1] = (byte)((value >> 16) & 0xFF);
+ retval[(i << 2) + 2] = (byte)((value >> 8) & 0xFF);
+ retval[(i << 2) + 3] = (byte)(value & 0xFF);
+ }
+
+ return retval;
+ }
+}
--
You received this message because you are subscribed to the Google Groups
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cytoscape-cvs?hl=en.