Example usage for java.nio ByteBuffer getShort

List of usage examples for java.nio ByteBuffer getShort

Introduction

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

Prototype

public abstract short getShort(int index);

Source Link

Document

Returns the short at the specified index.

Usage

From source file:com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionAzureKeyVaultProvider.java

private short convertTwoBytesToShort(byte[] input, int index) throws SQLServerException {

    short shortVal = -1;
    if (index + 1 >= input.length) {
        throw new SQLServerException(null, SQLServerException.getErrString("R_ByteToShortConversion"), null, 0,
                false);/* w w  w  . j  a v a 2  s  . c o m*/
    }
    ByteBuffer byteBuffer = ByteBuffer.allocate(2);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    byteBuffer.put(input[index]);
    byteBuffer.put(input[index + 1]);
    shortVal = byteBuffer.getShort(0);
    return shortVal;

}

From source file:com.nordicsemi.nrfUARTv2.MainActivity.java

private short bytesToShort(byte b1, byte b2) {
    ByteBuffer bb = ByteBuffer.allocate(2);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.put(b1);//from  w w w .  j av a 2  s  . c  om
    bb.put(b2);
    short shortVal = bb.getShort(0);
    return shortVal;
}

From source file:ar.com.qbe.siniestros.model.utils.MimeMagic.MagicMatcher.java

/**
 * convert a byte array to a short/* w ww .j a v  a2 s. c  om*/
 *
 * @param data buffer of byte data
 *
 * @return byte array converted to a short
 */
private short byteArrayToShort(ByteBuffer data) {
    return data.getShort(0);
}

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

/**
 * Reads the column cade page info from the given buffer, if supported for
 * this db.//  w  w w  .j  av  a2  s.c o m
 */
static short readCodePage(ByteBuffer buffer, int offset, JetFormat format) {
    int cpOffset = format.OFFSET_COLUMN_CODE_PAGE;
    return ((cpOffset >= 0) ? buffer.getShort(offset + cpOffset) : 0);
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffReader.java

private long readHeader() throws IOException {
    ByteBuffer tiffHeader = ByteBuffer.allocate(8);
    fileChannel_.read(tiffHeader, 0);/*w  w w  . java  2s . co  m*/
    char zeroOne = tiffHeader.getChar(0);
    if (zeroOne == 0x4949) {
        byteOrder_ = ByteOrder.LITTLE_ENDIAN;
    } else if (zeroOne == 0x4d4d) {
        byteOrder_ = ByteOrder.BIG_ENDIAN;
    } else {
        throw new IOException("Error reading Tiff header");
    }
    tiffHeader.order(byteOrder_);
    short twoThree = tiffHeader.getShort(2);
    if (twoThree != 42) {
        throw new IOException("Tiff identifier code incorrect");
    }
    return unsignInt(tiffHeader.getInt(4));
}

From source file:guru.benson.pinch.Pinch.java

/**
 * Get a {@link java.net.HttpURLConnection} that has its {@link java.io.InputStream} pointing at
 * the file data of the given {@link guru.benson.pinch.ExtendedZipEntry}.
 *
 * @throws IOException// ww w. j  a  v a  2  s .c om
 */
private HttpURLConnection getEntryInputStream(ExtendedZipEntry entry) throws IOException {
    HttpURLConnection conn;
    InputStream is;

    // Define the local header range
    long start = entry.getOffset();
    long end = start + ZipConstants.LOCHDR;

    conn = openConnection();
    conn.setRequestProperty("Range", "bytes=" + start + "-" + end);
    conn.setInstanceFollowRedirects(true);
    conn.connect();

    int responseCode = conn.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_PARTIAL) {
        throw new IOException("Unexpected HTTP server response: " + responseCode);
    }

    byte[] dataBuffer = new byte[2048];
    int read, bytes = 0;

    is = conn.getInputStream();
    while ((read = is.read(dataBuffer)) != -1) {
        bytes += read;
    }
    close(is);
    disconnect(conn);
    if (bytes < ZipConstants.LOCHDR) {
        throw new IOException("Unable to fetch the local header");
    }

    ByteBuffer buffer = ByteBuffer.allocate(ZipConstants.LOCHDR);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put(dataBuffer, 0, ZipConstants.LOCHDR);

    final int headerSignature = buffer.getInt(0);
    if (headerSignature != 0x04034b50) {
        disconnect(conn);
        throw new IOException("Local file header signature mismatch");
    }

    final int localCompressedSize = buffer.getInt(ZipConstants.LOCSIZ);
    final short localFileNameLength = buffer.getShort(ZipConstants.LOCNAM);
    final short localExtraLength = buffer.getShort(ZipConstants.LOCEXT);

    // Define the local file range
    start = entry.getOffset() + ZipConstants.LOCHDR + localFileNameLength + localExtraLength;
    end = start + localCompressedSize;

    // Open a new one with
    conn = openConnection();
    conn.setRequestProperty("Range", "bytes=" + start + "-" + end);
    conn.setInstanceFollowRedirects(true);
    conn.connect();

    responseCode = conn.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_PARTIAL) {
        disconnect(conn);
        close(is);
        throw new IOException("Unexpected HTTP server response: " + responseCode);
    }

    return conn;
}

From source file:spade.reporter.CDM.java

/**
 * Return null if null arguments//from   w w  w .  ja  va  2s  . c o  m
 * 
 * @param permission
 * @return null/Octal representation of permissions
 */
private String getPermissionSHORTAsString(SHORT permission) {
    if (permission == null) {
        return null;
    } else {
        ByteBuffer bb = ByteBuffer.allocate(2);
        bb.put(permission.bytes()[0]);
        bb.put(permission.bytes()[1]);
        int permissionShort = bb.getShort(0);
        return Integer.toOctalString(permissionShort);
    }
}

From source file:com.offbynull.portmapper.natpmp.NatPmpReceiver.java

/**
 * Start listening for NAT-PMP events. This method blocks until {@link #stop() } is called.
 * @param listener listener to notify of events
 * @throws IOException if socket error occurs
 * @throws NullPointerException if any argument is {@code null}
 *//*  w  w  w.j  a v a2  s  . com*/
public void start(NatPmpEventListener listener) throws IOException {
    Validate.notNull(listener);

    MulticastSocket socket = null;
    try {
        final InetAddress group = InetAddress.getByName("224.0.0.1"); // NOPMD
        final int port = 5350;
        final InetSocketAddress groupAddress = new InetSocketAddress(group, port);

        socket = new MulticastSocket(port);

        if (!currentSocket.compareAndSet(null, socket)) {
            IOUtils.closeQuietly(socket);
            return;
        }

        socket.setReuseAddress(true);

        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();
            Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
            while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface
                InetAddress addr = addrs.nextElement();

                try {
                    if (addr instanceof Inet4Address) {
                        socket.joinGroup(groupAddress, networkInterface);
                    }
                } catch (IOException ioe) { // NOPMD
                    // occurs with certain interfaces
                    // do nothing
                }
            }
        }

        ByteBuffer buffer = ByteBuffer.allocate(12);
        DatagramPacket data = new DatagramPacket(buffer.array(), buffer.capacity());

        while (true) {
            buffer.clear();
            socket.receive(data);
            buffer.position(data.getLength());
            buffer.flip();

            if (!data.getAddress().equals(gatewayAddress)) { // data isn't from our gateway, ignore
                continue;
            }

            if (buffer.remaining() != 12) { // data isn't the expected size, ignore
                continue;
            }

            int version = buffer.get(0);
            if (version != 0) { // data doesn't have the correct version, ignore
                continue;
            }

            int opcode = buffer.get(1) & 0xFF;
            if (opcode != 128) { // data doesn't have the correct op, ignore
                continue;
            }

            int resultCode = buffer.getShort(2) & 0xFFFF;
            switch (resultCode) {
            case 0:
                break;
            default:
                continue; // data doesn't have a successful result, ignore
            }

            listener.publicAddressUpdated(new ExternalAddressNatPmpResponse(buffer));
        }

    } catch (IOException ioe) {
        if (currentSocket.get() == null) {
            return; // ioexception caused by interruption/stop, so just return without propogating error up
        }

        throw ioe;
    } finally {
        IOUtils.closeQuietly(socket);
        currentSocket.set(null);
    }
}

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

/**
 * Reads the sort order info from the given buffer from the given position.
 *//*from   w  w  w.j  a  v a2 s  . c om*/
static SortOrder readSortOrder(ByteBuffer buffer, int position, JetFormat format) {
    short value = buffer.getShort(position);
    byte version = 0;
    if (format.SIZE_SORT_ORDER == 4) {
        version = buffer.get(position + 3);
    }

    if (value == 0) {
        // probably a file we wrote, before handling sort order
        return format.DEFAULT_SORT_ORDER;
    }

    if (value == GENERAL_SORT_ORDER_VALUE) {
        if (version == GENERAL_LEGACY_SORT_ORDER.getVersion()) {
            return GENERAL_LEGACY_SORT_ORDER;
        }
        if (version == GENERAL_SORT_ORDER.getVersion()) {
            return GENERAL_SORT_ORDER;
        }
    }
    return new SortOrder(value, version);
}

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

/**
 * Returns the row count for the current page.  If the page is invalid
 * ({@code null}) or the page is not a DATA page, 0 is returned.
 */// ww w  .j ava2s.c o m
private static int getRowsOnDataPage(ByteBuffer rowBuffer, JetFormat format) throws IOException {
    int rowsOnPage = 0;
    if ((rowBuffer != null) && (rowBuffer.get(0) == PageTypes.DATA)) {
        rowsOnPage = rowBuffer.getShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE);
    }
    return rowsOnPage;
}