Ejemplo n.º 1
0
        /// <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 &quot;exact packet size&quot;.
        /// </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
            {
#if PORTABLE
                return options.GetEncoding().GetString(output.ToArray(), 0, (int)output.Length);
#else
                return options.GetEncoding().GetString(output.GetBuffer(), 0, (int)output.Length);
#endif
            }
            catch
            {
                // Should not happen as UTF-8/16LE/BE are all available
                return output.ToString();
            }
        }
Ejemplo n.º 2
0
 // 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);
     }
 }