/** * Parses a long from a (big-endian) byte array. * * @param bytes The bytes representing the long integer. * @return The long integer represented by the given bytes. */ @SuppressWarnings("unused") public static long parseLong(byte[] bytes) { return parseLong(bytes, 0, bytes.length); }
/** * Parses a double from a (big-endian) byte array. * * @param bytes The bytes representing the double. * @param startIndex Beginning of the double in the byte array. * @param endIndex End of the double in the byte array. * @return The double represented by the given bytes. */ public static double parseDouble(byte[] bytes, int startIndex, int endIndex) { if (endIndex - startIndex == 8) { return Double.longBitsToDouble(parseLong(bytes, startIndex, endIndex)); } else if (endIndex - startIndex == 4) { return Float.intBitsToFloat((int)parseLong(bytes, startIndex, endIndex)); } else { throw new IllegalArgumentException("endIndex ("+endIndex+") - startIndex ("+startIndex+") != 4 or 8"); } }
/** * Creates a new NSNumber instance from its binary representation. * * @param bytes An array of bytes containing the binary representation of the number. * @param startIndex The position in the array at which the number is stored. * @param endIndex The position in the array at which the number's data ends. * @param type The type of number * @see #INTEGER * @see #REAL * @see #BOOLEAN */ public NSNumber(byte[] bytes, final int startIndex, final int endIndex, final int type){ switch (type) { case INTEGER: { this.doubleValue = this.longValue = BinaryPropertyListParser.parseLong(bytes, startIndex, endIndex); break; } case REAL: { this.doubleValue = BinaryPropertyListParser.parseDouble(bytes, startIndex, endIndex); this.longValue = Math.round(this.doubleValue); break; } default: { throw new IllegalArgumentException("Type argument is not valid."); } } this.type = type; }