/// <summary> /// Decodes data from Base64 notation, automatically /// detecting gzip-compressed data and decompressing it. /// </summary> /// <remarks> /// Decodes data from Base64 notation, automatically /// detecting gzip-compressed data and decompressing it. /// </remarks> /// <param name="s">the string to decode</param> /// <param name="options">encode options such as URL_SAFE</param> /// <returns>the decoded data</returns> /// <exception cref="System.IO.IOException">if there is an error</exception> /// <exception cref="System.ArgumentNullException">if <tt>s</tt> is null</exception> /// <since>1.4</since> public static byte[] Decode(string s, int options) { if (s == null) { throw new ArgumentNullException("Input string was null."); } // end if byte[] bytes; try { bytes = Sharpen.Runtime.GetBytesForString(s, PreferredEncoding); } catch (UnsupportedEncodingException) { // end try bytes = Sharpen.Runtime.GetBytesForString(s); } // end catch //</change> // Decode bytes = Decode(bytes, 0, bytes.Length, options); // Check to see if it's gzip-compressed // GZIP Magic Two-Byte Number: 0x8b1f (35615) bool dontGunzip = (options & DontGunzip) != 0; if ((bytes != null) && (bytes.Length >= 4) && (!dontGunzip)) { int head = ((int)bytes[0] & unchecked((int)(0xff))) | ((bytes[1] << 8) & unchecked( (int)(0xff00))); if (GZIPInputStream.GzipMagic == head) { ByteArrayInputStream bais = null; GZIPInputStream gzis = null; ByteArrayOutputStream baos = null; byte[] buffer = new byte[2048]; int length = 0; try { baos = new ByteArrayOutputStream(); bais = new ByteArrayInputStream(bytes); gzis = new GZIPInputStream(bais); while ((length = gzis.Read(buffer)) >= 0) { baos.Write(buffer, 0, length); } // end while: reading input // No error? Get new bytes. bytes = baos.ToByteArray(); } catch (IOException e) { // end try Sharpen.Runtime.PrintStackTrace(e); } finally { // Just return originally-decoded bytes // end catch try { baos.Close(); } catch (Exception) { } try { gzis.Close(); } catch (Exception) { } try { bais.Close(); } catch (Exception) { } } } } // end finally // end if: gzipped // end if: bytes.length >= 2 return bytes; }
/// <summary> /// Attempts to decode Base64 data and deserialize a Java /// Object within. /// </summary> /// <remarks> /// Attempts to decode Base64 data and deserialize a Java /// Object within. Returns <tt>null</tt> if there was an error. /// If <tt>loader</tt> is not null, it will be the class loader /// used when deserializing. /// </remarks> /// <param name="encodedObject">The Base64 data to decode</param> /// <param name="options">Various parameters related to decoding</param> /// <param name="loader">Optional class loader to use in deserializing classes.</param> /// <returns>The decoded and deserialized object</returns> /// <exception cref="System.ArgumentNullException">if encodedObject is null</exception> /// <exception cref="System.IO.IOException">if there is a general error</exception> /// <exception cref="System.TypeLoadException"> /// if the decoded object is of a /// class that cannot be found by the JVM /// </exception> /// <since>2.3.4</since> public static object DecodeToObject(string encodedObject, int options, ClassLoader loader) { // Decode and gunzip if necessary byte[] objBytes = Decode(encodedObject, options); ByteArrayInputStream bais = null; ObjectInputStream ois = null; object obj = null; try { bais = new ByteArrayInputStream(objBytes); // If no custom class loader is provided, use Java's builtin OIS. if (loader == null) { ois = new ObjectInputStream(bais); } else { // end if: no loader provided // Else make a customized object input stream that uses // the provided class loader. ois = new _ObjectInputStream_1358(loader, bais); } // Class loader knows of this class. // end else: not null // end resolveClass // end ois // end else: no custom class loader obj = ois.ReadObject(); } catch (IOException e) { // end try throw; } catch (TypeLoadException e) { // Catch and throw in order to execute finally{} // end catch throw; } finally { // Catch and throw in order to execute finally{} // end catch try { bais.Close(); } catch (Exception) { } try { ois.Close(); } catch (Exception) { } } // end finally return obj; }
/// <exception cref="System.IO.IOException"></exception> private void AssertNoCrLfHelper(string expect, string input) { byte[] inbytes = Sharpen.Runtime.GetBytesForString(input); byte[] expectBytes = Sharpen.Runtime.GetBytesForString(expect); for (int i = 0; i < 5; ++i) { byte[] buf = new byte[i]; InputStream @in = new ByteArrayInputStream(inbytes); ByteArrayOutputStream bos = new ByteArrayOutputStream(); OutputStream @out = new AutoCRLFOutputStream(bos); if (i > 0) { int n; while ((n = @in.Read(buf)) >= 0) { @out.Write(buf, 0, n); } } else { int c; while ((c = @in.Read()) != -1) { @out.Write(c); } } @out.Flush(); @in.Close(); @out.Close(); byte[] actualBytes = bos.ToByteArray(); NUnit.Framework.Assert.AreEqual(Encode(expectBytes), Encode(actualBytes), "bufsize=" + i); } }