// end encodeObject
/// <summary>
/// Serializes an object and returns the Base64-encoded
/// version of that serialized object.
/// </summary>
/// <remarks>
/// Serializes an object and returns the Base64-encoded
/// version of that serialized object.
/// <p>As of v 2.3, if the object
/// cannot be serialized or there is another error,
/// the method will throw an java.io.IOException. <b>This is new to v2.3!</b>
/// In earlier versions, it just returned a null value, but
/// in retrospect that's a pretty poor way to handle it.</p>
/// The object is not GZip-compressed before being encoded.
/// <p>
/// Example options:<pre>
/// GZIP: gzip-compresses object before encoding it.
/// DO_BREAK_LINES: break lines at 76 characters
/// </pre>
/// <p>
/// Example: <code>encodeObject( myObj, Base64.GZIP )</code> or
/// <p>
/// Example: <code>encodeObject( myObj, Base64.GZIP | Base64.DO_BREAK_LINES )</code>
/// </remarks>
/// <param name="serializableObject">The object to encode</param>
/// <param name="options">Specified options</param>
/// <returns>The Base64-encoded object</returns>
/// <seealso cref="Gzip">Gzip</seealso>
/// <seealso cref="DoBreakLines">DoBreakLines</seealso>
/// <exception cref="System.IO.IOException">if there is an error</exception>
/// <since>2.0</since>
public static string EncodeObject(Serializable serializableObject, int options)
{
if (serializableObject == null)
{
throw new ArgumentNullException("Cannot serialize a null object.");
}
// end if: null
// Streams
ByteArrayOutputStream baos = null;
OutputStream b64os = null;
GZIPOutputStream gzos = null;
ObjectOutputStream oos = null;
try
{
// ObjectOutputStream -> (GZIP) -> Base64 -> ByteArrayOutputStream
baos = new ByteArrayOutputStream();
b64os = new Base64.OutputStream(baos, Encode | options);
if ((options & Gzip) != 0)
{
// Gzip
gzos = new GZIPOutputStream(b64os);
oos = new ObjectOutputStream(gzos);
}
else
{
// Not gzipped
oos = new ObjectOutputStream(b64os);
}
oos.WriteObject(serializableObject);
}
catch (IOException e)
{
// end try
// Catch it and then throw it immediately so that
// the finally{} block is called for cleanup.
throw;
}
finally
{
// end catch
try
{
oos.Close();
}
catch (Exception)
{
}
try
{
gzos.Close();
}
catch (Exception)
{
}
try
{
b64os.Close();
}
catch (Exception)
{
}
try
{
baos.Close();
}
catch (Exception)
{
}
}
// end finally
// Return value according to relevant encoding.
try
{
return Sharpen.Runtime.GetStringForBytes(baos.ToByteArray(), PreferredEncoding);
}
catch (UnsupportedEncodingException)
{
// end try
// Fall back to some Java default
return Sharpen.Runtime.GetStringForBytes(baos.ToByteArray());
}
}