Java ByteBuffer Set fromListToSetByteArray(List list)

Here you can find the source of fromListToSetByteArray(List list)

Description

from List To Set Byte Array

License

Apache License

Declaration

private static Set<byte[]> fromListToSetByteArray(List<ByteBuffer> list) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;

import java.util.Comparator;

import java.util.List;

import java.util.Set;
import java.util.TreeSet;

public class Main {
    private static Set<byte[]> fromListToSetByteArray(List<ByteBuffer> list) {
        Set<byte[]> set = new TreeSet<byte[]>(new Comparator<byte[]>() {
            @Override/*  w  ww .j a v a2  s  . c  o m*/
            public int compare(byte[] left, byte[] right) {
                return compareTo(left, 0, left.length, right, 0, right.length);
            }

            public int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2,
                    int length2) {
                if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) {
                    return 0;
                }
                //WritableComparator code
                int end1 = offset1 + length1;
                int end2 = offset2 + length2;
                for (int i = offset1, j = offset2; i < end1 && j < end2; i++, j++) {
                    int a = (buffer1[i] & 0xff);
                    int b = (buffer2[j] & 0xff);
                    if (a != b) {
                        return a - b;
                    }
                }
                return length1 - length2;
            }
        });
        for (ByteBuffer e : list) {
            set.add(e.array());
        }
        return set;
    }
}

Related

  1. charstoUTF8Bytes(char[] srcBuf, int srcOffset, int srcLength, ByteBuffer dest, int destOffset)
  2. equals(final ByteBuffer keyValue, final int offset, final int keyLen, final byte[] otherKey)
  3. find(ByteBuffer buffer, int offset, byte searchKey)
  4. findCommonPrefix(ByteBuffer buffer, int offsetLeft, int offsetRight, int limit)
  5. from(ByteBuffer buffer, int offset)
  6. get(ByteBuffer srcBuffer, byte[] dstBytes, int dstOffset, int length)
  7. getCharsetFromDocument(ByteBuffer bb)
  8. getEquals(ByteBuffer buf, String s, String charsetName)
  9. getSignedInt(ByteBuffer buffer, int offset)