static boolean deserialize()

in common/src/main/java/com/microsoft/alm/secret/Token.java [216:261]


    static boolean deserialize(final byte[] bytes, final TokenType type, final AtomicReference<Token> tokenReference) {
        Debug.Assert(bytes != null, "The bytes parameter is null");
        Debug.Assert(bytes.length > 0, "The bytes parameter is too short");
        Debug.Assert(type != null, "The type parameter is invalid");

        tokenReference.set(null);

        try {
            final int preamble = sizeofTokenType + sizeofGuid;

            if (bytes.length > preamble) {
                TokenType readType;
                UUID targetIdentity;

                final ByteBuffer p = ByteBuffer.wrap(bytes); // PORT NOTE: ByteBuffer is closest to "fixed"
                {
                    readType = TokenType.fromValue(Integer.reverseBytes(p.getInt()));
                    byte[] guidBytes = new byte[16];
                    p.get(guidBytes);
                    targetIdentity = Guid.fromBytes(guidBytes);
                }

                if (readType == type) {
                    final String value = StringHelper.UTF8GetString(bytes, preamble, bytes.length - preamble);

                    if (!StringHelper.isNullOrWhiteSpace(value)) {
                        tokenReference.set(new Token(value, type));
                        tokenReference.get().targetIdentity = targetIdentity;
                    }
                }
            }

            // if value hasn't been set yet, fall back to old format decode
            if (tokenReference.get() == null) {
                final String value = StringHelper.UTF8GetString(bytes);

                if (!StringHelper.isNullOrWhiteSpace(value)) {
                    tokenReference.set(new Token(value, type));
                }
            }
        } catch (final Throwable throwable) {
            logger.debug("   token deserialization error");
        }

        return tokenReference.get() != null;
    }