Example usage for java.lang Integer reverseBytes

List of usage examples for java.lang Integer reverseBytes

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static int reverseBytes(int i) 

Source Link

Document

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

Usage

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private Archive readHeaders(final byte[] password) throws IOException {
    final byte[] signature = new byte[6];
    file.readFully(signature);//from  w  ww  .  j  a  va  2 s  . co  m
    if (!Arrays.equals(signature, sevenZSignature)) {
        throw new IOException("Bad 7z signature");
    }
    // 7zFormat.txt has it wrong - it's first major then minor
    final byte archiveVersionMajor = file.readByte();
    final byte archiveVersionMinor = file.readByte();
    if (archiveVersionMajor != 0) {
        throw new IOException(
                String.format("Unsupported 7z version (%d,%d)", archiveVersionMajor, archiveVersionMinor));
    }

    final long startHeaderCrc = 0xffffFFFFL & Integer.reverseBytes(file.readInt());
    final StartHeader startHeader = readStartHeader(startHeaderCrc);

    final int nextHeaderSizeInt = (int) startHeader.nextHeaderSize;
    if (nextHeaderSizeInt != startHeader.nextHeaderSize) {
        throw new IOException("cannot handle nextHeaderSize " + startHeader.nextHeaderSize);
    }
    file.seek(SIGNATURE_HEADER_SIZE + startHeader.nextHeaderOffset);
    final byte[] nextHeader = new byte[nextHeaderSizeInt];
    file.readFully(nextHeader);
    final CRC32 crc = new CRC32();
    crc.update(nextHeader);
    if (startHeader.nextHeaderCrc != crc.getValue()) {
        throw new IOException("NextHeader CRC mismatch");
    }

    final ByteArrayInputStream byteStream = new ByteArrayInputStream(nextHeader);
    DataInputStream nextHeaderInputStream = new DataInputStream(byteStream);
    Archive archive = new Archive();
    int nid = nextHeaderInputStream.readUnsignedByte();
    if (nid == NID.kEncodedHeader) {
        nextHeaderInputStream = readEncodedHeader(nextHeaderInputStream, archive, password);
        // Archive gets rebuilt with the new header
        archive = new Archive();
        nid = nextHeaderInputStream.readUnsignedByte();
    }
    if (nid == NID.kHeader) {
        readHeader(nextHeaderInputStream, archive);
        nextHeaderInputStream.close();
    } else {
        throw new IOException("Broken or unsupported archive: no Header");
    }
    return archive;
}

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

/**
 * Put an int value out to the specified ByteBuffer offset in big-endian format.
 * @param buf the ByteBuffer to write to
 * @param offset offset in the ByteBuffer
 * @param val int to write out//from   ww w .j a v  a2  s. c  o  m
 * @return incremented offset
 */
public static int putInt(ByteBuffer buf, int offset, int val) {
    if (littleEndian) {
        val = Integer.reverseBytes(val);
    }
    if (buf.isDirect()) {
        theUnsafe.putInt(((DirectBuffer) buf).address() + offset, val);
    } else {
        theUnsafe.putInt(buf.array(), offset + buf.arrayOffset() + BYTE_ARRAY_BASE_OFFSET, val);
    }
    return offset + Bytes.SIZEOF_INT;
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

/**
 * Finishes the addition of entries to this archive, without closing it.
 * // w  ww. ja  va2  s .  co  m
 * @throws IOException if archive is already closed.
 */
public void finish() throws IOException {
    if (finished) {
        throw new IOException("This archive has already been finished");
    }
    finished = true;

    final long headerPosition = file.getFilePointer();

    final ByteArrayOutputStream headerBaos = new ByteArrayOutputStream();
    final DataOutputStream header = new DataOutputStream(headerBaos);

    writeHeader(header);
    header.flush();
    final byte[] headerBytes = headerBaos.toByteArray();
    file.write(headerBytes);

    final CRC32 crc32 = new CRC32();

    // signature header
    file.seek(0);
    file.write(SevenZFile.sevenZSignature);
    // version
    file.write(0);
    file.write(2);

    // start header
    final ByteArrayOutputStream startHeaderBaos = new ByteArrayOutputStream();
    final DataOutputStream startHeaderStream = new DataOutputStream(startHeaderBaos);
    startHeaderStream.writeLong(Long.reverseBytes(headerPosition - SevenZFile.SIGNATURE_HEADER_SIZE));
    startHeaderStream.writeLong(Long.reverseBytes(0xffffFFFFL & headerBytes.length));
    crc32.reset();
    crc32.update(headerBytes);
    startHeaderStream.writeInt(Integer.reverseBytes((int) crc32.getValue()));
    startHeaderStream.flush();
    final byte[] startHeaderBytes = startHeaderBaos.toByteArray();
    crc32.reset();
    crc32.update(startHeaderBytes);
    file.writeInt(Integer.reverseBytes((int) crc32.getValue()));
    file.write(startHeaderBytes);
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private StartHeader readStartHeader(final long startHeaderCrc) throws IOException {
    final StartHeader startHeader = new StartHeader();
    DataInputStream dataInputStream = null;
    try {//w w w . ja va2  s  .c o  m
        dataInputStream = new DataInputStream(new CRC32VerifyingInputStream(
                new BoundedRandomAccessFileInputStream(file, 20), 20, startHeaderCrc));
        startHeader.nextHeaderOffset = Long.reverseBytes(dataInputStream.readLong());
        startHeader.nextHeaderSize = Long.reverseBytes(dataInputStream.readLong());
        startHeader.nextHeaderCrc = 0xffffFFFFL & Integer.reverseBytes(dataInputStream.readInt());
        return startHeader;
    } finally {
        if (dataInputStream != null) {
            dataInputStream.close();
        }
    }
}

From source file:org.basdroid.common.NetworkUtils.java

public static String getMacAddressFromNetworkInterface(final Context context) {

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();

    // Convert little-endian to big-endianif needed
    if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
        ipAddress = Integer.reverseBytes(ipAddress);
    }//  w  w  w . ja  v a  2 s  .c o  m

    byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();

    String result;
    try {
        InetAddress addr = InetAddress.getByAddress(bytes);
        NetworkInterface netInterface = NetworkInterface.getByInetAddress(addr);
        Log.d(TAG, "Wifi netInterface.getName() = " + netInterface.getName());

        byte[] mac = netInterface.getHardwareAddress();
        if (mac == null || mac.length == 0)
            return "";
        StringBuilder buf = new StringBuilder();
        for (int idx = 0; idx < mac.length; idx++) {
            buf.append(String.format("%02X:", mac[idx]));
        }
        if (buf.length() > 0)
            buf.deleteCharAt(buf.length() - 1);
        return buf.toString();
    } catch (UnknownHostException ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Unknown host.", ex);
        result = null;
    } catch (SocketException ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Socket exception.", ex);
        result = null;
    } catch (Exception ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Exception.", ex);
        result = null;
    }

    return result;
}

From source file:srebrinb.compress.sevenzip.SevenZFile.java

private StartHeader readStartHeader(final long startHeaderCrc) throws IOException {
    final StartHeader startHeader = new StartHeader();
    // using Stream rather than ByteBuffer for the benefit of the
    // built-in CRC check
    try (DataInputStream dataInputStream = new DataInputStream(new CRC32VerifyingInputStream(
            new BoundedSeekableByteChannelInputStream(channel, 20), 20, startHeaderCrc))) {
        startHeader.nextHeaderOffset = Long.reverseBytes(dataInputStream.readLong());
        startHeader.nextHeaderSize = Long.reverseBytes(dataInputStream.readLong());
        startHeader.nextHeaderCrc = 0xffffFFFFL & Integer.reverseBytes(dataInputStream.readInt());
        return startHeader;
    }//from w  ww .j  a  v  a 2s  .  com
}

From source file:com.l2jfree.gameserver.util.Util.java

public static int reverseRGBChanels(int rgbColor) {
    return (Integer.reverseBytes(rgbColor) >> 8) & 0xFFFFFF;
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writePackInfo(final DataOutput header) throws IOException {
    header.write(NID.kPackInfo);//from  w  w w .j  av  a2 s  .c o  m

    writeUint64(header, 0);
    writeUint64(header, 0xffffFFFFL & numNonEmptyStreams);

    header.write(NID.kSize);
    for (final SevenZArchiveEntry entry : files) {
        if (entry.hasStream()) {
            writeUint64(header, entry.getCompressedSize());
        }
    }

    header.write(NID.kCRC);
    header.write(1); // "allAreDefined" == true
    for (final SevenZArchiveEntry entry : files) {
        if (entry.hasStream()) {
            header.writeInt(Integer.reverseBytes((int) entry.getCompressedCrcValue()));
        }
    }

    header.write(NID.kEnd);
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeUnpackInfo(final DataOutput header) throws IOException {
    header.write(NID.kUnpackInfo);//from  w w w  .  j  a v a 2 s. com

    header.write(NID.kFolder);
    writeUint64(header, numNonEmptyStreams);
    header.write(0);
    for (final SevenZArchiveEntry entry : files) {
        if (entry.hasStream()) {
            writeFolder(header, entry);
        }
    }

    header.write(NID.kCodersUnpackSize);
    for (final SevenZArchiveEntry entry : files) {
        if (entry.hasStream()) {
            final long[] moreSizes = additionalSizes.get(entry);
            if (moreSizes != null) {
                for (final long s : moreSizes) {
                    writeUint64(header, s);
                }
            }
            writeUint64(header, entry.getSize());
        }
    }

    header.write(NID.kCRC);
    header.write(1); // "allAreDefined" == true
    for (final SevenZArchiveEntry entry : files) {
        if (entry.hasStream()) {
            header.writeInt(Integer.reverseBytes((int) entry.getCrcValue()));
        }
    }

    header.write(NID.kEnd);
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private void readPackInfo(final DataInput header, final Archive archive) throws IOException {
    archive.packPos = readUint64(header);
    final long numPackStreams = readUint64(header);
    int nid = header.readUnsignedByte();
    if (nid == NID.kSize) {
        archive.packSizes = new long[(int) numPackStreams];
        for (int i = 0; i < archive.packSizes.length; i++) {
            archive.packSizes[i] = readUint64(header);
        }//from ww  w . jav a  2 s .  c  om
        nid = header.readUnsignedByte();
    }

    if (nid == NID.kCRC) {
        archive.packCrcsDefined = readAllOrBits(header, (int) numPackStreams);
        archive.packCrcs = new long[(int) numPackStreams];
        for (int i = 0; i < (int) numPackStreams; i++) {
            if (archive.packCrcsDefined.get(i)) {
                archive.packCrcs[i] = 0xffffFFFFL & Integer.reverseBytes(header.readInt());
            }
        }

        nid = header.readUnsignedByte();
    }

    if (nid != NID.kEnd) {
        throw new IOException("Badly terminated PackInfo (" + nid + ")");
    }
}