A
andoni.oconchubhair
Hi all,
I have spent my working day wrestling with a piece of code which
"should" work but of course doesn't! It took me all of 30 minute to
write and I have spent the last 9 hours looking through to find the
bug!!! :-( It is a class which reads a binary file from a socket and
writes it to a text file in Base64 format using the Axis
encoder/decoder.
ws.apache.org/axis/java
If I make the block-size big enough that it does not have to loop it is
fine but I want to be able to process massive files passing through a
mail server with a big (but not massive) block size.
My thanks in advance for any help,
Andoni.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.axis.encoding.Base64;
/**
* Class for testing the use of Base64 encoders/decoders.
*
* @author Andoni OConchubhair
*
*/
public class TestBase64 {
private static final String BASE_64_FILE_SUFFIX = ".64";
private static final int BLOCK_SIZE = 128;
/**
* Convert a Base64 encoded file back to it's original Binary file.
*
* @param myFile The Base64 encoded file (as a text file).
* @throws IOException No need to handle exception here.
*/
private void reCreateFile(File myFile) throws IOException {
FileReader myFr = new FileReader(myFile);
FileOutputStream myFos = new FileOutputStream("new_" +
myFile.getName().substring(0, myFile.getName().lastIndexOf(".")));
char[] charArray = new char[BLOCK_SIZE];
final int OFFSET = 0;
int len = 0;
while(BLOCK_SIZE == (len = myFr.read(charArray, OFFSET, BLOCK_SIZE)))
{
System.out.print("<d");
Base64.decode(charArray, OFFSET, len, myFos);
}
System.out.println();
if(-1 < len) {
System.out.println("Len left is: " + len);
Base64.decode(charArray, OFFSET, len, myFos);
}
myFos.flush();
myFos.close();
}
/**
* Convert a binary file into a Base64 encoded text file.
*
* @param myFile The binary file to be encoded.
* @throws IOException No need to handle any exceptions.
*/
private void encodeFile(File myFile) throws IOException {
FileInputStream myFis = new FileInputStream(myFile);
FileWriter out = new FileWriter(myFile.getName() +
BASE_64_FILE_SUFFIX);
byte[] binArray = new byte[BLOCK_SIZE];
int len = 0;
final int OFFSET = 0;
while(BLOCK_SIZE == (len = myFis.read(binArray, OFFSET, BLOCK_SIZE)))
{
System.out.print("e>");
Base64.encode(binArray, OFFSET, len, out);
}
// Protect agains situation where there is no remainter (len == -1).
if(-1 < len) {
Base64.encode(binArray, OFFSET, len, out);
}
out.flush();
out.close();
}
/**
* @param args
*/
public static void main(String[] args) throws IOException {
if(null == args || args.length == 0) {
System.out.println("Usage: java TestBase64 <filename>");
System.exit(0);
}
String fileName = args[0];
File myFile = new File(fileName);
if(!myFile.exists()) {
System.out.println("The specified file does not exist!");
System.exit(0);
}
TestBase64 my64 = new TestBase64();
if(fileName.endsWith(BASE_64_FILE_SUFFIX)) {
my64.reCreateFile(myFile);
}
else {
// Encode file.
my64.encodeFile(myFile);
}
}
}
I have spent my working day wrestling with a piece of code which
"should" work but of course doesn't! It took me all of 30 minute to
write and I have spent the last 9 hours looking through to find the
bug!!! :-( It is a class which reads a binary file from a socket and
writes it to a text file in Base64 format using the Axis
encoder/decoder.
ws.apache.org/axis/java
If I make the block-size big enough that it does not have to loop it is
fine but I want to be able to process massive files passing through a
mail server with a big (but not massive) block size.
My thanks in advance for any help,
Andoni.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.axis.encoding.Base64;
/**
* Class for testing the use of Base64 encoders/decoders.
*
* @author Andoni OConchubhair
*
*/
public class TestBase64 {
private static final String BASE_64_FILE_SUFFIX = ".64";
private static final int BLOCK_SIZE = 128;
/**
* Convert a Base64 encoded file back to it's original Binary file.
*
* @param myFile The Base64 encoded file (as a text file).
* @throws IOException No need to handle exception here.
*/
private void reCreateFile(File myFile) throws IOException {
FileReader myFr = new FileReader(myFile);
FileOutputStream myFos = new FileOutputStream("new_" +
myFile.getName().substring(0, myFile.getName().lastIndexOf(".")));
char[] charArray = new char[BLOCK_SIZE];
final int OFFSET = 0;
int len = 0;
while(BLOCK_SIZE == (len = myFr.read(charArray, OFFSET, BLOCK_SIZE)))
{
System.out.print("<d");
Base64.decode(charArray, OFFSET, len, myFos);
}
System.out.println();
if(-1 < len) {
System.out.println("Len left is: " + len);
Base64.decode(charArray, OFFSET, len, myFos);
}
myFos.flush();
myFos.close();
}
/**
* Convert a binary file into a Base64 encoded text file.
*
* @param myFile The binary file to be encoded.
* @throws IOException No need to handle any exceptions.
*/
private void encodeFile(File myFile) throws IOException {
FileInputStream myFis = new FileInputStream(myFile);
FileWriter out = new FileWriter(myFile.getName() +
BASE_64_FILE_SUFFIX);
byte[] binArray = new byte[BLOCK_SIZE];
int len = 0;
final int OFFSET = 0;
while(BLOCK_SIZE == (len = myFis.read(binArray, OFFSET, BLOCK_SIZE)))
{
System.out.print("e>");
Base64.encode(binArray, OFFSET, len, out);
}
// Protect agains situation where there is no remainter (len == -1).
if(-1 < len) {
Base64.encode(binArray, OFFSET, len, out);
}
out.flush();
out.close();
}
/**
* @param args
*/
public static void main(String[] args) throws IOException {
if(null == args || args.length == 0) {
System.out.println("Usage: java TestBase64 <filename>");
System.exit(0);
}
String fileName = args[0];
File myFile = new File(fileName);
if(!myFile.exists()) {
System.out.println("The specified file does not exist!");
System.exit(0);
}
TestBase64 my64 = new TestBase64();
if(fileName.endsWith(BASE_64_FILE_SUFFIX)) {
my64.reCreateFile(myFile);
}
else {
// Encode file.
my64.encodeFile(myFile);
}
}
}