Example usage for java.nio ByteBuffer remaining

List of usage examples for java.nio ByteBuffer remaining

Introduction

In this page you can find the example usage for java.nio ByteBuffer remaining.

Prototype

public final int remaining() 

Source Link

Document

Returns the number of remaining elements in this buffer, that is limit - position .

Usage

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * ? remaining() ? limit - position//from  ww  w  .  j a v a2 s .com
 * 
 * @param buff
 * @return
 */
public static boolean isEmpty(ByteBuffer buff) {
    return buff == null || buff.remaining() == 0;
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * You should almost never use this. Instead, use the write* methods to
 * avoid copies./*from  w w w  .  java2  s . co m*/
 */
public static byte[] getArray(ByteBuffer buffer) {
    int length = buffer.remaining();

    if (buffer.hasArray()) {
        int boff = buffer.arrayOffset() + buffer.position();
        if (boff == 0 && length == buffer.array().length)
            return buffer.array();
        else
            return Arrays.copyOfRange(buffer.array(), boff, boff + length);
    }
    // else, DirectByteBuffer.get() is the fastest route
    byte[] bytes = new byte[length];
    buffer.duplicate().get(bytes);

    return bytes;
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static int length(ByteBuffer buffer) {
    return buffer == null ? 0 : buffer.remaining();
}

From source file:org.apache.nutch.store.readable.StoreReadable.java

/**
 * Given a <code>ByteBuffer</code> representing an html file of an
 * <em>unknown</em> encoding,  read out 'charset' parameter in the meta tag
 * from the first <code>CHUNK_SIZE</code> bytes.
 * If there's no meta tag for Content-Type or no charset is specified,
 * the content is checked for a Unicode Byte Order Mark (BOM).
 * This will also cover non-byte oriented character encodings (UTF-16 only).
 * If no character set can be determined,
 * <code>null</code> is returned.  <br />
 * See also http://www.w3.org/International/questions/qa-html-encoding-declarations,
 * http://www.w3.org/TR/2011/WD-html5-diff-20110405/#character-encoding, and
 * http://www.w3.org/TR/REC-xml/#sec-guessing
 * <br />//from www.  j a va2  s  . c  o  m
 *
 * @param content <code>ByteBuffer</code> representation of an html file
 */

private static String sniffCharacterEncoding(ByteBuffer content) {
    System.out.println(
            "[STORE-READABLE]sniffCharacterEncoding----------------------------------------------------------");
    int length = Math.min(content.remaining(), CHUNK_SIZE);

    // We don't care about non-ASCII parts so that it's sufficient
    // to just inflate each byte to a 16-bit value by padding.
    // For instance, the sequence {0x41, 0x82, 0xb7} will be turned into
    // {U+0041, U+0082, U+00B7}.
    String str = "";
    try {
        str = new String(content.array(), content.arrayOffset() + content.position(), length,
                Charset.forName("ASCII").toString());
    } catch (UnsupportedEncodingException e) {
        // code should never come here, but just in case...
        return null;
    }

    Matcher metaMatcher = metaPattern.matcher(str);
    String encoding = null;
    if (metaMatcher.find()) {
        Matcher charsetMatcher = charsetPattern.matcher(metaMatcher.group(1));
        if (charsetMatcher.find())
            encoding = new String(charsetMatcher.group(1));
    }
    if (encoding == null) {
        // check for HTML5 meta charset
        metaMatcher = charsetPatternHTML5.matcher(str);
        if (metaMatcher.find()) {
            encoding = new String(metaMatcher.group(1));
        }
    }
    if (encoding == null) {
        // check for BOM
        if (length >= 3 && content.get(0) == (byte) 0xEF && content.get(1) == (byte) 0xBB
                && content.get(2) == (byte) 0xBF) {
            encoding = "UTF-8";
        } else if (length >= 2) {
            if (content.get(0) == (byte) 0xFF && content.get(1) == (byte) 0xFE) {
                encoding = "UTF-16LE";
            } else if (content.get(0) == (byte) 0xFE && content.get(1) == (byte) 0xFF) {
                encoding = "UTF-16BE";
            }
        }
    }

    return encoding;
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static void writeWithLength(ByteBuffer bytes, DataOutput out) throws IOException {
    out.writeInt(bytes.remaining());
    write(bytes, out); // writing data bytes to output source
}

From source file:org.apache.nifi.util.orc.OrcUtils.java

public static void putToRowBatch(ColumnVector col, MutableInt vectorOffset, int rowNumber, Schema fieldSchema,
        Object o) {/*from w ww.ja v a  2s  .co m*/
    Schema.Type fieldType = fieldSchema.getType();

    if (fieldType == null) {
        throw new IllegalArgumentException("Field type is null");
    }

    if (o == null) {
        col.isNull[rowNumber] = true;
    } else {

        switch (fieldType) {
        case INT:
            ((LongColumnVector) col).vector[rowNumber] = (int) o;
            break;
        case LONG:
            ((LongColumnVector) col).vector[rowNumber] = (long) o;
            break;
        case BOOLEAN:
            ((LongColumnVector) col).vector[rowNumber] = ((boolean) o) ? 1 : 0;
            break;
        case BYTES:
            ByteBuffer byteBuffer = ((ByteBuffer) o);
            int size = byteBuffer.remaining();
            byte[] buf = new byte[size];
            byteBuffer.get(buf, 0, size);
            ((BytesColumnVector) col).setVal(rowNumber, buf);
            break;
        case DOUBLE:
            ((DoubleColumnVector) col).vector[rowNumber] = (double) o;
            break;
        case FLOAT:
            ((DoubleColumnVector) col).vector[rowNumber] = (float) o;
            break;
        case STRING:
        case ENUM:
            ((BytesColumnVector) col).setVal(rowNumber, o.toString().getBytes());
            break;
        case UNION:
            // If the union only has one non-null type in it, it was flattened in the ORC schema
            if (col instanceof UnionColumnVector) {
                UnionColumnVector union = ((UnionColumnVector) col);
                Schema.Type avroType = OrcUtils.getAvroSchemaTypeOfObject(o);
                // Find the index in the union with the matching Avro type
                int unionIndex = -1;
                List<Schema> types = fieldSchema.getTypes();
                final int numFields = types.size();
                for (int i = 0; i < numFields && unionIndex == -1; i++) {
                    if (avroType.equals(types.get(i).getType())) {
                        unionIndex = i;
                    }
                }
                if (unionIndex == -1) {
                    throw new IllegalArgumentException("Object type " + avroType.getName()
                            + " not found in union '" + fieldSchema.getName() + "'");
                }

                // Need nested vector offsets
                MutableInt unionVectorOffset = new MutableInt(0);
                putToRowBatch(union.fields[unionIndex], unionVectorOffset, rowNumber,
                        fieldSchema.getTypes().get(unionIndex), o);
            } else {
                // Find and use the non-null type from the union
                List<Schema> types = fieldSchema.getTypes();
                Schema effectiveType = null;
                for (Schema type : types) {
                    if (!Schema.Type.NULL.equals(type.getType())) {
                        effectiveType = type;
                        break;
                    }
                }
                putToRowBatch(col, vectorOffset, rowNumber, effectiveType, o);
            }
            break;
        case ARRAY:
            Schema arrayType = fieldSchema.getElementType();
            ListColumnVector array = ((ListColumnVector) col);
            if (o instanceof int[] || o instanceof long[]) {
                int length = (o instanceof int[]) ? ((int[]) o).length : ((long[]) o).length;
                for (int i = 0; i < length; i++) {
                    ((LongColumnVector) array.child).vector[vectorOffset.getValue() + i] = (o instanceof int[])
                            ? ((int[]) o)[i]
                            : ((long[]) o)[i];
                }
                array.offsets[rowNumber] = vectorOffset.longValue();
                array.lengths[rowNumber] = length;
                vectorOffset.add(length);
            } else if (o instanceof float[]) {
                float[] floatArray = (float[]) o;
                for (int i = 0; i < floatArray.length; i++) {
                    ((DoubleColumnVector) array.child).vector[vectorOffset.getValue() + i] = floatArray[i];
                }
                array.offsets[rowNumber] = vectorOffset.longValue();
                array.lengths[rowNumber] = floatArray.length;
                vectorOffset.add(floatArray.length);
            } else if (o instanceof double[]) {
                double[] doubleArray = (double[]) o;
                for (int i = 0; i < doubleArray.length; i++) {
                    ((DoubleColumnVector) array.child).vector[vectorOffset.getValue() + i] = doubleArray[i];
                }
                array.offsets[rowNumber] = vectorOffset.longValue();
                array.lengths[rowNumber] = doubleArray.length;
                vectorOffset.add(doubleArray.length);
            } else if (o instanceof String[]) {
                String[] stringArray = (String[]) o;
                BytesColumnVector byteCol = ((BytesColumnVector) array.child);
                for (int i = 0; i < stringArray.length; i++) {
                    if (stringArray[i] == null) {
                        byteCol.isNull[rowNumber] = true;
                    } else {
                        byteCol.setVal(vectorOffset.getValue() + i, stringArray[i].getBytes());
                    }
                }
                array.offsets[rowNumber] = vectorOffset.longValue();
                array.lengths[rowNumber] = stringArray.length;
                vectorOffset.add(stringArray.length);
            } else if (o instanceof Map[]) {
                Map[] mapArray = (Map[]) o;
                MutableInt mapVectorOffset = new MutableInt(0);
                for (int i = 0; i < mapArray.length; i++) {
                    if (mapArray[i] == null) {
                        array.child.isNull[rowNumber] = true;
                    } else {
                        putToRowBatch(array.child, mapVectorOffset, vectorOffset.getValue() + i, arrayType,
                                mapArray[i]);
                    }
                }
                array.offsets[rowNumber] = vectorOffset.longValue();
                array.lengths[rowNumber] = mapArray.length;
                vectorOffset.add(mapArray.length);
            } else if (o instanceof List) {
                List listArray = (List) o;
                MutableInt listVectorOffset = new MutableInt(0);
                int numElements = listArray.size();
                for (int i = 0; i < numElements; i++) {
                    if (listArray.get(i) == null) {
                        array.child.isNull[rowNumber] = true;
                    } else {
                        putToRowBatch(array.child, listVectorOffset, vectorOffset.getValue() + i, arrayType,
                                listArray.get(i));
                    }
                }
                array.offsets[rowNumber] = vectorOffset.longValue();
                array.lengths[rowNumber] = numElements;
                vectorOffset.add(numElements);

            } else {
                throw new IllegalArgumentException(
                        "Object class " + o.getClass().getName() + " not supported as an ORC list/array");
            }
            break;
        case MAP:
            MapColumnVector map = ((MapColumnVector) col);

            // Avro maps require String keys
            @SuppressWarnings("unchecked")
            Map<String, ?> mapObj = (Map<String, ?>) o;
            int effectiveRowNumber = vectorOffset.getValue();
            for (Map.Entry<String, ?> entry : mapObj.entrySet()) {
                putToRowBatch(map.keys, vectorOffset, effectiveRowNumber, Schema.create(Schema.Type.STRING),
                        entry.getKey());
                putToRowBatch(map.values, vectorOffset, effectiveRowNumber, fieldSchema.getValueType(),
                        entry.getValue());
                effectiveRowNumber++;
            }
            map.offsets[rowNumber] = vectorOffset.longValue();
            map.lengths[rowNumber] = mapObj.size();
            vectorOffset.add(mapObj.size());

            break;
        default:
            throw new IllegalArgumentException("Field type " + fieldType.getName() + " not recognized");
        }
    }
}

From source file:de.dfki.kiara.jsonrpc.JsonRpcMessage.java

private static JsonNode readFromBuffer(JsonRpcProtocol protocol, ByteBuffer data) throws IOException {
    byte[] array;
    int arrayOffset;
    int arrayLength;
    int oldPos = data.position();
    if (data.hasArray()) {
        array = data.array();//from   w  ww . j  av  a  2  s  . c  o  m
        arrayOffset = data.arrayOffset();
        arrayLength = data.remaining();
    } else {
        array = new byte[data.remaining()];
        data.get(array);
        arrayOffset = 0;
        arrayLength = array.length;
    }
    data.position(oldPos);

    JsonNode node;
    try (JsonParser parser = protocol.getObjectReader().getFactory().createParser(array, arrayOffset,
            arrayLength)) {
        node = parser.readValueAsTree();
    }
    return node;
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * @return a new copy of the data in @param buffer USUALLY YOU SHOULD USE
 *         ByteBuffer.duplicate() INSTEAD, which creates a new Buffer (so
 *         you can mutate its position without affecting the original)
 *         without copying the underlying array.
 *//*from   w  w w  . jav a 2  s.  c  o m*/
public static ByteBuffer clone(ByteBuffer buffer) {
    assert buffer != null;

    if (buffer.remaining() == 0)
        return EMPTY_BYTE_BUFFER;

    ByteBuffer clone = ByteBuffer.allocate(buffer.remaining());

    if (buffer.hasArray()) {
        System.arraycopy(buffer.array(), buffer.arrayOffset() + buffer.position(), clone.array(), 0,
                buffer.remaining());
    } else {
        clone.put(buffer.duplicate());
        clone.flip();
    }

    return clone;
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static String bytesToHex(ByteBuffer bytes) {
    final int offset = bytes.position();
    final int size = bytes.remaining();
    final char[] c = new char[size * 2];
    for (int i = 0; i < size; i++) {
        final int bint = bytes.get(i + offset);
        c[i * 2] = Hex.byteToChar[(bint & 0xf0) >> 4];
        c[1 + i * 2] = Hex.byteToChar[bint & 0x0f];
    }//from  w  w  w  .  j  av  a 2  s  .  co m
    return Hex.wrapCharArray(c);
}

From source file:org.apache.cassandra.db.index.sasi.disk.TokenTreeTest.java

private static DecoratedKey dk(Long token) {
    ByteBuffer buf = ByteBuffer.allocate(8);
    buf.putLong(token);/*from   www.ja  v  a  2  s  .co m*/
    buf.flip();
    Long hashed = MurmurHash.hash2_64(buf, buf.position(), buf.remaining(), 0);
    return new DecoratedKey(new LongToken(hashed), buf);
}