Example usage for java.nio ByteBuffer getInt

List of usage examples for java.nio ByteBuffer getInt

Introduction

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

Prototype

public abstract int getInt();

Source Link

Document

Returns the int at the current position and increases the position by 4.

Usage

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

@Override
public GBDeviceEvent[] decodeResponse(byte[] responseData) {
    ByteBuffer buf = ByteBuffer.wrap(responseData);
    buf.order(ByteOrder.BIG_ENDIAN);
    short length = buf.getShort();
    short endpoint = buf.getShort();
    GBDeviceEvent devEvts[] = null;//w ww .  java 2 s .c  o  m
    byte pebbleCmd;
    switch (endpoint) {
    case ENDPOINT_MUSICCONTROL:
        pebbleCmd = buf.get();
        GBDeviceEventMusicControl musicCmd = new GBDeviceEventMusicControl();
        switch (pebbleCmd) {
        case MUSICCONTROL_NEXT:
            musicCmd.event = GBDeviceEventMusicControl.Event.NEXT;
            break;
        case MUSICCONTROL_PREVIOUS:
            musicCmd.event = GBDeviceEventMusicControl.Event.PREVIOUS;
            break;
        case MUSICCONTROL_PLAY:
            musicCmd.event = GBDeviceEventMusicControl.Event.PLAY;
            break;
        case MUSICCONTROL_PAUSE:
            musicCmd.event = GBDeviceEventMusicControl.Event.PAUSE;
            break;
        case MUSICCONTROL_PLAYPAUSE:
            musicCmd.event = GBDeviceEventMusicControl.Event.PLAYPAUSE;
            break;
        case MUSICCONTROL_VOLUMEUP:
            musicCmd.event = GBDeviceEventMusicControl.Event.VOLUMEUP;
            break;
        case MUSICCONTROL_VOLUMEDOWN:
            musicCmd.event = GBDeviceEventMusicControl.Event.VOLUMEDOWN;
            break;
        default:
            break;
        }
        devEvts = new GBDeviceEvent[] { musicCmd };
        break;
    case ENDPOINT_PHONECONTROL:
        pebbleCmd = buf.get();
        GBDeviceEventCallControl callCmd = new GBDeviceEventCallControl();
        switch (pebbleCmd) {
        case PHONECONTROL_HANGUP:
            callCmd.event = GBDeviceEventCallControl.Event.END;
            break;
        default:
            LOG.info("Unknown PHONECONTROL event" + pebbleCmd);
            break;
        }
        devEvts = new GBDeviceEvent[] { callCmd };
        break;
    case ENDPOINT_FIRMWAREVERSION:
        pebbleCmd = buf.get();
        GBDeviceEventVersionInfo versionCmd = new GBDeviceEventVersionInfo();

        buf.getInt(); // skip
        versionCmd.fwVersion = getFixedString(buf, 32);

        mFwMajor = versionCmd.fwVersion.charAt(1) - 48;
        LOG.info("Pebble firmware major detected as " + mFwMajor);

        byte[] tmp = new byte[9];
        buf.get(tmp, 0, 9);
        int hwRev = buf.get() + 8;
        if (hwRev >= 0 && hwRev < hwRevisions.length) {
            versionCmd.hwVersion = hwRevisions[hwRev];
        }
        devEvts = new GBDeviceEvent[] { versionCmd };
        break;
    case ENDPOINT_APPMANAGER:
        pebbleCmd = buf.get();
        switch (pebbleCmd) {
        case APPMANAGER_GETAPPBANKSTATUS:
            GBDeviceEventAppInfo appInfoCmd = new GBDeviceEventAppInfo();
            int slotCount = buf.getInt();
            int slotsUsed = buf.getInt();
            appInfoCmd.apps = new GBDeviceApp[slotsUsed];
            boolean[] slotInUse = new boolean[slotCount];

            for (int i = 0; i < slotsUsed; i++) {
                int id = buf.getInt();
                int index = buf.getInt();
                slotInUse[index] = true;
                String appName = getFixedString(buf, 32);
                String appCreator = getFixedString(buf, 32);

                int flags = buf.getInt();

                GBDeviceApp.Type appType;
                if ((flags & 16) == 16) { // FIXME: verify this assumption
                    appType = GBDeviceApp.Type.APP_ACTIVITYTRACKER;
                } else if ((flags & 1) == 1) { // FIXME: verify this assumption
                    appType = GBDeviceApp.Type.WATCHFACE;
                } else {
                    appType = GBDeviceApp.Type.APP_GENERIC;
                }
                Short appVersion = buf.getShort();
                appInfoCmd.apps[i] = new GBDeviceApp(tmpUUIDS.get(i), appName, appCreator,
                        appVersion.toString(), appType);
            }
            for (int i = 0; i < slotCount; i++) {
                if (!slotInUse[i]) {
                    appInfoCmd.freeSlot = (byte) i;
                    LOG.info("found free slot " + i);
                    break;
                }
            }
            devEvts = new GBDeviceEvent[] { appInfoCmd };
            break;
        case APPMANAGER_GETUUIDS:
            GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
            sendBytes.encodedBytes = encodeSimpleMessage(ENDPOINT_APPMANAGER, APPMANAGER_GETAPPBANKSTATUS);
            devEvts = new GBDeviceEvent[] { sendBytes };
            tmpUUIDS.clear();
            slotsUsed = buf.getInt();
            for (int i = 0; i < slotsUsed; i++) {
                UUID uuid = getUUID(buf);
                LOG.info("found uuid: " + uuid);
                tmpUUIDS.add(uuid);
            }
            break;
        case APPMANAGER_REMOVEAPP:
            GBDeviceEventAppManagement deleteRes = new GBDeviceEventAppManagement();
            deleteRes.type = GBDeviceEventAppManagement.EventType.DELETE;

            int result = buf.getInt();
            switch (result) {
            case APPMANAGER_RES_SUCCESS:
                deleteRes.event = GBDeviceEventAppManagement.Event.SUCCESS;
                break;
            default:
                deleteRes.event = GBDeviceEventAppManagement.Event.FAILURE;
                break;
            }
            devEvts = new GBDeviceEvent[] { deleteRes };
            break;
        default:
            LOG.info("Unknown APPMANAGER event" + pebbleCmd);
            break;
        }
        break;
    case ENDPOINT_PUTBYTES:
        pebbleCmd = buf.get();
        GBDeviceEventAppManagement installRes = new GBDeviceEventAppManagement();
        installRes.type = GBDeviceEventAppManagement.EventType.INSTALL;
        switch (pebbleCmd) {
        case PUTBYTES_INIT:
            installRes.token = buf.getInt();
            installRes.event = GBDeviceEventAppManagement.Event.SUCCESS;
            break;
        default:
            installRes.token = buf.getInt();
            installRes.event = GBDeviceEventAppManagement.Event.FAILURE;
            break;
        }
        devEvts = new GBDeviceEvent[] { installRes };
        break;
    case ENDPOINT_APPLICATIONMESSAGE:
    case ENDPOINT_LAUNCHER:
        pebbleCmd = buf.get();
        last_id = buf.get();
        UUID uuid = getUUID(buf);

        switch (pebbleCmd) {
        case APPLICATIONMESSAGE_PUSH:
            LOG.info((endpoint == ENDPOINT_LAUNCHER ? "got LAUNCHER PUSH from UUID : "
                    : "got APPLICATIONMESSAGE PUSH from UUID : ") + uuid);
            AppMessageHandler handler = mAppMessageHandlers.get(uuid);
            if (handler != null) {
                if (handler.isEnabled()) {
                    if (endpoint == ENDPOINT_APPLICATIONMESSAGE) {
                        ArrayList<Pair<Integer, Object>> dict = decodeDict(buf);
                        devEvts = handler.handleMessage(dict);
                    } else {
                        currentRunningApp = uuid;
                        devEvts = handler.onAppStart();
                    }
                } else {
                    devEvts = new GBDeviceEvent[] { null };
                }
            } else {
                try {
                    if (endpoint == ENDPOINT_APPLICATIONMESSAGE) {
                        devEvts = decodeDictToJSONAppMessage(uuid, buf);
                    } else {
                        currentRunningApp = uuid;
                        GBDeviceEventAppManagement gbDeviceEventAppManagement = new GBDeviceEventAppManagement();
                        gbDeviceEventAppManagement.uuid = uuid;
                        gbDeviceEventAppManagement.type = GBDeviceEventAppManagement.EventType.START;
                        gbDeviceEventAppManagement.event = GBDeviceEventAppManagement.Event.SUCCESS;
                        devEvts = new GBDeviceEvent[] { gbDeviceEventAppManagement };
                    }
                } catch (JSONException e) {
                    LOG.error(e.getMessage());
                    return null;
                }
            }
            break;
        case APPLICATIONMESSAGE_ACK:
            LOG.info("got APPLICATIONMESSAGE/LAUNCHER (EP " + endpoint + ")  ACK");
            devEvts = new GBDeviceEvent[] { null };
            break;
        case APPLICATIONMESSAGE_NACK:
            LOG.info("got APPLICATIONMESSAGE/LAUNCHER (EP " + endpoint + ")  NACK");
            devEvts = new GBDeviceEvent[] { null };
            break;
        case APPLICATIONMESSAGE_REQUEST:
            LOG.info("got APPLICATIONMESSAGE/LAUNCHER (EP " + endpoint + ")  REQUEST");
            devEvts = new GBDeviceEvent[] { null };
            break;
        default:
            break;
        }
        break;
    case ENDPOINT_PHONEVERSION:
        pebbleCmd = buf.get();
        switch (pebbleCmd) {
        case PHONEVERSION_REQUEST:
            LOG.info("Pebble asked for Phone/App Version - repLYING!");
            GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
            sendBytes.encodedBytes = encodePhoneVersion(PHONEVERSION_REMOTE_OS_ANDROID);
            devEvts = new GBDeviceEvent[] { sendBytes };
            break;
        default:
            break;
        }
        break;
    case ENDPOINT_DATALOG:
        devEvts = decodeDatalog(buf, length);
        break;
    case ENDPOINT_SCREENSHOT:
        devEvts = new GBDeviceEvent[] { decodeScreenshot(buf, length) };
        break;
    case ENDPOINT_EXTENSIBLENOTIFS:
    case ENDPOINT_NOTIFICATIONACTION:
        devEvts = decodeAction(buf);
        break;
    case ENDPOINT_PING:
        devEvts = new GBDeviceEvent[] { decodePing(buf) };
        break;
    case ENDPOINT_APPFETCH:
        devEvts = new GBDeviceEvent[] { decodeAppFetch(buf) };
        break;
    case ENDPOINT_SYSTEMMESSAGE:
        devEvts = new GBDeviceEvent[] { decodeSystemMessage(buf) };
        break;
    case ENDPOINT_APPRUNSTATE:
        devEvts = decodeAppRunState(buf);
        break;
    case ENDPOINT_BLOBDB:
        devEvts = new GBDeviceEvent[] { decodeBlobDb(buf) };
        break;
    case ENDPOINT_APPREORDER:
        devEvts = new GBDeviceEvent[] { decodeAppReorder(buf) };
        break;
    case ENDPOINT_APPLOGS:
        decodeAppLogs(buf);
        break;
    //            case ENDPOINT_VOICECONTROL:
    //                devEvts = new GBDeviceEvent[]{decodeVoiceControl(buf)};
    //            case ENDPOINT_AUDIOSTREAM:
    //                LOG.debug(GB.hexdump(responseData, 0, responseData.length));
    //                break;
    default:
        break;
    }

    return devEvts;
}

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

private void readFilesInfo(final ByteBuffer header, final Archive archive) throws IOException {
    final long numFiles = readUint64(header);
    final SevenZArchiveEntry[] files = new SevenZArchiveEntry[(int) numFiles];
    for (int i = 0; i < files.length; i++) {
        files[i] = new SevenZArchiveEntry();
    }/*from  ww w  . j  av  a  2s.c o  m*/
    BitSet isEmptyStream = null;
    BitSet isEmptyFile = null;
    BitSet isAnti = null;
    while (true) {
        final int propertyType = getUnsignedByte(header);
        if (propertyType == 0) {
            break;
        }
        final long size = readUint64(header);
        switch (propertyType) {
        case NID.kEmptyStream: {
            isEmptyStream = readBits(header, files.length);
            break;
        }
        case NID.kEmptyFile: {
            if (isEmptyStream == null) { // protect against NPE
                throw new IOException("Header format error: kEmptyStream must appear before kEmptyFile");
            }
            isEmptyFile = readBits(header, isEmptyStream.cardinality());
            break;
        }
        case NID.kAnti: {
            if (isEmptyStream == null) { // protect against NPE
                throw new IOException("Header format error: kEmptyStream must appear before kAnti");
            }
            isAnti = readBits(header, isEmptyStream.cardinality());
            break;
        }
        case NID.kName: {
            final int external = getUnsignedByte(header);
            if (external != 0) {
                throw new IOException("Not implemented");
            }
            if (((size - 1) & 1) != 0) {
                throw new IOException("File names length invalid");
            }
            final byte[] names = new byte[(int) (size - 1)];
            header.get(names);
            int nextFile = 0;
            int nextName = 0;
            for (int i = 0; i < names.length; i += 2) {
                if (names[i] == 0 && names[i + 1] == 0) {
                    files[nextFile++].setName(new String(names, nextName, i - nextName, CharsetNames.UTF_16LE));
                    nextName = i + 2;
                }
            }
            if (nextName != names.length || nextFile != files.length) {
                throw new IOException("Error parsing file names");
            }
            break;
        }
        case NID.kCTime: {
            final BitSet timesDefined = readAllOrBits(header, files.length);
            final int external = getUnsignedByte(header);
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasCreationDate(timesDefined.get(i));
                if (files[i].getHasCreationDate()) {
                    files[i].setCreationDate(header.getLong());
                }
            }
            break;
        }
        case NID.kATime: {
            final BitSet timesDefined = readAllOrBits(header, files.length);
            final int external = getUnsignedByte(header);
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasAccessDate(timesDefined.get(i));
                if (files[i].getHasAccessDate()) {
                    files[i].setAccessDate(header.getLong());
                }
            }
            break;
        }
        case NID.kMTime: {
            final BitSet timesDefined = readAllOrBits(header, files.length);
            final int external = getUnsignedByte(header);
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasLastModifiedDate(timesDefined.get(i));
                if (files[i].getHasLastModifiedDate()) {
                    files[i].setLastModifiedDate(header.getLong());
                }
            }
            break;
        }
        case NID.kWinAttributes: {
            final BitSet attributesDefined = readAllOrBits(header, files.length);
            final int external = getUnsignedByte(header);
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasWindowsAttributes(attributesDefined.get(i));
                if (files[i].getHasWindowsAttributes()) {
                    files[i].setWindowsAttributes(header.getInt());
                }
            }
            break;
        }
        case NID.kStartPos: {
            throw new IOException("kStartPos is unsupported, please report");
        }
        case NID.kDummy: {
            // 7z 9.20 asserts the content is all zeros and ignores the property
            // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287

            if (skipBytesFully(header, size) < size) {
                throw new IOException("Incomplete kDummy property");
            }
            break;
        }

        default: {
            // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287
            if (skipBytesFully(header, size) < size) {
                throw new IOException("Incomplete property of type " + propertyType);
            }
            break;
        }
        }
    }
    int nonEmptyFileCounter = 0;
    int emptyFileCounter = 0;
    for (int i = 0; i < files.length; i++) {
        files[i].setHasStream(isEmptyStream == null ? true : !isEmptyStream.get(i));
        if (files[i].hasStream()) {
            files[i].setDirectory(false);
            files[i].setAntiItem(false);
            files[i].setHasCrc(archive.subStreamsInfo.hasCrc.get(nonEmptyFileCounter));
            files[i].setCrcValue(archive.subStreamsInfo.crcs[nonEmptyFileCounter]);
            files[i].setSize(archive.subStreamsInfo.unpackSizes[nonEmptyFileCounter]);
            ++nonEmptyFileCounter;
        } else {
            files[i].setDirectory(isEmptyFile == null ? true : !isEmptyFile.get(emptyFileCounter));
            files[i].setAntiItem(isAnti == null ? false : isAnti.get(emptyFileCounter));
            files[i].setHasCrc(false);
            files[i].setSize(0);
            ++emptyFileCounter;
        }
    }
    archive.files = files;
    calculateStreamMap(archive);
}