patterncsharpMinor
Proper usage of "using" statement in base64 encode method
Viewed 0 times
methodstatementusageusingbase64properencode
Problem
I used the example on here, but with little changes in order to write a base64 encoder method. I have a few points about the code:
-
I would like to use
-
Inside the
-
Also, I would like to use an extra try-catch block which surrounds the
` public static bool ConvertToBase64(string inputFile, string outputFile)
{
try
{
using (FileStream inputFileStream = new FileStream(inputFile, FileMode.Open),
outputFileStream = new FileStream(outputFile, FileMode.Create))
{
try
{
ToBase64Transform base64Transform = new ToBase64Transform();
//Buffers for read/write operations
Byte[] outputBuffer = new byte[base64Transform.OutputBlockSize];
Byte[] inputBuffer = new byte[inputFileStream.Length];
//Offset to count the number of bytes transformed so far
int inputOffset = 0;
try
{
inputFileStream.Read(inputBuffer, 0, (int)inputFileStream.Length);
}
catch(Exce
-
I would like to use
using statement, but I do not want to use inner "using" statements, so I use multiple variable declaration in a single "using" statement. Is that suitable in order to benefit from "using" statement for more than one object. Moreover, what are alternatives?-
Inside the
using statement block, I used different try-catch blocks for each operation that can be throw an exception. I did not catch specific exceptions first an just use Exception class, but my goal is just return true or false which indicates the success status of the method. Is it a good practice to use different try-catch blocks and use "return" statement inside these blocks for resource handling?-
Also, I would like to use an extra try-catch block which surrounds the
using blocks for the classes which have Dispose() methods that can throw exceptions also. This is also discussed here. Is that a common approach, or should I completely stop using using statements, for these cases.` public static bool ConvertToBase64(string inputFile, string outputFile)
{
try
{
using (FileStream inputFileStream = new FileStream(inputFile, FileMode.Open),
outputFileStream = new FileStream(outputFile, FileMode.Create))
{
try
{
ToBase64Transform base64Transform = new ToBase64Transform();
//Buffers for read/write operations
Byte[] outputBuffer = new byte[base64Transform.OutputBlockSize];
Byte[] inputBuffer = new byte[inputFileStream.Length];
//Offset to count the number of bytes transformed so far
int inputOffset = 0;
try
{
inputFileStream.Read(inputBuffer, 0, (int)inputFileStream.Length);
}
catch(Exce
Solution
SRP
Your method is responsible of to many things, like
So I would suggest to add some more methods
As already stated in the comment, the inner
About the
If you don't need them, don't use them. As you are reading/writing to/from a file, you can just use the above methods, which internally using a
If you need to use
Comments
Don't use comments to describe what you are doing, but why you are doing something. Especially if it is crystal clear what you are doing like
this comment is just useless.
Magic numbers
As you need a comment to describe
you should better use a const for that
If you still want the inner
Otherwise your code looks good. The naming of your fields is also meaningful.
Your method is responsible of to many things, like
- reading from a file
- writing to a file
- converting to base64
So I would suggest to add some more methods
public static bool ConvertToBase64(string inputFile, string outputFile)
{
try
{
byte[] input = System.IO.File.ReadAllBytes(inputFile);
byte[] base64 = ConvertToBase64(input);
System.IO.File.WriteAllBytes(outputFile,base64);
return true;
}
catch(Exception)
{
return false;
}
}
private byte[] ConvertToBase64(byte[] input)
{
}As already stated in the comment, the inner
try..catch statements are useless. Your outer try..catch would handle the exceptions in the same way. About the
using statements If you don't need them, don't use them. As you are reading/writing to/from a file, you can just use the above methods, which internally using a
using statement. Always use the right tool to do the job. If you need to use
using statements, you can also stack them like using (disposable1)
using (disposible2)
{
}Comments
Don't use comments to describe what you are doing, but why you are doing something. Especially if it is crystal clear what you are doing like
//Buffers for read/write operations
Byte[] outputBuffer = new byte[base64Transform.OutputBlockSize];
Byte[] inputBuffer = new byte[inputFileStream.Length];this comment is just useless.
Magic numbers
As you need a comment to describe
if (inputOffset % 19 == 0)you should better use a const for that
19 (but don't ask me how to name it) If you still want the inner
try..catch you can simply use catch(Exception) as there is no need for ex. Otherwise your code looks good. The naming of your fields is also meaningful.
Code Snippets
public static bool ConvertToBase64(string inputFile, string outputFile)
{
try
{
byte[] input = System.IO.File.ReadAllBytes(inputFile);
byte[] base64 = ConvertToBase64(input);
System.IO.File.WriteAllBytes(outputFile,base64);
return true;
}
catch(Exception)
{
return false;
}
}
private byte[] ConvertToBase64(byte[] input)
{
}using (disposable1)
using (disposible2)
{
}//Buffers for read/write operations
Byte[] outputBuffer = new byte[base64Transform.OutputBlockSize];
Byte[] inputBuffer = new byte[inputFileStream.Length];if (inputOffset % 19 == 0)Context
StackExchange Code Review Q#67806, answer score: 7
Revisions (0)
No revisions yet.