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:org.apache.pdfbox.tools.imageio.TestImageIOUtils.java

/**
 * checks whether the resolution of a BMP image file is as expected.
 *
 * @param filename the name of the BMP file
 * @param expectedResolution the expected resolution
 *
 * @throws IOException if something goes wrong
 *///from   w w  w .j av a2s .c  o m
private void checkBmpResolution(String filename, int expectedResolution)
        throws FileNotFoundException, IOException {
    // BMP format explained here:
    // http://www.javaworld.com/article/2077561/learn-java/java-tip-60--saving-bitmap-files-in-java.html
    // we skip 38 bytes and then read two 4 byte-integers and reverse the bytes
    DataInputStream dis = new DataInputStream(new FileInputStream(new File(filename)));
    int skipped = dis.skipBytes(38);
    assertEquals("Can't skip 38 bytes in image file " + filename, 38, skipped);
    int pixelsPerMeter = Integer.reverseBytes(dis.readInt());
    int actualResolution = (int) Math.round(pixelsPerMeter / 100.0 * 2.54);
    assertEquals("X resolution doesn't match in image file " + filename, expectedResolution, actualResolution);
    pixelsPerMeter = Integer.reverseBytes(dis.readInt());
    actualResolution = (int) Math.round(pixelsPerMeter / 100.0 * 2.54);
    assertEquals("Y resolution doesn't match in image file " + filename, expectedResolution, actualResolution);
    dis.close();
}

From source file:com.adamkruger.myipaddressinfo.NetworkInfoFragment.java

private static String intToHostAddress(int addressAsInt) {
    String hostAddress = "";
    byte[] ipAddress = BigInteger.valueOf(Integer.reverseBytes(addressAsInt)).toByteArray();
    try {//ww  w.j  ava  2  s  . c o  m
        InetAddress inetAddress = InetAddress.getByAddress(ipAddress);
        hostAddress = InetAddressToString(inetAddress);
    } catch (UnknownHostException e) {
    }
    return hostAddress;
}

From source file:com.diablominer.DiabloMiner.NetworkState.JSONRPCNetworkState.java

WorkState doGetWorkMessage(boolean longPoll) throws IOException {
    ObjectNode getWorkMessage = mapper.createObjectNode();

    getWorkMessage.put("method", "getwork");
    getWorkMessage.putArray("params");
    getWorkMessage.put("id", 1);

    JsonNode responseMessage = doJSONRPCCall(longPoll, getWorkMessage);

    String datas;//www.j a  v  a  2  s  .c  o m
    String midstates;
    String targets;

    try {
        datas = responseMessage.get("data").asText();
        midstates = responseMessage.get("midstate").asText();
        targets = responseMessage.get("target").asText();
    } catch (Exception e) {
        throw new IOException("Bitcoin returned unparsable JSON");
    }

    WorkState workState = new WorkState(this);

    String parse;

    for (int i = 0; i < 32; i++) {
        parse = datas.substring(i * 8, (i * 8) + 8);
        workState.setData(i, Integer.reverseBytes((int) Long.parseLong(parse, 16)));
    }

    for (int i = 0; i < 8; i++) {
        parse = midstates.substring(i * 8, (i * 8) + 8);
        workState.setMidstate(i, Integer.reverseBytes((int) Long.parseLong(parse, 16)));
    }

    for (int i = 0; i < 8; i++) {
        parse = targets.substring(i * 8, (i * 8) + 8);
        workState.setTarget(i, (Long.reverseBytes(Long.parseLong(parse, 16) << 16)) >>> 16);
    }

    return workState;
}

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

private void readUnpackInfo(final DataInput header, final Archive archive) throws IOException {
    int nid = header.readUnsignedByte();
    if (nid != NID.kFolder) {
        throw new IOException("Expected kFolder, got " + nid);
    }//from   w  w  w .j a v  a 2s  .  c  o  m
    final long numFolders = readUint64(header);
    final Folder[] folders = new Folder[(int) numFolders];
    archive.folders = folders;
    final int external = header.readUnsignedByte();
    if (external != 0) {
        throw new IOException("External unsupported");
    }
    for (int i = 0; i < (int) numFolders; i++) {
        folders[i] = readFolder(header);
    }

    nid = header.readUnsignedByte();
    if (nid != NID.kCodersUnpackSize) {
        throw new IOException("Expected kCodersUnpackSize, got " + nid);
    }
    for (final Folder folder : folders) {
        folder.unpackSizes = new long[(int) folder.totalOutputStreams];
        for (int i = 0; i < folder.totalOutputStreams; i++) {
            folder.unpackSizes[i] = readUint64(header);
        }
    }

    nid = header.readUnsignedByte();
    if (nid == NID.kCRC) {
        final BitSet crcsDefined = readAllOrBits(header, (int) numFolders);
        for (int i = 0; i < (int) numFolders; i++) {
            if (crcsDefined.get(i)) {
                folders[i].hasCrc = true;
                folders[i].crc = 0xffffFFFFL & Integer.reverseBytes(header.readInt());
            } else {
                folders[i].hasCrc = false;
            }
        }

        nid = header.readUnsignedByte();
    }

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

From source file:com.diablominer.DiabloMiner.NetworkState.JSONRPCNetworkState.java

boolean doSendWorkMessage(WorkState workState) throws IOException {
    StringBuilder dataOutput = new StringBuilder(8 * 32 + 1);
    Formatter dataFormatter = new Formatter(dataOutput);
    int[] data = workState.getData();

    dataFormatter.format(/*  w w  w .ja va  2  s.co m*/
            "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x"
                    + "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
            Integer.reverseBytes(data[0]), Integer.reverseBytes(data[1]), Integer.reverseBytes(data[2]),
            Integer.reverseBytes(data[3]), Integer.reverseBytes(data[4]), Integer.reverseBytes(data[5]),
            Integer.reverseBytes(data[6]), Integer.reverseBytes(data[7]), Integer.reverseBytes(data[8]),
            Integer.reverseBytes(data[9]), Integer.reverseBytes(data[10]), Integer.reverseBytes(data[11]),
            Integer.reverseBytes(data[12]), Integer.reverseBytes(data[13]), Integer.reverseBytes(data[14]),
            Integer.reverseBytes(data[15]), Integer.reverseBytes(data[16]), Integer.reverseBytes(data[17]),
            Integer.reverseBytes(data[18]), Integer.reverseBytes(data[19]), Integer.reverseBytes(data[20]),
            Integer.reverseBytes(data[21]), Integer.reverseBytes(data[22]), Integer.reverseBytes(data[23]),
            Integer.reverseBytes(data[24]), Integer.reverseBytes(data[25]), Integer.reverseBytes(data[26]),
            Integer.reverseBytes(data[27]), Integer.reverseBytes(data[28]), Integer.reverseBytes(data[29]),
            Integer.reverseBytes(data[30]), Integer.reverseBytes(data[31]));

    ObjectNode sendWorkMessage = mapper.createObjectNode();
    sendWorkMessage.put("method", "getwork");
    ArrayNode params = sendWorkMessage.putArray("params");
    params.add(dataOutput.toString());
    sendWorkMessage.put("id", 1);

    JsonNode responseMessage = doJSONRPCCall(false, sendWorkMessage);

    boolean accepted;

    dataFormatter.close();

    try {
        accepted = responseMessage.getBooleanValue();
    } catch (Exception e) {
        throw new IOException("Bitcoin returned unparsable JSON");
    }

    return accepted;
}

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

private void readSubStreamsInfo(final DataInput header, final Archive archive) throws IOException {
    for (final Folder folder : archive.folders) {
        folder.numUnpackSubStreams = 1;/*from  ww  w .java  2 s  .  c  om*/
    }
    int totalUnpackStreams = archive.folders.length;

    int nid = header.readUnsignedByte();
    if (nid == NID.kNumUnpackStream) {
        totalUnpackStreams = 0;
        for (final Folder folder : archive.folders) {
            final long numStreams = readUint64(header);
            folder.numUnpackSubStreams = (int) numStreams;
            totalUnpackStreams += numStreams;
        }
        nid = header.readUnsignedByte();
    }

    final SubStreamsInfo subStreamsInfo = new SubStreamsInfo();
    subStreamsInfo.unpackSizes = new long[totalUnpackStreams];
    subStreamsInfo.hasCrc = new BitSet(totalUnpackStreams);
    subStreamsInfo.crcs = new long[totalUnpackStreams];

    int nextUnpackStream = 0;
    for (final Folder folder : archive.folders) {
        if (folder.numUnpackSubStreams == 0) {
            continue;
        }
        long sum = 0;
        if (nid == NID.kSize) {
            for (int i = 0; i < folder.numUnpackSubStreams - 1; i++) {
                final long size = readUint64(header);
                subStreamsInfo.unpackSizes[nextUnpackStream++] = size;
                sum += size;
            }
        }
        subStreamsInfo.unpackSizes[nextUnpackStream++] = folder.getUnpackSize() - sum;
    }
    if (nid == NID.kSize) {
        nid = header.readUnsignedByte();
    }

    int numDigests = 0;
    for (final Folder folder : archive.folders) {
        if (folder.numUnpackSubStreams != 1 || !folder.hasCrc) {
            numDigests += folder.numUnpackSubStreams;
        }
    }

    if (nid == NID.kCRC) {
        final BitSet hasMissingCrc = readAllOrBits(header, numDigests);
        final long[] missingCrcs = new long[numDigests];
        for (int i = 0; i < numDigests; i++) {
            if (hasMissingCrc.get(i)) {
                missingCrcs[i] = 0xffffFFFFL & Integer.reverseBytes(header.readInt());
            }
        }
        int nextCrc = 0;
        int nextMissingCrc = 0;
        for (final Folder folder : archive.folders) {
            if (folder.numUnpackSubStreams == 1 && folder.hasCrc) {
                subStreamsInfo.hasCrc.set(nextCrc, true);
                subStreamsInfo.crcs[nextCrc] = folder.crc;
                ++nextCrc;
            } else {
                for (int i = 0; i < folder.numUnpackSubStreams; i++) {
                    subStreamsInfo.hasCrc.set(nextCrc, hasMissingCrc.get(nextMissingCrc));
                    subStreamsInfo.crcs[nextCrc] = missingCrcs[nextMissingCrc];
                    ++nextCrc;
                    ++nextMissingCrc;
                }
            }
        }

        nid = header.readUnsignedByte();
    }

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

    archive.subStreamsInfo = subStreamsInfo;
}

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

/**
 * Converts a byte array to an int value (Unsafe version)
 *
 * @param bytes  byte array/*from  www.  j a  v  a2  s.co  m*/
 * @param offset offset into array
 * @return the int value
 */
public static int toIntUnsafe(byte[] bytes, int offset) {
    if (org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.littleEndian) {
        return Integer.reverseBytes(
                org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.theUnsafe
                        .getInt(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
                .getInt(bytes, (long) offset
                        + org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.BYTE_ARRAY_BASE_OFFSET);
    }
}

From source file:org.nuras.mcpha.Client.java

/**
 * Return the status of the oscilloscope.
 * //ww w.  j av  a 2s . co  m
 * @return the status of the oscilloscope
 * @throws java.io.IOException 
 */
synchronized public static int mcphaReadOscilloscopeStatus() throws IOException {
    sendCommand(MCPHA_COMMAND_READ_OSCILLOSCOPE_STATUS, 0L, 4L);

    // read response
    DataInputStream in = new DataInputStream(deviceSocket.getInputStream());

    return Integer.reverseBytes(in.readInt());
}

From source file:com.mobilyzer.util.PhoneUtils.java

public String getWifiIpAddress() {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null) {
        int ip = wifiInfo.getIpAddress();
        if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
            ip = Integer.reverseBytes(ip);
        }//from  w  w w.j  a v a  2s . c  o m
        byte[] bytes = BigInteger.valueOf(ip).toByteArray();
        String address;
        try {
            address = InetAddress.getByAddress(bytes).getHostAddress();
            return address;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }
    return null;
}

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

private void writeFileWindowsAttributes(final DataOutput header) throws IOException {
    int numWindowsAttributes = 0;
    for (final SevenZArchiveEntry entry : files) {
        if (entry.getHasWindowsAttributes()) {
            ++numWindowsAttributes;//from  www  .  j  a v a  2  s.c o m
        }
    }
    if (numWindowsAttributes > 0) {
        header.write(NID.kWinAttributes);

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        if (numWindowsAttributes != files.size()) {
            out.write(0);
            final BitSet attributes = new BitSet(files.size());
            for (int i = 0; i < files.size(); i++) {
                attributes.set(i, files.get(i).getHasWindowsAttributes());
            }
            writeBits(out, attributes, files.size());
        } else {
            out.write(1); // "allAreDefined" == true
        }
        out.write(0);
        for (final SevenZArchiveEntry entry : files) {
            if (entry.getHasWindowsAttributes()) {
                out.writeInt(Integer.reverseBytes(entry.getWindowsAttributes()));
            }
        }
        out.flush();
        final byte[] contents = baos.toByteArray();
        writeUint64(header, contents.length);
        header.write(contents);
    }
}