Package com.webfirmframework.wffweb.util
Class ByteBufferUtil
java.lang.Object
com.webfirmframework.wffweb.util.ByteBufferUtil
- Since:
- 3.0.1
-
Method Summary
Modifier and TypeMethodDescriptionstatic ByteBuffer
merge
(ByteBuffer... dataArray) Merges and returns ByteBuffer from the given dataArray.static int
slice
(ByteBuffer data, int maxSliceSize, Slice<ByteBuffer> slice) Eg:static int
sliceIfRequired
(ByteBuffer data, int maxSliceSize, Slice<ByteBuffer> slice) It slices only if required, i.e.
-
Method Details
-
slice
Eg:final String s = "1234567890abc"; final ByteBuffer inputData = ByteBuffer .wrap(s.getBytes(StandardCharsets.UTF_8)); final StringBuilder builder = new StringBuilder(); final int totalSlices = ByteBufferUtil.slice(inputData, 3, (part, last) -> { builder.append( new String(part.array(), StandardCharsets.UTF_8)); return !last; }); System.out.println(s.equals(builder.toString())); System.out.println(5 == totalSlices);
true true
- Parameters:
data
- ByteBuffer to be slicedmaxSliceSize
- the maximum size of ByteBuffer of the sliced part.slice
- the Slice functional interface. The method Slice.each must return true to continue slicing.- Returns:
- the total number of slices made. NB: it is just the count of invocation of Slice.each method. If the Slice.each returned false while the last (second param in Slice.each) is false then the returned slice count will be less than the probable number of slices in the input data.
- Since:
- 3.0.1
-
sliceIfRequired
It slices only if required, i.e. if the capacity of input data is equal to the remaining data in the buffer and the capacity is less than or equal to the maxSliceSize then the Slice.each method will reuse the same input data as the first argument.
Eg:final String s = "1234567890abc"; final ByteBuffer inputData = ByteBuffer .wrap(s.getBytes(StandardCharsets.UTF_8)); final StringBuilder builder = new StringBuilder(); final int totalSlices = ByteBufferUtil.sliceIfRequired(inputData, 3, (part, last) -> { builder.append( new String(part.array(), StandardCharsets.UTF_8)); return !last; }); System.out.println(s.equals(builder.toString())); System.out.println(5 == totalSlices);
true true
- Parameters:
data
- ByteBuffer to be slicedmaxSliceSize
- the maximum size of ByteBuffer of the sliced part.slice
- the Slice functional interface. The method Slice.each must return true to continue slicing.- Returns:
- the total number of slices made. NB: it is just the count of invocation of Slice.each method. If the Slice.each returned false while the last (second param in Slice.each) is false then the returned slice count will be less than the probable number of slices in the input data.
- Since:
- 3.0.1
-
merge
Merges and returns ByteBuffer from the given dataArray. The ByteBuffer will be returned after flip.- Parameters:
dataArray
- the ByteByffers to merge- Returns:
- a single ByteBuffer after flip merged from all ByteBuffer objects from dataArray.
- Since:
- 3.0.2
-