List of usage examples for java.nio ByteBuffer getShort
public abstract short getShort(int index);
From source file:org.onlab.packet.ONLabLddp.java
/** * Checks if packet has size of OVX-generated LLDP, and correctness of two * organizationally specific TLVs that use ON.Lab's OUI. Assumes packet is * valid LLDP packet// www.ja v a 2 s . c o m * * @param packet packet data * @return eth type or -1 */ public static short isOVXLLDP(byte[] packet) { if (packet.length < OVX_LLDP_SIZE) { return -1; } // Extra offset due to VLAN tag final ByteBuffer bb = ByteBuffer.wrap(packet); int offset = 0; short ethType = bb.getShort(ETHERTYPE_OFFSET); if (ethType != Ethernet.TYPE_LLDP && ethType != Ethernet.TYPE_BSN) { offset = 4; ethType = bb.getShort(ETHERTYPE_OFFSET + offset); if (ethType != Ethernet.TYPE_LLDP && ethType != Ethernet.TYPE_BSN) { return -1; } } // Compare packet's organizationally specific TLVs to the expected // values for (int i = 0; i < OUI_TLV.length; i++) { if (packet[NAME_TLV_OFFSET + offset + i] != OUI_TLV[i]) { return -1; } } return ethType; }
From source file:net.onrc.openvirtex.packet.OVXLLDP.java
/** * Extracts dpid and port from OVX-generated LLDP packet. * * @param packet/*from w ww. j a v a2 s .c om*/ * @return Dpid and port */ public static DPIDandPort parseLLDP(final byte[] packet) { final ByteBuffer bb = ByteBuffer.wrap(packet); // Extra offset due to VLAN tag int offset = 0; if (bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_LLDP && bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_BSN) { offset = 4; } final short port = bb.getShort(PORT_OFFSET + offset); final long dpid = bb.getLong(DPID_OFFSET + offset); return new DPIDandPort(dpid, port); }
From source file:org.onlab.packet.ONLabLddp.java
/** * Extracts dpid and port from OVX-generated LLDP packet. * * @param packet packet data// w w w . jav a2s.co m * @return Dpid and port */ public static DPIDandPort parseLLDP(final byte[] packet) { final ByteBuffer bb = ByteBuffer.wrap(packet); // Extra offset due to VLAN tag int offset = 0; if (bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_LLDP && bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_BSN) { offset = 4; } final int port = bb.getInt(PORT_OFFSET + offset); final long dpid = bb.getLong(DPID_OFFSET + offset); return new DPIDandPort(dpid, port); }
From source file:com.linkedin.databus.core.DbusEventPart.java
/** * Decodes a bytebuffer and returns DbusEventPart. Preserves the ByteBuffer position. * @param buf/*from ww w .j a v a 2 s . c o m*/ * @return */ public static DbusEventPart decode(ByteBuffer buf) { int pos = buf.position(); int dataLen = buf.getInt(pos); if (dataLen < 0) { throw new UnsupportedOperationException("Data length " + dataLen + " not supported"); } short attributes = buf.getShort(pos + AttributesOffset); short schemaVersion = (short) (attributes >> VERSION_SHIFT); SchemaDigestType schemaDigestType = digestType(attributes); int digestLen = digestLen(schemaDigestType); byte[] digest = new byte[digestLen]; for (int i = 0; i < digestLen; i++) { digest[i] = buf.get(pos + AttributesOffset + AttributesLen + i); } // NOTE - this will create a new ByteBuffer object pointing to the // same memory. So the position of this new BB is beginning of the data // and limit is set to the end of the data. ByteBuffer dataBuf = buf.asReadOnlyBuffer(); dataBuf.position(pos + AttributesOffset + AttributesLen + digestLen); dataBuf.limit(dataBuf.position() + dataLen); return new DbusEventPart(schemaDigestType, digest, schemaVersion, dataBuf); }
From source file:jsave.Utils.java
/** * Returns an integer.//from w w w.ja v a2 s.c om * * Since java does not provide unsigned primitive types, each unsigned * value read from the buffer is promoted up to the next bigger primitive * data type : getUnsignedShort() returns an int. * * @param bb the array of bytes in the buffer * @return an integer */ public static int getUnsignedShort(ByteBuffer bb) { return (bb.getShort(0) & 0xffff); }
From source file:gridool.memcached.gateway.BinaryCommandProxy.java
private static void xferResponse(final byte opcode, final SocketChannel src, final Channel dst, final String key) throws IOException { ByteBuffer headerBuf = ByteBuffer.allocate(BinaryProtocol.HEADER_LENGTH); int headerRead = NIOUtils.readFully(src, headerBuf, BinaryProtocol.HEADER_LENGTH); assert (headerRead == BinaryProtocol.HEADER_LENGTH) : headerRead; headerBuf.flip();/* ww w . j ava 2 s . c o m*/ if (BinaryProtocol.surpressSuccessResponse(opcode)) { // piggyback will never happens final short status = headerBuf.getShort(6); if (status == 0) { return; } } ChannelBuffer res; int totalBody = headerBuf.getInt(8); if (totalBody > 0) { ByteBuffer bodyBuf = ByteBuffer.allocate(totalBody); int bodyRead = NIOUtils.readFully(src, bodyBuf, totalBody); assert (bodyRead == totalBody) : "bodyRead (" + bodyRead + ") != totalBody (" + totalBody + ")"; bodyBuf.flip(); res = ChannelBuffers.wrappedBuffer(headerBuf, bodyBuf); } else { res = ChannelBuffers.wrappedBuffer(headerBuf); } String opname = BinaryProtocol.resolveName(headerBuf.get(1)); if (LOG.isDebugEnabled()) { Header header = new Header(); header.decode(headerBuf); LOG.debug( "Start sending memcached response [" + opname + "] " + res.readableBytes() + " bytes for key '" + key + "'\n" + header + '\n' + Arrays.toString(res.toByteBuffer().array())); } dst.write(res).addListener(new VerboseListener("sendResponse [" + opname + "] for key: " + key)); }
From source file:net.onrc.openvirtex.packet.OVXLLDP.java
/** * Checks if LLDP packet has correct size, LLDP multicast address, and * ethertype. Packet assumed to have Ethernet header. * * @param packet// ww w . j a va 2 s . c o m * @return true if packet is LLDP, false otherwise */ public static boolean isLLDP(final byte[] packet) { // Does packet exist and does it have the mininum size? if (packet == null || packet.length < MINIMUM_LLDP_SIZE) { return false; } // Packet has LLDP multicast destination address? final ByteBuffer bb = ByteBuffer.wrap(packet); final byte[] dst = new byte[6]; bb.get(dst); if (!(Arrays.equals(dst, OVXLLDP.LLDP_NICIRA) || Arrays.equals(dst, OVXLLDP.LLDP_MULTICAST) || Arrays.equals(dst, OVXLLDP.BDDP_MULTICAST))) { return false; } // Fetch ethertype, skip VLAN tag if it's there short etherType = bb.getShort(ETHERTYPE_OFFSET); if (etherType == ETHERTYPE_VLAN) { etherType = bb.getShort(ETHERTYPE_OFFSET + 4); } // Check ethertype if (etherType == Ethernet.TYPE_LLDP) { return true; } if (etherType == Ethernet.TYPE_BSN) { return true; } return false; }
From source file:org.onlab.packet.ONLabLddp.java
/** * Checks if LLDP packet has correct size, LLDP multicast address, and * ethertype. Packet assumed to have Ethernet header. * * @param packet packet data//from ww w . j a v a 2s . c o m * @return true if packet is LLDP, false otherwise */ public static boolean isLLDP(final byte[] packet) { // Does packet exist and does it have the mininum size? if (packet == null || packet.length < MINIMUM_LLDP_SIZE) { return false; } // Packet has LLDP multicast destination address? final ByteBuffer bb = ByteBuffer.wrap(packet); final byte[] dst = new byte[6]; bb.get(dst); if (!(Arrays.equals(dst, ONLabLddp.LLDP_NICIRA) || Arrays.equals(dst, ONLabLddp.LLDP_MULTICAST) || Arrays.equals(dst, ONLabLddp.BDDP_MULTICAST))) { return false; } // Fetch ethertype, skip VLAN tag if it's there short etherType = bb.getShort(ETHERTYPE_OFFSET); if (etherType == ETHERTYPE_VLAN) { etherType = bb.getShort(ETHERTYPE_OFFSET + 4); } // Check ethertype if (etherType == Ethernet.TYPE_LLDP) { return true; } if (etherType == Ethernet.TYPE_BSN) { return true; } return false; }
From source file:guru.benson.pinch.Pinch.java
/** * Extract all ZipEntries from the ZIP central directory. * * @param buf//from ww w . j ava 2 s . co m * The byte buffer containing the ZIP central directory. * * @return A list with all ZipEntries. */ private static ArrayList<ExtendedZipEntry> parseHeaders(ByteBuffer buf) { ArrayList<ExtendedZipEntry> zeList = new ArrayList<ExtendedZipEntry>(); buf.order(ByteOrder.LITTLE_ENDIAN); int offset = 0; while (offset < buf.limit() - ZipConstants.CENHDR) { short fileNameLen = buf.getShort(offset + ZipConstants.CENNAM); short extraFieldLen = buf.getShort(offset + ZipConstants.CENEXT); short fileCommentLen = buf.getShort(offset + ZipConstants.CENCOM); String fileName = new String(buf.array(), offset + ZipConstants.CENHDR, fileNameLen); ExtendedZipEntry zeGermans = new ExtendedZipEntry(fileName); zeGermans.setMethod(buf.getShort(offset + ZipConstants.CENHOW)); CRC32 crc = new CRC32(); crc.update(buf.getInt(offset + ZipConstants.CENCRC)); zeGermans.setCrc(crc.getValue()); zeGermans.setCompressedSize(buf.getInt(offset + ZipConstants.CENSIZ)); zeGermans.setSize(buf.getInt(offset + ZipConstants.CENLEN)); zeGermans.setInternalAttr(buf.getShort(offset + ZipConstants.CENATT)); zeGermans.setExternalAttr(buf.getShort(offset + ZipConstants.CENATX)); zeGermans.setOffset((long) buf.getInt(offset + ZipConstants.CENOFF)); zeGermans.setExtraLength(extraFieldLen); zeList.add(zeGermans); offset += ZipConstants.CENHDR + fileNameLen + extraFieldLen + fileCommentLen; } return zeList; }
From source file:edu.umn.cs.spatialHadoop.nasa.HDFRecordReader.java
/** * Find runs of true values in each row of the given 2D array of value. * @param values All the short values stored in a {@link ByteBuffer} * @param fillValue The marker that marks fillValue * @return An array of runs as one for each row in the given array. *///w w w . j a v a 2 s . c o m static ShortArray[] findTrueRuns(ByteBuffer values, short fillValue) { int resolution = (int) Math.sqrt(values.limit() / 2); ShortArray[] trueRuns = new ShortArray[resolution]; for (short row = 0; row < resolution; row++) { trueRuns[row] = new ShortArray(); // A flag that is set to true if currently inside a run of fillValues. boolean insideFillValue = true; for (short col = 0; col < resolution; col++) { if ((values.getShort((row * resolution + col) * 2) == fillValue) ^ insideFillValue) { // Found a flip between true and fill values. if (!insideFillValue && col != 0) trueRuns[row].append((short) (col - 1)); else trueRuns[row].append(col); insideFillValue = !insideFillValue; } } if (!insideFillValue) trueRuns[row].append((short) (resolution - 1)); } return trueRuns; }