示例#1
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();
     }
     catch (IOException)
     {
         throw new XmpException("Error writing to the OutputStream", XmpErrorCode.Unknown);
     }
 }
        /// <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());
            }
        }
        /// <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 &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="iText.Kernel.XMP.Options.SerializeOptions"/>
        /// ).
        /// </param>
        /// <returns>Returns a string containing the serialized RDF.</returns>
        public static String SerializeToString(XMPMetaImpl xmp, SerializeOptions options)
        {
            // forces the encoding to be UTF-16 to get the correct string length
            options = options ?? new SerializeOptions();
            options.SetEncodeUTF16BE(true);

            MemoryStream output = new MemoryStream(2048);

            Serialize(xmp, output, options);

            try {
                return(new EncodingNoPreamble(IanaEncodings.GetEncodingEncoding(options.GetEncoding())).GetString(output.GetBuffer()));
            }
            catch (Exception) {
                // cannot happen as UTF-8/16LE/BE is required to be implemented in
                // Java
                return(GetString(output.GetBuffer()));
            }
        }
        /// <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 &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="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 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();
            // By default encoding is utf8
            // options should be set by the client. Commenting setting utf16 option
            // so that users can get the string in whichever encoding they want (by setting the options bits)
            //options.EncodeUtf16Be = true;

            var output = new MemoryStream(2048);

            Serialize(xmp, output, options);
            try
            {
                return(options.GetEncoding().GetString(output.ToArray(), 0, (int)output.Length));
            }
            catch
            {
                // Should not happen as UTF-8/16LE/BE are all available
                return(output.ToString());
            }
        }