コード例 #1
0
ファイル: Base64.cs プロジェクト: Redth/couchbase-lite-net
		// end decodeFromFile
		/// <summary>
		/// Convenience method for reading a binary file
		/// and base64-encoding it.
		/// </summary>
		/// <remarks>
		/// Convenience method for reading a binary file
		/// and base64-encoding it.
		/// <p>As of v 2.3, if there is a error,
		/// the method will throw an java.io.IOException. <b>This is new to v2.3!</b>
		/// In earlier versions, it just returned false, but
		/// in retrospect that's a pretty poor way to handle it.</p>
		/// </remarks>
		/// <param name="filename">Filename for reading binary data</param>
		/// <returns>base64-encoded string</returns>
		/// <exception cref="System.IO.IOException">if there is an error</exception>
		/// <since>2.1</since>
		public static string EncodeFromFile(string filename)
		{
			string encodedData = null;
			Base64.InputStream bis = null;
			try
			{
				// Set up some useful variables
				FilePath file = new FilePath(filename);
				byte[] buffer = new byte[Math.Max((int)(file.Length() * 1.4 + 1), 40)];
				// Need max() for math on small files (v2.2.1); Need +1 for a few corner cases (v2.3.5)
				int length = 0;
				int numBytes = 0;
				// Open a stream
				bis = new Base64.InputStream(new BufferedInputStream(new FileInputStream(file)), 
					Couchbase.Lite.Support.Base64.Encode);
				// Read until done
				while ((numBytes = bis.Read(buffer, length, 4096)) >= 0)
				{
					length += numBytes;
				}
				// end while
				// Save in a variable to return
				encodedData = Sharpen.Runtime.GetStringForBytes(buffer, 0, length, Couchbase.Lite.Support.Base64
					.PreferredEncoding);
			}
			catch (IOException e)
			{
				// end try
				throw;
			}
			finally
			{
				// Catch and release to execute finally{}
				// end catch: java.io.IOException
				try
				{
					bis.Close();
				}
				catch (Exception)
				{
				}
			}
			// end finally
			return encodedData;
		}
コード例 #2
0
ファイル: Base64.cs プロジェクト: Redth/couchbase-lite-net
		// end finally
		// end decodeToFile
		/// <summary>
		/// Convenience method for reading a base64-encoded
		/// file and decoding it.
		/// </summary>
		/// <remarks>
		/// Convenience method for reading a base64-encoded
		/// file and decoding it.
		/// <p>As of v 2.3, if there is a error,
		/// the method will throw an java.io.IOException. <b>This is new to v2.3!</b>
		/// In earlier versions, it just returned false, but
		/// in retrospect that's a pretty poor way to handle it.</p>
		/// </remarks>
		/// <param name="filename">Filename for reading encoded data</param>
		/// <returns>decoded byte array</returns>
		/// <exception cref="System.IO.IOException">if there is an error</exception>
		/// <since>2.1</since>
		public static byte[] DecodeFromFile(string filename)
		{
			byte[] decodedData = null;
			Base64.InputStream bis = null;
			try
			{
				// Set up some useful variables
				FilePath file = new FilePath(filename);
				byte[] buffer = null;
				int length = 0;
				int numBytes = 0;
				// Check for size of file
				if (file.Length() > int.MaxValue)
				{
					throw new IOException("File is too big for this convenience method (" + file.Length
						() + " bytes).");
				}
				// end if: file too big for int index
				buffer = new byte[(int)file.Length()];
				// Open a stream
				bis = new Base64.InputStream(new BufferedInputStream(new FileInputStream(file)), 
					Couchbase.Lite.Support.Base64.Decode);
				// Read until done
				while ((numBytes = bis.Read(buffer, length, 4096)) >= 0)
				{
					length += numBytes;
				}
				// end while
				// Save in a variable to return
				decodedData = new byte[length];
				System.Array.Copy(buffer, 0, decodedData, 0, length);
			}
			catch (IOException e)
			{
				// end try
				throw;
			}
			finally
			{
				// Catch and release to execute finally{}
				// end catch: java.io.IOException
				try
				{
					bis.Close();
				}
				catch (Exception)
				{
				}
			}
			// end finally
			return decodedData;
		}