List of usage examples for java.nio ByteBuffer putShort
public abstract ByteBuffer putShort(short value);
From source file:org.opendaylight.lispflowmapping.lisp.serializer.MappingRecordSerializer.java
public void serialize(ByteBuffer replyBuffer, MappingRecord record) { replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl())); if (record.getLocatorRecord() != null) { replyBuffer.put((byte) record.getLocatorRecord().size()); } else {/*from www . ja v a 2s .co m*/ replyBuffer.put((byte) 0); } if (record.getEid() != null && MaskUtil.getMaskForAddress(record.getEid().getAddress()) != -1) { replyBuffer.put((byte) NumberUtil.asShort(MaskUtil.getMaskForAddress(record.getEid().getAddress()))); } else { replyBuffer.put((byte) 0); } Action act = Action.NoAction; if (record.getAction() != null) { act = record.getAction(); } replyBuffer.put((byte) ((act.getIntValue() << 5) | // ByteUtil.boolToBit(BooleanUtils.isTrue(record.isAuthoritative()), Flags.AUTHORITATIVE))); replyBuffer.position(replyBuffer.position() + Length.RESERVED); replyBuffer.putShort(NumberUtil.asShort(record.getMapVersion())); if (record.getEid() != null && record.getEid().getAddress() != null) { LispAddressSerializer.getInstance().serialize(replyBuffer, record.getEid()); } if (record.getLocatorRecord() != null) { for (LocatorRecord locatorRecord : record.getLocatorRecord()) { LocatorRecordSerializer.getInstance().serialize(replyBuffer, locatorRecord); } } }
From source file:org.deegree.tools.rendering.dem.builder.DEMDatasetGenerator.java
private void put(ByteBuffer buffer, float value, int bytesPerValue) { if (bytesPerValue == 4) { buffer.putFloat(value);/*from ww w . ja v a 2 s . c o m*/ } else if (bytesPerValue == 2) { short s = 0; if (value < 1) { s = (short) (32768.0f * value); } else { s = (short) (32767.0f * value); } buffer.putShort(s); } else if (bytesPerValue == 1) { byte b = 0; if (value < 1) { b = (byte) (128.0f * value); } else { b = (byte) (127.0f * value); } buffer.put(b); } }
From source file:org.springframework.integration.x.ip.websocket.WebSocketSerializer.java
@Override public void serialize(final Object frame, OutputStream outputStream) throws IOException { String data = ""; WebSocketFrame theFrame = null;//from w ww . j a v a 2 s. c o m if (frame instanceof String) { data = (String) frame; theFrame = new WebSocketFrame(WebSocketFrame.TYPE_DATA, data); } else if (frame instanceof WebSocketFrame) { theFrame = (WebSocketFrame) frame; data = theFrame.getPayload(); } if (data != null && data.startsWith("HTTP/1.1")) { outputStream.write(data.getBytes()); return; } int lenBytes; int payloadLen = this.server ? 0 : 0x80; //masked boolean close = theFrame.getType() == WebSocketFrame.TYPE_CLOSE; boolean ping = theFrame.getType() == WebSocketFrame.TYPE_PING; boolean pong = theFrame.getType() == WebSocketFrame.TYPE_PONG; byte[] bytes = theFrame.getBinary() != null ? theFrame.getBinary() : data.getBytes("UTF-8"); int length = bytes.length; if (close) { length += 2; } if (length >= Math.pow(2, 16)) { lenBytes = 8; payloadLen |= 127; } else if (length > 125) { lenBytes = 2; payloadLen |= 126; } else { lenBytes = 0; payloadLen |= length; } int mask = (int) System.currentTimeMillis(); ByteBuffer buffer = ByteBuffer.allocate(length + 6 + lenBytes); if (ping) { buffer.put((byte) 0x89); } else if (pong) { buffer.put((byte) 0x8a); } else if (close) { buffer.put((byte) 0x88); } else if (theFrame.getType() == WebSocketFrame.TYPE_DATA_BINARY) { buffer.put((byte) 0x82); } else { // Final fragment; text buffer.put((byte) 0x81); } buffer.put((byte) payloadLen); if (lenBytes == 2) { buffer.putShort((short) length); } else if (lenBytes == 8) { buffer.putLong(length); } byte[] maskBytes = new byte[4]; if (!server) { buffer.putInt(mask); buffer.position(buffer.position() - 4); buffer.get(maskBytes); } if (close) { buffer.putShort(theFrame.getStatus()); // TODO: mask status when client } for (int i = 0; i < bytes.length; i++) { if (server) { buffer.put(bytes[i]); } else { buffer.put((byte) (bytes[i] ^ maskBytes[i % 4])); } } outputStream.write(buffer.array(), 0, buffer.position()); }
From source file:org.apache.carbondata.core.indexstore.blockletindex.BlockDataMap.java
private byte[] convertRowCountFromShortToByteArray(List<Short> blockletCountInEachBlock) { int bufferSize = blockletCountInEachBlock.size() * 2; ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize); for (Short blockletCount : blockletCountInEachBlock) { byteBuffer.putShort(blockletCount); }//from ww w. ja v a 2 s. c o m byteBuffer.rewind(); return byteBuffer.array(); }
From source file:com.warfrog.bitmapallthethings.BattEngine.java
private InputStream generateBitmapHeader(int width, int height, int fileSize, int fillerBytes) { ByteBuffer buffer = ByteBuffer.allocate(54); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.put((byte) 0x42); //B buffer.put((byte) 0x4D); //M buffer.putInt(fileSize + 54); //total file size buffer.putInt(fileSize); //unofficial -- used to save the file size buffer.putInt(54); //pixel info offset buffer.putInt(40); //size of the bitmap info header buffer.putInt(width); //width buffer.putInt(height); //height buffer.putShort((short) 1); //number of color planes buffer.putShort((short) getBytesPerPixel()); //bytes per pixel buffer.putInt(0); //no compression buffer.putInt(fileSize); //size of the raw pixel array buffer.putInt(2835); //horizontal resolution buffer.putInt(2835); //vertical resolution buffer.putInt(0); //number of colors buffer.putInt(0); //important colors return new ByteArrayInputStream(buffer.array()); }
From source file:org.openpilot_nonag.uavtalk.UAVTalk.java
/** * Send an object through the telemetry link. * @throws IOException/*from w ww . j a va 2 s.c o m*/ * @param[in] obj Object handle to send * @param[in] type Transaction type \return Success (true), Failure (false) */ private boolean transmitSingleObject(int type, long objId, long instId, UAVObject obj) throws IOException { int length = 0; assert (objMngr != null && outStream != null); // IMPORTANT : obj can be null (when type is NACK for example) // Determine data length if (type == TYPE_OBJ_REQ || type == TYPE_ACK || type == TYPE_NACK) { length = 0; } else { length = obj.getNumBytes(); } ByteBuffer bbuf = ByteBuffer.allocate(MAX_PACKET_LENGTH); bbuf.order(ByteOrder.LITTLE_ENDIAN); // Setup type and object id fields bbuf.put((byte) (SYNC_VAL & 0xff)); bbuf.put((byte) (type & 0xff)); bbuf.putShort((short) (length + HEADER_LENGTH)); bbuf.putInt((int) objId); bbuf.putShort((short) (instId & 0xffff)); // Check length if (length >= MAX_PAYLOAD_LENGTH) { ++stats.txErrors; return false; } // Copy data (if any) if (length > 0) try { if (obj.pack(bbuf) == 0) { ++stats.txErrors; return false; } } catch (Exception e) { ++stats.txErrors; // TODO Auto-generated catch block e.printStackTrace(); return false; } // Calculate checksum bbuf.put((byte) (updateCRC(0, bbuf.array(), bbuf.position()) & 0xff)); int packlen = bbuf.position(); bbuf.position(0); byte[] dst = new byte[packlen]; bbuf.get(dst, 0, packlen); outStream.write(dst); // Update stats ++stats.txObjects; stats.txBytes += bbuf.position(); stats.txObjectBytes += length; // Done return true; }
From source file:com.healthmarketscience.jackcess.Column.java
/** * Writes the sort order info to the given buffer at the current position. *///from w ww .j a v a 2 s . co m private static void writeSortOrder(ByteBuffer buffer, SortOrder sortOrder, JetFormat format) { if (sortOrder == null) { sortOrder = format.DEFAULT_SORT_ORDER; } buffer.putShort(sortOrder.getValue()); if (format.SIZE_SORT_ORDER == 4) { buffer.put((byte) 0x00); // unknown buffer.put(sortOrder.getVersion()); } }
From source file:com.healthmarketscience.jackcess.Table.java
/** * Writes the given name into the given buffer in the format as expected by * {@link #readName}./*from w w w .j a v a 2s .c om*/ */ static void writeName(ByteBuffer buffer, String name, Charset charset) { ByteBuffer encName = Column.encodeUncompressedText(name, charset); buffer.putShort((short) encName.remaining()); buffer.put(encName); }
From source file:com.healthmarketscience.jackcess.Table.java
/** * Create the usage map definition page buffer. The "used pages" map is in * row 0, the "pages with free space" map is in row 1. Index usage maps are * in subsequent rows./*w w w. j a va 2 s . com*/ */ private static void createUsageMapDefinitionBuffer(TableCreator creator) throws IOException { // 2 table usage maps plus 1 for each index int umapNum = 2 + creator.getIndexCount(); JetFormat format = creator.getFormat(); int usageMapRowLength = format.OFFSET_USAGE_MAP_START + format.USAGE_MAP_TABLE_BYTE_LENGTH; int freeSpace = format.DATA_PAGE_INITIAL_FREE_SPACE - (umapNum * getRowSpaceUsage(usageMapRowLength, format)); // for now, don't handle writing that many indexes if (freeSpace < 0) { throw new IOException("FIXME attempting to write too many indexes"); } int umapPageNumber = creator.getUmapPageNumber(); PageChannel pageChannel = creator.getPageChannel(); ByteBuffer rtn = pageChannel.createPageBuffer(); rtn.put(PageTypes.DATA); rtn.put((byte) 0x1); //Unknown rtn.putShort((short) freeSpace); //Free space in page rtn.putInt(0); //Table definition rtn.putInt(0); //Unknown rtn.putShort((short) umapNum); //Number of records on this page // write two rows of usage map definitions for the table int rowStart = findRowEnd(rtn, 0, format) - usageMapRowLength; for (int i = 0; i < 2; ++i) { rtn.putShort(getRowStartOffset(i, format), (short) rowStart); if (i == 0) { // initial "usage pages" map definition rtn.put(rowStart, UsageMap.MAP_TYPE_REFERENCE); } else { // initial "pages with free space" map definition rtn.put(rowStart, UsageMap.MAP_TYPE_INLINE); } rowStart -= usageMapRowLength; } if (creator.hasIndexes()) { for (int i = 0; i < creator.getIndexes().size(); ++i) { IndexBuilder idx = creator.getIndexes().get(i); // allocate root page for the index int rootPageNumber = pageChannel.allocateNewPage(); int umapRowNum = i + 2; // stash info for later use TableCreator.IndexState idxState = creator.getIndexState(idx); idxState.setRootPageNumber(rootPageNumber); idxState.setUmapRowNumber((byte) umapRowNum); idxState.setUmapPageNumber(umapPageNumber); // index map definition, including initial root page rtn.putShort(getRowStartOffset(umapRowNum, format), (short) rowStart); rtn.put(rowStart, UsageMap.MAP_TYPE_INLINE); rtn.putInt(rowStart + 1, rootPageNumber); rtn.put(rowStart + 5, (byte) 1); rowStart -= usageMapRowLength; } } pageChannel.writePage(rtn, umapPageNumber); }
From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java
private byte[] getIPv6ExtensionPortMatchMsg(short port) { ByteBuffer ipv6ext_port_msg = ByteBuffer.allocate(6); int nxm_header = getIPv6ExtensionMatchHeader(Extension_Types.OF_10, OF_Match_Types.MATCH_OF_IN_PORT.getValue(), 0, 2); ipv6ext_port_msg.putInt(nxm_header); ipv6ext_port_msg.putShort(port); return (ipv6ext_port_msg.array()); }