Example usage for java.nio ByteBuffer putShort

List of usage examples for java.nio ByteBuffer putShort

Introduction

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

Prototype

public abstract ByteBuffer putShort(short value);

Source Link

Document

Writes the given short to the current position and increases the position by 2.

Usage

From source file:org.opendaylight.lispflowmapping.implementation.serializer.MapRequestSerializer.java

public ByteBuffer serialize(MapRequest mapRequest) {
    int size = Length.HEADER_SIZE;
    if (mapRequest.getSourceEid() != null && mapRequest.getSourceEid().getLispAddressContainer() != null) {
        size += LispAddressSerializer.getInstance()
                .getAddressSize(LispAFIConvertor.toAFI(mapRequest.getSourceEid().getLispAddressContainer()));
    } else {/* w w  w . j av a2  s.c om*/
        size += 2;
    }
    if (mapRequest.getItrRloc() != null) {
        for (ItrRloc address : mapRequest.getItrRloc()) {
            size += LispAddressSerializer.getInstance()
                    .getAddressSize(LispAFIConvertor.toAFI(address.getLispAddressContainer()));
        }
    }
    if (mapRequest.getEidRecord() != null) {
        for (EidRecord record : mapRequest.getEidRecord()) {
            size += 2 + LispAddressSerializer.getInstance()
                    .getAddressSize(LispAFIConvertor.toAFI(record.getLispAddressContainer()));
        }
    }
    ByteBuffer requestBuffer = ByteBuffer.allocate(size);
    requestBuffer.put((byte) ((byte) (LispMessageEnum.MapRequest.getValue() << 4)
            | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isAuthoritative()), Flags.AUTHORITATIVE)
            | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isMapDataPresent()), Flags.MAP_DATA_PRESENT)
            | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isProbe()), Flags.PROBE)
            | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isSmr()), Flags.SMR)));
    requestBuffer.put((byte) (ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isPitr()), Flags.PITR)
            | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isSmrInvoked()), Flags.SMR_INVOKED)));
    if (mapRequest.getItrRloc() != null) {
        int IRC = mapRequest.getItrRloc().size();
        if (IRC > 0) {
            IRC--;
        }
        requestBuffer.put((byte) (IRC));
    } else {
        requestBuffer.put((byte) 0);

    }
    if (mapRequest.getEidRecord() != null) {
        requestBuffer.put((byte) mapRequest.getEidRecord().size());
    } else {
        requestBuffer.put((byte) 0);

    }
    requestBuffer.putLong(NumberUtil.asLong(mapRequest.getNonce()));
    if (mapRequest.getSourceEid() != null && mapRequest.getSourceEid().getLispAddressContainer() != null) {
        LispAddressSerializer.getInstance().serialize(requestBuffer,
                LispAFIConvertor.toAFI(mapRequest.getSourceEid().getLispAddressContainer()));
    } else {
        requestBuffer.putShort((short) 0);
    }
    if (mapRequest.getItrRloc() != null) {
        for (ItrRloc address : mapRequest.getItrRloc()) {
            LispAddressSerializer.getInstance().serialize(requestBuffer,
                    LispAFIConvertor.toAFI(address.getLispAddressContainer()));
        }
    }
    if (mapRequest.getEidRecord() != null) {
        for (EidRecord record : mapRequest.getEidRecord()) {
            requestBuffer.put((byte) 0);
            requestBuffer.put((byte) record.getMask().byteValue());
            LispAddressSerializer.getInstance().serialize(requestBuffer,
                    LispAFIConvertor.toAFI(record.getLispAddressContainer()));
        }
    }
    if (mapRequest.getMapReply() != null) {
        ByteBuffer replyBuffer = ByteBuffer.allocate(
                EidToLocatorRecordSerializer.getInstance().getSerializationSize(mapRequest.getMapReply()));
        EidToLocatorRecordSerializer.getInstance().serialize(replyBuffer, mapRequest.getMapReply());
        ByteBuffer combinedBuffer = ByteBuffer.allocate(requestBuffer.capacity() + replyBuffer.capacity());
        combinedBuffer.put(requestBuffer.array());
        combinedBuffer.put(replyBuffer.array());
        return combinedBuffer;
    }
    return requestBuffer;
}

From source file:com.healthmarketscience.jackcess.Table.java

/**
 * @param buffer Buffer to write to/*from   w w  w. j ava  2s . co m*/
 * @param columns List of Columns in the table
 */
private static void writeTableDefinitionHeader(TableCreator creator, ByteBuffer buffer, int totalTableDefSize)
        throws IOException {
    List<Column> columns = creator.getColumns();

    //Start writing the tdef
    writeTablePageHeader(buffer);
    buffer.putInt(totalTableDefSize); //Length of table def
    buffer.putInt(MAGIC_TABLE_NUMBER); // seemingly constant magic value
    buffer.putInt(0); //Number of rows
    buffer.putInt(0); //Last Autonumber
    buffer.put((byte) 1); // this makes autonumbering work in access
    for (int i = 0; i < 15; i++) { //Unknown
        buffer.put((byte) 0);
    }
    buffer.put(Table.TYPE_USER); //Table type
    buffer.putShort((short) columns.size()); //Max columns a row will have
    buffer.putShort(Column.countVariableLength(columns)); //Number of variable columns in table
    buffer.putShort((short) columns.size()); //Number of columns in table
    buffer.putInt(creator.getLogicalIndexCount()); //Number of logical indexes in table
    buffer.putInt(creator.getIndexCount()); //Number of indexes in table
    buffer.put((byte) 0); //Usage map row number
    ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Usage map page number
    buffer.put((byte) 1); //Free map row number
    ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Free map page number
    if (LOG.isDebugEnabled()) {
        int position = buffer.position();
        buffer.rewind();
        LOG.debug("Creating new table def block:\n"
                + ByteUtil.toHexString(buffer, creator.getFormat().SIZE_TDEF_HEADER));
        buffer.position(position);
    }
}

From source file:spade.storage.CDM.java

/**
 * Converts the permissions string to short first (using base 8) and then
 * adds writes the short to a bytebuffer and then gets the byte array from the
 * buffer which is then added to the CDM SHORT type
 * //from www  . j  a  v  a2s .  c  om
 * @param permissions octal permissions string
 * @return CDM SHORT type or null
 */
private SHORT getPermissionsAsCDMSHORT(String permissions) {
    // IMPORTANT: If this function is changed then change the function in CDM reporter which reverses this
    if (permissions == null || permissions.isEmpty()) {
        return null;
    } else {
        Short permissionsShort = Short.parseShort(permissions, 8);
        ByteBuffer bb = ByteBuffer.allocate(2);
        bb.putShort(permissionsShort);
        SHORT cdmPermissions = new SHORT(bb.array());
        return cdmPermissions;
    }
}

From source file:org.bimserver.geometry.GeometryRunner.java

private ByteBuffer quantizeVertices(float[] vertices, float[] quantizationMatrix, float multiplierToMm) {
    ByteBuffer quantizedBuffer = ByteBuffer.wrap(new byte[vertices.length * 2]);
    quantizedBuffer.order(ByteOrder.LITTLE_ENDIAN);

    float[] vertex = new float[4];
    float[] result = new float[4];
    vertex[3] = 1;//from ww w .  j  av  a2  s  .c o  m
    int nrVertices = vertices.length;
    for (int i = 0; i < nrVertices; i += 3) {
        vertex[0] = vertices[i];
        vertex[1] = vertices[i + 1];
        vertex[2] = vertices[i + 2];

        if (multiplierToMm != 1f) {
            vertex[0] = vertex[0] * multiplierToMm;
            vertex[1] = vertex[1] * multiplierToMm;
            vertex[2] = vertex[2] * multiplierToMm;
        }

        Matrix.multiplyMV(result, 0, quantizationMatrix, 0, vertex, 0);

        quantizedBuffer.putShort((short) result[0]);
        quantizedBuffer.putShort((short) result[1]);
        quantizedBuffer.putShort((short) result[2]);
    }

    return quantizedBuffer;
}

From source file:org.bimserver.geometry.StreamingGeometryGenerator.java

private ByteBuffer quantizeVertices(DoubleBuffer vertices, double[] quantizationMatrix, double multiplierToMm) {
    ByteBuffer quantizedBuffer = ByteBuffer.wrap(new byte[vertices.capacity() * 2]);
    quantizedBuffer.order(ByteOrder.LITTLE_ENDIAN);

    double[] vertex = new double[4];
    double[] result = new double[4];
    vertex[3] = 1;/*  w  w  w  .  j a v a2  s  . co m*/
    int nrVertices = vertices.capacity();
    for (int i = 0; i < nrVertices; i += 3) {
        vertex[0] = vertices.get(i);
        vertex[1] = vertices.get(i + 1);
        vertex[2] = vertices.get(i + 2);

        if (multiplierToMm != 1f) {
            vertex[0] = vertex[0] * multiplierToMm;
            vertex[1] = vertex[1] * multiplierToMm;
            vertex[2] = vertex[2] * multiplierToMm;
        }

        Matrix.multiplyMV(result, 0, quantizationMatrix, 0, vertex, 0);

        quantizedBuffer.putShort((short) result[0]);
        quantizedBuffer.putShort((short) result[1]);
        quantizedBuffer.putShort((short) result[2]);
    }

    return quantizedBuffer;
}

From source file:org.apache.carbondata.core.scan.filter.FilterUtil.java

public static byte[] getNoDictionaryDefaultStartKey(SegmentProperties segmentProperties) {

    int numberOfNoDictionaryDimension = segmentProperties.getNumberOfNoDictSortColumns();
    // in case of non filter query when no dictionary columns are present we
    // need to set the default start key, as for non filter query we need to get the first
    // block of the btree so we are setting the least byte value in the start key
    ByteBuffer noDictionaryStartKeyBuffer = ByteBuffer
            .allocate((numberOfNoDictionaryDimension * CarbonCommonConstants.SHORT_SIZE_IN_BYTE)
                    + numberOfNoDictionaryDimension);
    // end key structure will be
    //<Offset of first No Dictionary key in 2 Bytes><Offset of second No Dictionary key in 2 Bytes>
    //<Offset of n No Dictionary key in 2 Bytes><first no dictionary column value>
    // <second no dictionary column value> <N no dictionary column value>
    //example if we have 2 no dictionary column
    //<[0,4,0,5,0,0]>
    short startPoint = (short) (numberOfNoDictionaryDimension * CarbonCommonConstants.SHORT_SIZE_IN_BYTE);
    for (int i = 0; i < numberOfNoDictionaryDimension; i++) {
        noDictionaryStartKeyBuffer.putShort((startPoint));
        startPoint++;/*from  ww  w .j av a  2  s . c om*/
    }
    for (int i = 0; i < numberOfNoDictionaryDimension; i++) {
        noDictionaryStartKeyBuffer.put((byte) 0);
    }
    return noDictionaryStartKeyBuffer.array();
}

From source file:org.apache.carbondata.core.scan.filter.FilterUtil.java

public static byte[] getNoDictionaryDefaultEndKey(SegmentProperties segmentProperties) {

    int numberOfNoDictionaryDimension = segmentProperties.getNumberOfNoDictSortColumns();
    // in case of non filter query when no dictionary columns are present we
    // need to set the default end key, as for non filter query
    // we need to get the last
    // block of the btree so we are setting the max byte value in the end key
    ByteBuffer noDictionaryEndKeyBuffer = ByteBuffer
            .allocate((numberOfNoDictionaryDimension * CarbonCommonConstants.SHORT_SIZE_IN_BYTE)
                    + numberOfNoDictionaryDimension);
    // end key structure will be
    //<Offset of first No Dictionary key in 2 Bytes><Offset of second No Dictionary key in 2 Bytes>
    //<Offset of n No Dictionary key in 2 Bytes><first no dictionary column value>
    // <second no dictionary column value> <N no dictionary column value>
    //example if we have 2 no dictionary column
    //<[0,4,0,5,127,127]>
    short startPoint = (short) (numberOfNoDictionaryDimension * CarbonCommonConstants.SHORT_SIZE_IN_BYTE);
    for (int i = 0; i < numberOfNoDictionaryDimension; i++) {
        noDictionaryEndKeyBuffer.putShort((startPoint));
        startPoint++;//from   w w  w  .j  a va2s .  c  o m
    }
    for (int i = 0; i < numberOfNoDictionaryDimension; i++) {
        noDictionaryEndKeyBuffer.put((byte) 0xFF);
    }
    return noDictionaryEndKeyBuffer.array();
}

From source file:com.healthmarketscience.jackcess.Column.java

/**
 * Writes the column definitions into a table definition buffer.
 * @param buffer Buffer to write to/*from ww  w . j a  v  a  2 s  . com*/
 * @param columns List of Columns to write definitions for
 */
protected static void writeDefinitions(TableCreator creator, ByteBuffer buffer) throws IOException {
    List<Column> columns = creator.getColumns();
    short columnNumber = (short) 0;
    short fixedOffset = (short) 0;
    short variableOffset = (short) 0;
    // we specifically put the "long variable" values after the normal
    // variable length values so that we have a better chance of fitting it
    // all (because "long variable" values can go in separate pages)
    short longVariableOffset = Column.countNonLongVariableLength(columns);
    for (Column col : columns) {
        // record this for later use when writing indexes
        col.setColumnNumber(columnNumber);

        int position = buffer.position();
        buffer.put(col.getType().getValue());
        buffer.putInt(Table.MAGIC_TABLE_NUMBER); //constant magic number
        buffer.putShort(columnNumber); //Column Number
        if (col.isVariableLength()) {
            if (!col.getType().isLongValue()) {
                buffer.putShort(variableOffset++);
            } else {
                buffer.putShort(longVariableOffset++);
            }
        } else {
            buffer.putShort((short) 0);
        }
        buffer.putShort(columnNumber); //Column Number again
        if (col.getType().isTextual()) {
            // this will write 4 bytes (note we don't support writing dbs which
            // use the text code page)
            writeSortOrder(buffer, col.getTextSortOrder(), creator.getFormat());
        } else {
            if (col.getType().getHasScalePrecision()) {
                buffer.put(col.getPrecision()); // numeric precision
                buffer.put(col.getScale()); // numeric scale
            } else {
                buffer.put((byte) 0x00); //unused
                buffer.put((byte) 0x00); //unused
            }
            buffer.putShort((short) 0); //Unknown
        }
        buffer.put(col.getColumnBitFlags()); // misc col flags
        if (col.isCompressedUnicode()) { //Compressed
            buffer.put((byte) 1);
        } else {
            buffer.put((byte) 0);
        }
        buffer.putInt(0); //Unknown, but always 0.
        //Offset for fixed length columns
        if (col.isVariableLength()) {
            buffer.putShort((short) 0);
        } else {
            buffer.putShort(fixedOffset);
            fixedOffset += col.getType().getFixedSize(col.getLength());
        }
        if (!col.getType().isLongValue()) {
            buffer.putShort(col.getLength()); //Column length
        } else {
            buffer.putShort((short) 0x0000); // unused
        }
        columnNumber++;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Creating new column def block\n"
                    + ByteUtil.toHexString(buffer, position, creator.getFormat().SIZE_COLUMN_DEF_BLOCK));
        }
    }
    for (Column col : columns) {
        Table.writeName(buffer, col.getName(), creator.getCharset());
    }
}

From source file:com.healthmarketscience.jackcess.impl.TableImpl.java

/**
 * @param buffer Buffer to write to//from   w  w  w. j  a v  a  2 s .  com
 * @param columns List of Columns in the table
 */
private static void writeTableDefinitionHeader(TableCreator creator, ByteBuffer buffer, int totalTableDefSize)
        throws IOException {
    List<ColumnBuilder> columns = creator.getColumns();

    //Start writing the tdef
    writeTablePageHeader(buffer);
    buffer.putInt(totalTableDefSize); //Length of table def
    buffer.putInt(MAGIC_TABLE_NUMBER); // seemingly constant magic value
    buffer.putInt(0); //Number of rows
    buffer.putInt(0); //Last Autonumber
    buffer.put((byte) 1); // this makes autonumbering work in access
    for (int i = 0; i < 15; i++) { //Unknown
        buffer.put((byte) 0);
    }
    buffer.put(TYPE_USER); //Table type
    buffer.putShort((short) columns.size()); //Max columns a row will have
    buffer.putShort(ColumnImpl.countVariableLength(columns)); //Number of variable columns in table
    buffer.putShort((short) columns.size()); //Number of columns in table
    buffer.putInt(creator.getLogicalIndexCount()); //Number of logical indexes in table
    buffer.putInt(creator.getIndexCount()); //Number of indexes in table
    buffer.put((byte) 0); //Usage map row number
    ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Usage map page number
    buffer.put((byte) 1); //Free map row number
    ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Free map page number
}

From source file:com.healthmarketscience.jackcess.impl.ColumnImpl.java

protected ByteBuffer writeFixedLengthField(Object obj, ByteBuffer buffer) throws IOException {
    // since booleans are not written by this method, it's safe to convert any
    // incoming boolean into an integer.
    obj = booleanToInteger(obj);/*w w w  . ja  v  a2  s.  c  om*/

    switch (getType()) {
    case BOOLEAN:
        //Do nothing
        break;
    case BYTE:
        buffer.put(toNumber(obj).byteValue());
        break;
    case INT:
        buffer.putShort(toNumber(obj).shortValue());
        break;
    case LONG:
        buffer.putInt(toNumber(obj).intValue());
        break;
    case MONEY:
        writeCurrencyValue(buffer, obj);
        break;
    case FLOAT:
        buffer.putFloat(toNumber(obj).floatValue());
        break;
    case DOUBLE:
        buffer.putDouble(toNumber(obj).doubleValue());
        break;
    case SHORT_DATE_TIME:
        writeDateValue(buffer, obj);
        break;
    case TEXT:
        // apparently text numeric values are also occasionally written as fixed
        // length...
        int numChars = getLengthInUnits();
        // force uncompressed encoding for fixed length text
        buffer.put(encodeTextValue(obj, numChars, numChars, true));
        break;
    case GUID:
        writeGUIDValue(buffer, obj);
        break;
    case NUMERIC:
        // yes, that's right, occasionally numeric values are written as fixed
        // length...
        writeNumericValue(buffer, obj);
        break;
    case BINARY:
    case UNKNOWN_0D:
    case UNKNOWN_11:
    case COMPLEX_TYPE:
        buffer.putInt(toNumber(obj).intValue());
        break;
    case UNSUPPORTED_FIXEDLEN:
        byte[] bytes = toByteArray(obj);
        if (bytes.length != getLength()) {
            throw new IOException(
                    "Invalid fixed size binary data, size " + getLength() + ", got " + bytes.length);
        }
        buffer.put(bytes);
        break;
    default:
        throw new IOException("Unsupported data type: " + getType());
    }
    return buffer;
}