void writeBytes(long value, int bytes) throws IOException { // write low-order bytes big-endian style for (int i = bytes - 1; i >= 0; i--) { this.write((int) (value >> (8 * i))); } }
/** * Assigns IDs to all the objects in this NSObject subtree. * * @param out The writer object that handles the binary serialization. */ void assignIDs(BinaryPropertyListWriter out) { out.assignID(this); }
@Override void toBinary(BinaryPropertyListWriter out) throws IOException { out.writeIntHeader(0xA, this.array.length); for(NSObject obj : this.array) { out.writeID(out.getID(obj)); } }
@Override void toBinary(BinaryPropertyListWriter out) throws IOException { out.writeIntHeader(0x4, this.bytes.length); out.write(this.bytes); }
@Override public void toBinary(BinaryPropertyListWriter out) throws IOException { out.write(0x33); out.writeDouble((this.date.getTime() - EPOCH) / 1000.0); }
this.write(new byte[]{'b', 'p', 'l', 'i', 's', 't'}); this.write(new byte[]{'0', '0'}); break; this.write(new byte[]{'1', '0'}); break; this.write(new byte[]{'1', '5'}); break; this.write(new byte[]{'2', '0'}); break; this.idSizeInBytes = computeIdSizeInBytes(this.idMap.size()); offsets[id] = this.count; if (obj == null) { this.write(0x00); } else { obj.toBinary(this); int offsetSizeInBytes = this.computeOffsetSizeInBytes(this.count); for (long offset : offsets) { this.writeBytes(offset, offsetSizeInBytes); this.write(new byte[6]); this.write(offsetSizeInBytes);
/** * Writes a binary plist serialization of the given object as the root. * This method does not close the output stream. * * @param out the stream to write to * @param root the source of the data to write to the stream * @throws IOException If an I/O error occurs while writing to the stream or the object structure contains * data that cannot be saved. */ public static void write(OutputStream out, NSObject root) throws IOException { int minVersion = getMinimumRequiredVersion(root); if (minVersion > VERSION_00) { String versionString = minVersion == VERSION_10 ? "v1.0" : (minVersion == VERSION_15 ? "v1.5" : (minVersion == VERSION_20 ? "v2.0" : "v0.0")); throw new IOException("The given property list structure cannot be saved. " + "The required version of the binary format (" + versionString + ") is not yet supported."); } BinaryPropertyListWriter w = new BinaryPropertyListWriter(out, minVersion); w.write(root); }
case INTEGER: { if (this.longValue() < 0) { out.write(0x13); out.writeBytes(this.longValue(), 8); } else if (this.longValue() <= 0xff) { out.write(0x10); out.writeBytes(this.longValue(), 1); } else if (this.longValue() <= 0xffff) { out.write(0x11); out.writeBytes(this.longValue(), 2); } else if (this.longValue() <= 0xffffffffL) { out.write(0x12); out.writeBytes(this.longValue(), 4); } else { out.write(0x13); out.writeBytes(this.longValue(), 8); out.write(0x23); out.writeDouble(this.doubleValue()); break; out.write(this.boolValue() ? 0x09 : 0x08); break;
void writeIntHeader(int kind, int value) throws IOException { assert value >= 0; if (value < 15) { this.write((kind << 4) + value); } else if (value < 256) { this.write((kind << 4) + 15); this.write(0x10); this.writeBytes(value, 1); } else if (value < 65536) { this.write((kind << 4) + 15); this.write(0x11); this.writeBytes(value, 2); } else { this.write((kind << 4) + 15); this.write(0x12); this.writeBytes(value, 4); } }
void writeID(int id) throws IOException { this.writeBytes(id, this.idSizeInBytes); }
NSDictionary dict = (NSDictionary) root; for (NSObject o : dict.getHashMap().values()) { int v = getMinimumRequiredVersion(o); if (v > minVersion) minVersion = v; NSArray array = (NSArray) root; for (NSObject o : array.getArray()) { int v = getMinimumRequiredVersion(o); if (v > minVersion) minVersion = v; NSSet set = (NSSet) root; for (NSObject o : set.allObjects()) { int v = getMinimumRequiredVersion(o); if (v > minVersion) minVersion = v;
@Override public void toBinary(BinaryPropertyListWriter out) throws IOException { CharBuffer charBuf = CharBuffer.wrap(this.content); int kind; ByteBuffer byteBuf; synchronized (NSString.class) { if (asciiEncoder == null) asciiEncoder = Charset.forName("ASCII").newEncoder(); else asciiEncoder.reset(); if (asciiEncoder.canEncode(charBuf)) { kind = 0x5; // standard ASCII byteBuf = asciiEncoder.encode(charBuf); } else { if (utf16beEncoder == null) utf16beEncoder = Charset.forName("UTF-16BE").newEncoder(); else utf16beEncoder.reset(); kind = 0x6; // UTF-16-BE byteBuf = utf16beEncoder.encode(charBuf); } } byte[] bytes = new byte[byteBuf.remaining()]; byteBuf.get(bytes); out.writeIntHeader(kind, this.content.length()); out.write(bytes); }
void writeLong(long value) throws IOException { this.writeBytes(value, 8); }
/** * Saves a property list with the given object as root in binary format into an output stream. * This method does not close the specified input stream. * * @param root The root object. * @param out The output stream. * @throws IOException If an error occurs during the writing process. */ public static void saveAsBinary(NSObject root, OutputStream out) throws IOException { BinaryPropertyListWriter.write(out, root); }
@Override void toBinary(BinaryPropertyListWriter out) throws IOException { if (this.ordered) { out.writeIntHeader(0xB, this.set.size()); } else { out.writeIntHeader(0xC, this.set.size()); } for (NSObject obj : this.set) { out.writeID(out.getID(obj)); } }
@Override void toBinary(BinaryPropertyListWriter out) throws IOException { out.writeIntHeader(0xD, this.dict.size()); Set<Map.Entry<String, NSObject>> entries = this.dict.entrySet(); for (Map.Entry<String, NSObject> entry : entries) { out.writeID(out.getID(new NSString(entry.getKey()))); } for (Map.Entry<String, NSObject> entry : entries) { out.writeID(out.getID(entry.getValue())); } }
/** * Writes a binary plist serialization of the given object as the root * into a byte array. * * @param root The root object of the property list * @return The byte array containing the serialized property list * @throws IOException If an I/O error occurs while writing to the stream or the object structure contains * data that cannot be saved. */ public static byte[] writeToArray(NSObject root) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); write(bout, root); return bout.toByteArray(); }
/** * Saves a property list with the given object as root into a binary file. * * @param root The root object. * @param out The output file. * @throws IOException If an error occurs during the writing process. */ public static void saveAsBinary(NSObject root, File out) throws IOException { File parent = out.getParentFile(); if (!parent.exists() && !parent.mkdirs()) { throw new IOException("The output directory does not exist and could not be created."); } BinaryPropertyListWriter.write(out, root); }
/** * Writes a binary plist file with the given object as the root. * * @param file the file to write to * @param root the source of the data to write to the file * @throws IOException If an I/O error occurs while writing to the file or the object structure contains * data that cannot be saved. */ public static void write(File file, NSObject root) throws IOException { OutputStream fileOutputStream = new FileOutputStream(file); try { write(fileOutputStream, root); } finally { try { fileOutputStream.close(); } catch (IOException ex) { // ignore } } }