Skip to content

Commit

Permalink
added computeFragOffsets()
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Feb 25, 2004
1 parent 21c1dea commit bf91016
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/org/jgroups/util/Util.java
@@ -1,4 +1,4 @@
// $Id: Util.java,v 1.3 2004/02/25 20:46:23 belaban Exp $
// $Id: Util.java,v 1.4 2004/02/25 21:07:17 belaban Exp $

package org.jgroups.util;

Expand Down Expand Up @@ -297,6 +297,39 @@ public static byte[][] fragmentBuffer(byte[] buf, int frag_size) {
}


/**
* Given a buffer and a fragmentation size, compute a list of fragmentation offset/length pairs, and
* return them in a list. Example:<br/>
* Buffer is 10 bytes, frag_size is 4 bytes. Return value will be ({0,4}, {4,4}, {8,2}).
* This is a total of 3 fragments: the first fragment starts at 0, and has a length of 4 bytes, the second fragment
* starts at offset 4 and has a length of 4 bytes, and the last fragment starts at offset 8 and has a length
* of 2 bytes.
* @param buf
* @param frag_size
* @return List. A List<Range> of offset/length pairs
*/
public static java.util.List computeFragOffsets(byte[] buf, int frag_size) {
java.util.List retval=new ArrayList();
long total_size=buf.length;
int accumulated_size=0;
int tmp_size=0;
Range r;

num_frags=buf.length % frag_size == 0 ? buf.length / frag_size : buf.length / frag_size + 1;

while(accumulated_size < total_size) {
if(accumulated_size + frag_size <= total_size)
tmp_size=frag_size;
else
tmp_size=(int)(total_size - accumulated_size);
r=new Range(accumulated_size, tmp_size);
retval.add(r);
accumulated_size+=tmp_size;
}
return retval;
}


/**
Concatenates smaller fragments into entire buffers.
@param fragments An array of byte buffers (<code>byte[]</code>)
Expand Down

0 comments on commit bf91016

Please sign in to comment.