/// <summary>Serializes an <code>XMPMeta</code>-object as RDF into a string.</summary> /// <remarks> /// Serializes an <code>XMPMeta</code>-object as RDF into a string. /// <em>Note:</em> Encoding is forced to UTF-16 when serializing to a /// string to ensure the correctness of "exact packet size". /// </remarks> /// <param name="xmp">a metadata implementation object</param> /// <param name="options"> /// Options to control the serialization (see /// <see cref="Com.Adobe.Xmp.Options.SerializeOptions"/> /// ). /// </param> /// <returns>Returns a string containing the serialized RDF.</returns> /// <exception cref="Com.Adobe.Xmp.XMPException">on serializsation errors.</exception> public static string SerializeToString(XMPMetaImpl xmp, SerializeOptions options) { // forces the encoding to be UTF-16 to get the correct string length options = options != null ? options : new SerializeOptions(); options.SetEncodeUTF16BE(true); ByteArrayOutputStream @out = new ByteArrayOutputStream(2048); Serialize(xmp, @out, options); try { return @out.ToString(options.GetEncoding()); } catch (UnsupportedEncodingException) { // cannot happen as UTF-8/16LE/BE is required to be implemented in // Java return @out.ToString(); } }
// UTF-8 /// <summary>The actual serialization.</summary> /// <param name="xmp">the metadata object to be serialized</param> /// <param name="out">outputStream the output stream to serialize to</param> /// <param name="options">the serialization options</param> /// <exception cref="Com.Adobe.Xmp.XMPException">If case of wrong options or any other serialization error.</exception> public virtual void Serialize(XMPMeta xmp, OutputStream @out, SerializeOptions options) { try { outputStream = new CountOutputStream(@out); writer = new OutputStreamWriter(outputStream, options.GetEncoding()); this.xmp = (XMPMetaImpl)xmp; this.options = options; this.padding = options.GetPadding(); writer = new OutputStreamWriter(outputStream, options.GetEncoding()); CheckOptionsConsistence(); // serializes the whole packet, but don't write the tail yet // and flush to make sure that the written bytes are calculated correctly string tailStr = SerializeAsRDF(); writer.Flush(); // adds padding AddPadding(tailStr.Length); // writes the tail Write(tailStr); writer.Flush(); outputStream.Close(); } catch (IOException) { throw new XMPException("Error writing to the OutputStream", XMPErrorConstants.Unknown); } }