/// <summary>Static method to serialize the metadata object.</summary> /// <remarks> /// For each serialisation, a new XMPSerializer /// instance is created, either XMPSerializerRDF or XMPSerializerPlain so that its possible to /// serialialize the same XMPMeta objects in two threads. /// </remarks> /// <param name="xmp">a metadata implementation object</param> /// <param name="stream">the output stream to serialize to</param> /// <param name="options">serialization options, can be <c>null</c> for default.</param> /// <exception cref="XmpException"/> public static void Serialize(XmpMeta xmp, Stream stream, SerializeOptions options) { options = options ?? new SerializeOptions(); // sort the internal data model on demand if (options.Sort) xmp.Sort(); new XmpSerializerRdf().Serialize(xmp, stream, options); }
/// <summary>Serializes an <c>XMPMeta</c>-object as RDF into a string.</summary> /// <remarks> /// <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="SerializeOptions"/>).</param> /// <returns>Returns a string containing the serialized RDF.</returns> /// <exception cref="XmpException">on serialization errors.</exception> public static string SerializeToString(XmpMeta xmp, SerializeOptions options) { // forces the encoding to be UTF-16 to get the correct string length options = options ?? new SerializeOptions(); options.EncodeUtf16Be = true; var output = new MemoryStream(2048); Serialize(xmp, output, options); try { return options.GetEncoding().GetString(output.GetBuffer(), 0, (int)output.Length); } catch { // Should not happen as UTF-8/16LE/BE are all available return output.ToString(); } }
/// <summary>Serializes an <c>XMPMeta</c>-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="SerializeOptions"/>).</param> /// <returns>Returns a byte buffer containing the serialized RDF.</returns> /// <exception cref="XmpException">on serialization errors.</exception> public static byte[] SerializeToBuffer(XmpMeta xmp, SerializeOptions options) { var output = new MemoryStream(2048); Serialize(xmp, output, options); return output.ToArray(); }
/// <summary>Serializes an <c>XMPMeta</c>-object as RDF into an <c>OutputStream</c>.</summary> /// <param name="xmp">a metadata object</param> /// <param name="options">Options to control the serialization (see <see cref="SerializeOptions"/>).</param> /// <param name="stream">an <c>OutputStream</c> to write the serialized RDF to.</param> /// <exception cref="XmpException">on serialization errors.</exception> public static void Serialize(IXmpMeta xmp, Stream stream, SerializeOptions options = null) { AssertImplementation(xmp); XmpSerializerHelper.Serialize((XmpMeta)xmp, stream, options); }
/// <summary>Serializes an <c>XMPMeta</c>-object as RDF into a string.</summary> /// <remarks> /// Serializes an <c>XMPMeta</c>-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="SerializeOptions"/>).</param> /// <returns>Returns a string containing the serialized RDF.</returns> /// <exception cref="XmpException">on serialization errors.</exception> public static string SerializeToString(IXmpMeta xmp, SerializeOptions options) { AssertImplementation(xmp); return XmpSerializerHelper.SerializeToString((XmpMeta)xmp, options); }
/// <summary>Serializes an <c>XMPMeta</c>-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="SerializeOptions"/>).</param> /// <returns>Returns a byte buffer containing the serialized RDF.</returns> /// <exception cref="XmpException">on serialization errors.</exception> public static byte[] SerializeToBuffer(IXmpMeta xmp, SerializeOptions options) { AssertImplementation(xmp); return XmpSerializerHelper.SerializeToBuffer((XmpMeta)xmp, options); }
// UTF-8 /// <summary>The actual serialization.</summary> /// <param name="xmp">the metadata object to be serialized</param> /// <param name="stream">outputStream the output stream to serialize to</param> /// <param name="options">the serialization options</param> /// <exception cref="XmpException">If case of wrong options or any other serialization error.</exception> public void Serialize(IXmpMeta xmp, Stream stream, SerializeOptions options) { try { _stream = stream; _startPos = _stream.Position; _writer = new StreamWriter(_stream, options.GetEncoding()); _xmp = (XmpMeta)xmp; _options = options; _padding = options.Padding; _writer = new StreamWriter(_stream, 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 var tailStr = SerializeAsRdf(); _writer.Flush(); // adds padding AddPadding(tailStr.Length); // writes the tail Write(tailStr); _writer.Flush(); _stream.Close(); } catch (IOException) { throw new XmpException("Error writing to the OutputStream", XmpErrorCode.Unknown); } }