Example usage for java.lang Short reverseBytes

List of usage examples for java.lang Short reverseBytes

Introduction

In this page you can find the example usage for java.lang Short reverseBytes.

Prototype

@HotSpotIntrinsicCandidate
public static short reverseBytes(short i) 

Source Link

Document

Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified short value.

Usage

From source file:Main.java

public static void main(String[] args) {
    short shortNum = 50;

    short shortValue = Short.reverseBytes(shortNum);

    System.out.println("The reversed value is = " + shortValue);
}

From source file:Main.java

public static short readShort(DataInputStream is) throws IOException {
    return Short.reverseBytes(is.readShort());
}

From source file:Main.java

public static void writeShort(DataOutputStream os, short s) throws IOException {
    os.writeShort(Short.reverseBytes(s));
}

From source file:org.apache.hadoop.hbase.util.UnsafeAccess.java

/**
 * Converts a byte array to a short value considering it was written in big-endian format.
 * @param bytes byte array/*  w ww. jav a2 s. c  o m*/
 * @param offset offset into array
 * @return the short value
 */
public static short toShort(byte[] bytes, int offset) {
    if (littleEndian) {
        return Short.reverseBytes(theUnsafe.getShort(bytes, offset + BYTE_ARRAY_BASE_OFFSET));
    } else {
        return theUnsafe.getShort(bytes, offset + BYTE_ARRAY_BASE_OFFSET);
    }
}

From source file:org.apache.hadoop.hbase.util.UnsafeAccess.java

/**
 * Put a short value out to the specified byte array position in big-endian format.
 * @param bytes the byte array//from  ww  w. j  a va  2  s  .c  om
 * @param offset position in the array
 * @param val short to write out
 * @return incremented offset
 */
public static int putShort(byte[] bytes, int offset, short val) {
    if (littleEndian) {
        val = Short.reverseBytes(val);
    }
    theUnsafe.putShort(bytes, offset + BYTE_ARRAY_BASE_OFFSET, val);
    return offset + Bytes.SIZEOF_SHORT;
}

From source file:fr.noop.subtitle.stl.StlParser.java

private StlGsi readGsi(DataInputStream dis) throws IOException, InvalidTimeRangeException {
    // Read and extract metadata from GSI block
    // GSI block is 1024 bytes long
    StlGsi gsi = new StlGsi();

    // Read Code Page Number (CPN)
    byte[] cpnBytes = new byte[3];
    dis.readFully(cpnBytes, 0, 3);/*  ww  w.ja v  a 2  s .  co m*/
    int cpn = cpnBytes[0] << 16 | cpnBytes[1] << 8 | cpnBytes[2];
    gsi.setCpn(StlGsi.Cpn.getEnum(cpn));

    // Read Disk Format Code (DFC)
    gsi.setDfc(StlGsi.Dfc.getEnum(this.readString(dis, 8)));

    // Read Display Standard Code (DSC)
    gsi.setDsc(StlGsi.Dsc.getEnum(dis.readUnsignedByte()));

    // Read Character Code Table number (CCT)
    gsi.setCct(StlGsi.Cct.getEnum(Short.reverseBytes(dis.readShort())));

    // Read Character Language Code (LC)
    gsi.setLc(Short.reverseBytes(dis.readShort()));

    // Read Original Programme Title (OPT)
    gsi.setOpt(this.readString(dis, 32));

    // Read Original Programme Title (OET)
    gsi.setOet(this.readString(dis, 32));

    // Read Translated Programme Title (TPT)
    gsi.setTpt(this.readString(dis, 32));

    // Read translated Episode Title (TET)
    gsi.setTet(this.readString(dis, 32));

    // Read Translator's Name (TN)
    gsi.setTn(this.readString(dis, 32));

    // Read Translator's Contact Details (TCD)
    gsi.setTcd(this.readString(dis, 32));

    // Read Subtitle List Reference Code (SLR)
    gsi.setSlr(this.readString(dis, 16));

    // Read Creation Date (CD)
    gsi.setCd(this.readDate(this.readString(dis, 6)));

    // Read Revision Date (RD)
    gsi.setRd(this.readDate(this.readString(dis, 6)));

    // Read Revision number RN
    gsi.setRn(Short.reverseBytes(dis.readShort()));

    // Read Total Number of Text and Timing Information (TTI) blocks (TNB)
    gsi.setTnb(Integer.parseInt(this.readString(dis, 5)));

    // Read Total Number of Subtitles (TNS)
    gsi.setTns(Integer.parseInt(this.readString(dis, 5)));

    // Read Total Number of Subtitle Groups (TNG)
    dis.skipBytes(3);

    // Read Maximum Number of Displayable Characters in any text row (MNC)
    gsi.setMnc(Integer.parseInt(this.readString(dis, 2)));

    // Read Maximum Number of Displayable Rows (MNR)
    gsi.setMnr(Integer.parseInt(this.readString(dis, 2)));

    // Read Time Code: Status (TCS)
    gsi.setTcs((short) dis.readUnsignedByte());

    // Read Time Code: Start-of-Programme (TCP)
    gsi.setTcp(this.readTimeCode(this.readString(dis, 8), gsi.getDfc().getFrameRate()));

    // Read Time Code: First In-Cue (TCF)
    gsi.setTcf(this.readTimeCode(this.readString(dis, 8), gsi.getDfc().getFrameRate()));

    // Read Total Number of Disks (TND)
    gsi.setTnd((short) dis.readUnsignedByte());

    // Read Disk Sequence Number (DSN)
    gsi.setDsn((short) dis.readUnsignedByte());

    // Read Country of Origin (CO)
    gsi.setCo(this.readString(dis, 3));

    // Read Publisher (PUB)
    gsi.setPub(this.readString(dis, 32));

    // Read Editor's Name (EN)
    gsi.setEn(this.readString(dis, 32));

    // Read Editor's Contact Details (ECD)
    gsi.setEcd(this.readString(dis, 32));

    // Spare Bytes
    dis.skipBytes(75);

    // Read User-Defined Area (UDA)
    gsi.setUda(this.readString(dis, 576));

    return gsi;
}

From source file:org.apache.hadoop.hbase.util.UnsafeAccess.java

/**
 * Reads a short value at the given buffer's offset considering it was written in big-endian
 * format./*www. j a  v a2 s .  c  o m*/
 *
 * @param buf
 * @param offset
 * @return short value at offset
 */
public static short toShort(ByteBuffer buf, int offset) {
    if (littleEndian) {
        return Short.reverseBytes(getAsShort(buf, offset));
    }
    return getAsShort(buf, offset);
}

From source file:fr.noop.subtitle.stl.StlParser.java

private StlTti readTti(DataInputStream dis, StlGsi gsi) throws IOException, InvalidTimeRangeException {
    // Get charset from gsi
    String charset = gsi.getCct().getCharset();

    // Get frame rate from gsi
    int frameRate = gsi.getDfc().getFrameRate();

    // Read and extract metadata from TTI block
    // Each TTI block is 128 bytes long
    StlTti tti = new StlTti();

    // Read Subtitle Group Number (SGN)
    tti.setSgn((short) dis.readUnsignedByte());

    // Read Subtitle Number (SN)
    tti.setSn(Short.reverseBytes(dis.readShort()));

    // Read Extension Block Number (EBN)
    tti.setEbn((short) dis.readUnsignedByte());

    // Read Cumulative Status (CS)
    tti.setCs((short) dis.readUnsignedByte());

    // Read Time Code In (TCI)
    tti.setTci(this.readTimeCode(dis, frameRate));

    // Read Time Code Out (TCO)
    tti.setTco(this.readTimeCode(dis, frameRate));

    // Read Vertical Position (VP)
    tti.setVp((short) dis.readUnsignedByte());

    // Read Justification Code (JC)
    tti.setJc(StlTti.Jc.getEnum(dis.readUnsignedByte()));

    // Read Comment Flag (CF)
    tti.setCf((short) dis.readUnsignedByte());

    // Read TextField (TF)
    byte[] tfBytes = new byte[112];
    dis.readFully(tfBytes, 0, 112);/* ww  w.jav  a  2s . c om*/
    tti.setTf(new String(tfBytes, charset));

    // TTI is fully parsed
    return tti;
}

From source file:org.apache.hadoop.hbase.util.UnsafeAccess.java

/**
 * Put a short value out to the specified BB position in big-endian format.
 * @param buf the byte buffer//from   w  w  w  .ja  va2  s  .  co  m
 * @param offset position in the buffer
 * @param val short to write out
 * @return incremented offset
 */
public static int putShort(ByteBuffer buf, int offset, short val) {
    if (littleEndian) {
        val = Short.reverseBytes(val);
    }
    if (buf.isDirect()) {
        theUnsafe.putShort(((DirectBuffer) buf).address() + offset, val);
    } else {
        theUnsafe.putShort(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, val);
    }
    return offset + Bytes.SIZEOF_SHORT;
}

From source file:org.apache.kylin.common.util.Bytes.java

/**
 * Converts a byte array to an short value (Unsafe version)
 *
 * @param bytes  byte array/*from  ww  w .  ja v a2s  .c om*/
 * @param offset offset into array
 * @return the short value
 */
public static short toShortUnsafe(byte[] bytes, int offset) {
    if (org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.littleEndian) {
        return Short.reverseBytes(
                org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.theUnsafe
                        .getShort(bytes, (long) offset
                                + org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.BYTE_ARRAY_BASE_OFFSET));
    } else {
        return org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.theUnsafe
                .getShort(bytes, (long) offset
                        + org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.BYTE_ARRAY_BASE_OFFSET);
    }
}