List of usage examples for java.nio ByteBuffer getShort
public abstract short getShort(int index);
From source file:Main.java
public static void main(String[] args) { ByteBuffer bb = ByteBuffer.allocate(BSIZE); bb.asShortBuffer().put((short) 471142); System.out.println(bb.getShort(0)); }
From source file:Main.java
public static int getUnsignedShort(ByteBuffer bb, int position) { return (bb.getShort(position) & 0xffff); }
From source file:Main.java
public static int getUnsignedShort(final ByteBuffer pByteBuffer, final int pPosition) { return pByteBuffer.getShort(pPosition) & 0xFFFF; }
From source file:Main.java
public static int byteArrayToShort(byte[] bytes, int offset) { ByteBuffer bb = ByteBuffer.wrap(bytes); bb.order(ByteOrder.LITTLE_ENDIAN); return bb.getShort(offset); }
From source file:Main.java
private static short stream2Short(byte[] stream, int offset) { ByteBuffer buffer = ByteBuffer.allocate(2); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.put(stream[offset]);//from www. j a v a 2 s. c o m buffer.put(stream[offset + 1]); return buffer.getShort(0); }
From source file:org.bimserver.utils.BinUtils.java
public static short readShort(byte[] value, int index) { ByteBuffer byteBuffer = ByteBuffer.wrap(value); return byteBuffer.getShort(index); }
From source file:com.linkedin.databus.core.DbusEventPart.java
/** * @return the length of the DbusEventPart that is encoded in 'buf' at position 'position'. * Callers can use this method to advance across the DbusEventPart in a serialized V2 event. *//* ww w .j av a 2 s. co m*/ public static int partLength(ByteBuffer buf, int position) { DbusEvent.SchemaDigestType digestType = digestType(buf.getShort(position + AttributesOffset)); int digestLen = digestLen(digestType); return AttributesOffset + AttributesLen + digestLen + buf.getInt(position); }
From source file:com.linkedin.databus.core.DbusEventPart.java
/** * Replace the schema-digest in a serialized DbusEventPart. * * @param buf The buffer that contains the serialized DbusEventPart. * @param position the position in the buffer where the DbusEventPart starts. * @param schemaDigest The digest value to substitute. The value must match in length to the existing value. *//* ww w.j a v a2 s . c om*/ public static void replaceSchemaDigest(ByteBuffer buf, int position, byte[] schemaDigest) { DbusEvent.SchemaDigestType digestType = digestType(buf.getShort(position + AttributesOffset)); int digestLen = digestLen(digestType); if (schemaDigest.length != digestLen) { throw new RuntimeException( "Expecting length " + digestLen + " for type " + digestType + ", found " + schemaDigest.length); } for (int i = 0; i < digestLen; i++) { buf.put(position + AttributesOffset + AttributesLen + i, schemaDigest[i]); } }
From source file:Main.java
public static long getCommentLength(final FileChannel fileChannel) throws IOException { // End of central directory record (EOCD) // Offset Bytes Description[23] // 0 4 End of central directory signature = 0x06054b50 // 4 2 Number of this disk // 6 2 Disk where central directory starts // 8 2 Number of central directory records on this disk // 10 2 Total number of central directory records // 12 4 Size of central directory (bytes) // 16 4 Offset of start of central directory, relative to start of archive // 20 2 Comment length (n) // 22 n Comment // For a zip with no archive comment, the // end-of-central-directory record will be 22 bytes long, so // we expect to find the EOCD marker 22 bytes from the end. final long archiveSize = fileChannel.size(); if (archiveSize < ZIP_EOCD_REC_MIN_SIZE) { throw new IOException("APK too small for ZIP End of Central Directory (EOCD) record"); }// w w w . j ava2 s .co m // ZIP End of Central Directory (EOCD) record is located at the very end of the ZIP archive. // The record can be identified by its 4-byte signature/magic which is located at the very // beginning of the record. A complication is that the record is variable-length because of // the comment field. // The algorithm for locating the ZIP EOCD record is as follows. We search backwards from // end of the buffer for the EOCD record signature. Whenever we find a signature, we check // the candidate record's comment length is such that the remainder of the record takes up // exactly the remaining bytes in the buffer. The search is bounded because the maximum // size of the comment field is 65535 bytes because the field is an unsigned 16-bit number. final long maxCommentLength = Math.min(archiveSize - ZIP_EOCD_REC_MIN_SIZE, UINT16_MAX_VALUE); final long eocdWithEmptyCommentStartPosition = archiveSize - ZIP_EOCD_REC_MIN_SIZE; for (int expectedCommentLength = 0; expectedCommentLength <= maxCommentLength; expectedCommentLength++) { final long eocdStartPos = eocdWithEmptyCommentStartPosition - expectedCommentLength; final ByteBuffer byteBuffer = ByteBuffer.allocate(4); fileChannel.position(eocdStartPos); fileChannel.read(byteBuffer); byteBuffer.order(ByteOrder.LITTLE_ENDIAN); if (byteBuffer.getInt(0) == ZIP_EOCD_REC_SIG) { final ByteBuffer commentLengthByteBuffer = ByteBuffer.allocate(2); fileChannel.position(eocdStartPos + ZIP_EOCD_COMMENT_LENGTH_FIELD_OFFSET); fileChannel.read(commentLengthByteBuffer); commentLengthByteBuffer.order(ByteOrder.LITTLE_ENDIAN); final int actualCommentLength = commentLengthByteBuffer.getShort(0); if (actualCommentLength == expectedCommentLength) { return actualCommentLength; } } } throw new IOException("ZIP End of Central Directory (EOCD) record not found"); }
From source file:net.onrc.openvirtex.packet.OVXLLDP.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// ww w. ja va 2s .c om * * @param packet * @return */ public static boolean isOVXLLDP(byte[] packet) { if (packet.length < OVX_LLDP_SIZE) { return false; } // Extra offset due to VLAN tag final ByteBuffer bb = ByteBuffer.wrap(packet); int offset = 0; if (bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_LLDP && bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_BSN) { offset = 4; } // 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 false; } } return true; }