/** * Parses a binary property list from a byte array. * * @param data The binary property list's data. * @return The root object of the property list. This is usually a {@link NSDictionary} but can also be a {@link NSArray}. * @throws PropertyListFormatException When the property list's format could not be parsed. * @throws java.io.UnsupportedEncodingException If a {@link NSString} object could not be decoded. */ public static NSObject parse(byte[] data) throws PropertyListFormatException, UnsupportedEncodingException { BinaryPropertyListParser parser = new BinaryPropertyListParser(); return parser.doParse(data); }
/** * Parses a double from a (big-endian) byte array. * * @param bytes The bytes representing the double. * @return The double represented by the given bytes. */ @SuppressWarnings("unused") public static double parseDouble(byte[] bytes) { return parseDouble(bytes, 0, bytes.length); }
/** * 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 binary property list file. * * @param f The binary property list file * @return The root object of the property list. This is usually a {@link NSDictionary} but can also be a {@link NSArray}. * @throws PropertyListFormatException When the property list's format could not be parsed. * @throws java.io.IOException If a {@link NSString} object could not be decoded or an I/O error occurs on the input stream. */ public static NSObject parse(File f) throws IOException, PropertyListFormatException { InputStream fileInputStream = new FileInputStream(f); try { return parse(fileInputStream); } finally { try { fileInputStream.close(); } catch (IOException e) { // ignore } } }
int[] lengthAndOffset = this.readLengthAndOffset(objInfo, offset); int length = lengthAndOffset[0]; int dataOffset = lengthAndOffset[1]; return new NSData(copyOfRange(this.bytes, offset + dataOffset, offset + dataOffset + length)); int[] lengthAndOffset = this.readLengthAndOffset(objInfo, offset); int length = lengthAndOffset[0]; //Each character is 1 byte int strOffset = lengthAndOffset[1]; int[] lengthAndOffset = this.readLengthAndOffset(objInfo, offset); int characters = lengthAndOffset[0]; int strOffset = lengthAndOffset[1]; int[] lengthAndOffset = this.readLengthAndOffset(objInfo, offset); int strOffset = lengthAndOffset[1]; int characters = lengthAndOffset[0]; int length = this.calculateUtf8StringLength(this.bytes, offset + strOffset, characters); return new NSString(this.bytes, offset + strOffset, offset + strOffset + length, "UTF-8"); return new UID(String.valueOf(obj), copyOfRange(this.bytes, offset + 1, offset + 1 + length)); int[] lengthAndOffset = this.readLengthAndOffset(objInfo, offset); int length = lengthAndOffset[0]; int arrayOffset = lengthAndOffset[1]; int objRef = (int) parseUnsignedInt(this.bytes, offset + arrayOffset + i * this.objectRefSize, offset + arrayOffset + (i + 1) * this.objectRefSize); array.setValue(i, this.parseObject(objRef));
String magic = new String(copyOfRange(this.bytes, 0, 8)); if (!magic.startsWith("bplist")) { throw new IllegalArgumentException("The given data is no binary property list. Wrong magic bytes: " + magic); byte[] trailer = copyOfRange(this.bytes, this.bytes.length - 32, this.bytes.length); int offsetSize = (int) parseUnsignedInt(trailer, 6, 7); this.objectRefSize = (int) parseUnsignedInt(trailer, 7, 8); int numObjects = (int) parseUnsignedInt(trailer, 8, 16); int topObject = (int) parseUnsignedInt(trailer, 16, 24); int offsetTableOffset = (int) parseUnsignedInt(trailer, 24, 32); this.offsetTable[i] = (int) parseUnsignedInt(this.bytes, offsetTableOffset + i * offsetSize, offsetTableOffset + (i + 1) * offsetSize); return this.parseObject(topObject);
/** * 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; }
/** * Reads the length for arrays, sets and dictionaries. * * @param objInfo Object information byte. * @param offset Offset in the byte array at which the object is located. * @return An array with the length two. First entry is the length, second entry the offset at which the content starts. */ private int[] readLengthAndOffset(int objInfo, int offset) { int lengthValue = objInfo; int offsetValue = 1; if (objInfo == 0xF) { int int_type = this.bytes[offset + 1]; int intType = (int_type & 0xF0) >> 4; if (intType != 0x1) { System.err.println("BinaryPropertyListParser: Length integer has an unexpected type" + intType + ". Attempting to parse anyway..."); } int intInfo = int_type & 0x0F; int intLength = (int) Math.pow(2, intInfo); offsetValue = 2 + intLength; if (intLength < 3) { lengthValue = (int) parseUnsignedInt(this.bytes, offset + 2, offset + 2 + intLength); } else { lengthValue = new BigInteger(copyOfRange(this.bytes, offset + 2, offset + 2 + intLength)).intValue(); } } return new int[]{lengthValue, offsetValue}; }
/** * Parses an unsigned integers from a byte array. * * @param bytes The byte array containing the unsigned integer. * @return The unsigned integer represented by the given bytes. */ @SuppressWarnings("unused") public static long parseUnsignedInt(byte[] bytes) { return parseUnsignedInt(bytes, 0, bytes.length); }
public static JsonElement parse(byte[] bytes) { try { return parse(BinaryPropertyListParser.parse(bytes)); } catch (IOException | PropertyListFormatException e) { throw new IllegalArgumentException("not a plist"); } }
/** * Parses a binary property list from an input stream. * This method does not close the specified input stream. * * @param is The input stream that points to the property list's data. * @return The root object of the property list. This is usually a {@link NSDictionary} but can also be a {@link NSArray}. * @throws PropertyListFormatException When the property list's format could not be parsed. * @throws java.io.IOException If a {@link NSString} object could not be decoded or an I/O error occurs on the input stream. */ public static NSObject parse(InputStream is) throws IOException, PropertyListFormatException { return parse(PropertyListParser.readAll(is)); }
/** * Creates a new NSDate instance from its binary representation. * * @param bytes The byte array containing the date data. * @param startIndex The index within the array at which the date data begins. * @param endIndex The index within the array at which the data date ends. */ public NSDate(byte[] bytes, final int startIndex, final int endIndex) { //dates are 8 byte big-endian double, seconds since the epoch this.date = new Date(EPOCH + (long) (1000 * BinaryPropertyListParser.parseDouble(bytes, startIndex, endIndex))); }
/** * 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"); } }
public static JsonElement parse(InputStream is) throws IOException { try { return parse(BinaryPropertyListParser.parse(is)); } catch (PropertyListFormatException e) { throw new IllegalArgumentException("not a plist"); } }
try{ rootDict = (NSDictionary) BinaryPropertyListParser .parse(buffer.toByteArray()); }catch(IllegalArgumentException e){ log.debug("Uploaded file didn't have a Binary Plist");
/** * Parses a property list from an InputStream. * This method does not close the specified input stream. * * @param is The InputStream delivering the property list data. * @return The root object of the property list. This is usually a {@link NSDictionary} but can also be a {@link NSArray}. * @throws javax.xml.parsers.ParserConfigurationException If a document builder for parsing a XML property list could not be created. * @throws java.io.IOException If any I/O error occurs while reading the input stream. * @throws org.xml.sax.SAXException If any parse error occurs. * @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format. * @throws java.text.ParseException If the given property list has an invalid format. */ public static NSObject parse(InputStream is) throws IOException, PropertyListFormatException, ParseException, ParserConfigurationException, SAXException { if (!is.markSupported()) { is = new BufferedInputStream(is); } switch(determineType(is, 0)) { case TYPE_BINARY: return BinaryPropertyListParser.parse(is); case TYPE_XML: return XMLPropertyListParser.parse(is); case TYPE_ASCII: return ASCIIPropertyListParser.parse(is); case TYPE_ERROR_BLANK: return null; default: throw new PropertyListFormatException("The given data is not a property list of a supported format."); } }