/// <summary>Static method to serialize the metadata object.</summary> /// <remarks> /// Static method to serialize the metadata object. For each serialisation, a new XMPSerializer /// instance is created, either XMPSerializerRDF or XMPSerializerPlain so thats its possible to /// serialialize the same XMPMeta objects in two threads. /// </remarks> /// <param name="xmp">a metadata implementation object</param> /// <param name="out">the output stream to serialize to</param> /// <param name="options">serialization options, can be <code>null</code> for default.</param> /// <exception cref="Com.Adobe.Xmp.XMPException"/> public static void Serialize(XMPMetaImpl xmp, OutputStream @out, SerializeOptions options) { options = options != null ? options : new SerializeOptions(); // sort the internal data model on demand if (options.GetSort()) { xmp.Sort(); } new XMPSerializerRDF().Serialize(xmp, @out, options); }
/// <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(); } }
/// <summary>Serializes the XmpDirectory component of <code>Metadata</code> into an <code>OutputStream</code></summary> /// <param name="os">Destination for the xmp data</param> /// <param name="data">populated metadata</param> /// <returns>serialize success</returns> public static bool Write(OutputStream os, Com.Drew.Metadata.Metadata data) { XmpDirectory dir = data.GetFirstDirectoryOfType<XmpDirectory>(); if (dir == null) { return false; } XMPMeta meta = dir.GetXMPMeta(); try { SerializeOptions so = new SerializeOptions().SetOmitPacketWrapper(true); XMPMetaFactory.Serialize(meta, os, so); } catch (XMPException e) { Sharpen.Runtime.PrintStackTrace(e); return false; } return true; }
/// <summary>Serializes an <code>XMPMeta</code>-object as RDF into a byte buffer.</summary> /// <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 byte buffer containing the serialized RDF.</returns> /// <exception cref="Com.Adobe.Xmp.XMPException">on serializsation errors.</exception> public static sbyte[] SerializeToBuffer(XMPMetaImpl xmp, SerializeOptions options) { ByteArrayOutputStream @out = new ByteArrayOutputStream(2048); Serialize(xmp, @out, options); return @out.ToByteArray(); }
// 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); } }
/// <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 ignored when serializing to a string. /// </remarks> /// <param name="xmp">a metadata 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="XMPException">on serializsation errors.</exception> /// <exception cref="Com.Adobe.Xmp.XMPException"/> public static string SerializeToString(XMPMeta xmp, SerializeOptions options) { AssertImplementation(xmp); return XMPSerializerHelper.SerializeToString((XMPMetaImpl)xmp, options); }
/// <summary>Serializes an <code>XMPMeta</code>-object as RDF into a byte buffer.</summary> /// <param name="xmp">a metadata object</param> /// <param name="options"> /// Options to control the serialization (see /// <see cref="Com.Adobe.Xmp.Options.SerializeOptions"/> /// ). /// </param> /// <returns>Returns a byte buffer containing the serialized RDF.</returns> /// <exception cref="XMPException">on serializsation errors.</exception> /// <exception cref="Com.Adobe.Xmp.XMPException"/> public static sbyte[] SerializeToBuffer(XMPMeta xmp, SerializeOptions options) { AssertImplementation(xmp); return XMPSerializerHelper.SerializeToBuffer((XMPMetaImpl)xmp, options); }
/// <summary>Serializes an <code>XMPMeta</code>-object as RDF into an <code>OutputStream</code>.</summary> /// <param name="xmp">a metadata object</param> /// <param name="options"> /// Options to control the serialization (see /// <see cref="Com.Adobe.Xmp.Options.SerializeOptions"/> /// ). /// </param> /// <param name="out">an <code>OutputStream</code> to write the serialized RDF to.</param> /// <exception cref="XMPException">on serializsation errors.</exception> /// <exception cref="Com.Adobe.Xmp.XMPException"/> public static void Serialize(XMPMeta xmp, OutputStream @out, SerializeOptions options) { AssertImplementation(xmp); XMPSerializerHelper.Serialize((XMPMetaImpl)xmp, @out, options); }